Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Creating a circle of circles  (Read 1906 times)

0 Members and 1 Guest are viewing this topic.

Nickalus

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Creating a circle of circles
« on: September 08, 2014, 05:31:49 am »
Hey guys, I'm working on a small project. I'm wanting to create a circle of circles (weird right? :P) Anyway I know the trig formulas of using cos and sin for creating the circle, but when doing this I either get just one circle placed at an offset, or I get a continuous circle (I know the reasioning for that) Anyway here is what I have for my code:
#include <SFML/Graphics.hpp>
#include <cmath>

#define PI 3.14159265

int main()
{
    // create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "woot");

    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();
        }

        // clear the window with black color
        window.clear();

        sf::CircleShape shape(10.f);
        shape.setFillColor(sf::Color::Green);
        shape.setOrigin(shape.getLocalBounds().width/2,
                        shape.getLocalBounds().height/2);
        shape.setPosition(window.getSize().x/2, window.getSize().y/2);
        for(int i = 0; i < 360; i++)
        {
          shape.move(cos(i*PI/180), sin(i*PI/180));
          window.draw(shape);
        }

        // end the current frame
        window.display();
    }
}
 

This draws the continuous circle. If I want a circle placed every 20 degrees or so I should be able to change the i to increase by 20 each time or so I thought. Attached is an example of what I'm wanting to draw. Anyway, does anyone have any advice? I'd really appreciate it! Thanks!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Creating a circle of circles
« Reply #1 on: September 08, 2014, 07:28:50 am »
If you're going to use move(), you'll need to move back to the centre when you've drawn it, ready for the next time.
cos and sin return a value between 0 and 1 so your "ring" has a radius of 1 whereas the circle that you're using to draw it has a radius of 10.
This would make your code work:
                for (int i = 0; i < 360; i += 20)
                {
                        const float ringRadius = 65.f;
                        shape.move(ringRadius * cos(i*PI / 180), ringRadius * sin(i*PI / 180));
                        window.draw(shape);
                        shape.move(-ringRadius * cos(i*PI / 180), -ringRadius * sin(i*PI / 180));
                }
 
but I'm not sure it's the best method. I would probably recommend using setPosition inside the loop and set the position from there.
e.g.
shape.setPosition(window.getSize().x / 2 + ringRadius * cos(i*PI / 180), window.getSize().y / 2 + ringRadius * sin(i*PI / 180));
 
instead of the moves.
Here it is again but casting to float explicitly (stops warnings):
shape.setPosition(static_cast<float>(window.getSize().x / 2 + ringRadius * cos(i*PI / 180)), static_cast<float>(window.getSize().y / 2 + ringRadius * sin(i*PI / 180)));
 
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Nickalus

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Creating a circle of circles (Solved)
« Reply #2 on: September 08, 2014, 01:10:56 pm »
Thanks a ton! I had originally went with setPosition , but was only able to get one position (You explained why). I went with your setPosition function, it worked, which is awesome! Thank you so much! I owe you a cookie man! :D

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Creating a circle of circles
« Reply #3 on: September 08, 2014, 01:44:36 pm »
You're welcome  :)
It turns out that you don't actually need a second move as it still draws a circle; it's just larger and not arranged around the centre (setPosition is still the better way). The problem you had, it seems, was the use of cos and sin's ranges; it was ringRadius that made it do something! ;)

Don't forget to use a constant/variable for the angle step rather than hard-coding it (as I did)!
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything