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!
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)));