Public Group

# How do i create a circle in SFML?

This topic is 3171 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Trying to figure this out has really shown me that I dont remember much of my highschool math involving PI, sine, cosine and tangent. Im currently trying to draw a circle in SFMl, and im having trouble figuring out how to do it. Initially I was thinking of having a circle with 32 "sides" or edges, and therefore creating a for loop that loops 32 times. The problem is I dont know how to calculate where the point moves to next. I know the angle of each edge would be 360/32. I know there's probably a simpler way to perhaps use PI * 2 or one of the other functions from math.h. Just been a while since ive done this stuff. Any help to get me along would be greatly appreciated.

##### Share on other sites
There are predefined shapes in SFML so you could use the circle shape:
sf::Shape Circle = sf::Shape::Circle(X, Y, Radius, Color, [Outline], [OutlineColor]);

If you really want to draw a circle "by hand", you have to divide pi*2 by the number of sides you need, then iterate the value of (pi*2/i) and use
y=sin(value)*radius;x=cos(value)*radius;

Let me know if this works out :)

##### Share on other sites
Didnt realize SFML had the predefined shapes though. But thanks for explaining how it's done by hand, always a good thing to know.

##### Share on other sites
The unit circle is a great tool to keep in mind, with the idea that any point on a circle of radius 1 centered at the origin is at (cosine theta, sine theta) where theta is the angle of rotation from the positive x axis. It also has the bonus of helping to remember the cos/sin values of major angles like 0, 90, etc.

I draw this every time I'm working out trig-related problems:

##### Share on other sites
You could loop through each pixel on the screen and calculate the distance between that pixel and the centre of the circle and if that distance is equal to the radius of the circle then draw it! xD Though that'd be stupid, don't actually do that!

##### Share on other sites
Just want to double check my code here, as I'd to learn this and get it right:

The screen renders fine, however I do not see a blue circle.

#include "stdafx.h"#include <SFML/Graphics.hpp>#define PI 3.141592int _tmain(int argc, _TCHAR* argv[]){	sf::RenderWindow app(sf::VideoMode(800, 600, 32), "Title");	app.SetFramerateLimit(60);	app.UseVerticalSync(true);	sf::Shape player;	double degrees = 360.0 / 32.0;	for(double i = 0; i < 32; i++)	{		double value = (PI * 2) / i;		double y = sin(value) * degrees;		double x = cos(value) * degrees;		player.AddPoint((float)x, (float)y, sf::Color(0, 0, 255));	}	player.EnableFill(true);	player.SetPosition(sf::Vector2f(100, 100));	while(app.IsOpened())	{		sf::Event e;		while(app.GetEvent(e))		{			if(e.Type == sf::Event::Closed)			{				app.Close();			}			// if escape was pressed			if((e.Type == sf::Event::KeyPressed) && (e.Key.Code == sf::Key::Escape))			{				app.Close();			}		}		float timePassed = app.GetFrameTime();		app.Clear();		app.Draw(player);		app.Display();	}	return EXIT_SUCCESS;}

##### Share on other sites
Quote:
 sf::Shape player;double degrees = 360.0 / 32.0;for(double i = 0; i < 32; i++){ double value = (PI * 2) / i; double y = sin(value) * degrees; double x = cos(value) * degrees; player.AddPoint((float)x, (float)y, sf::Color(0, 0, 255));}

Here you're creating an arc (points on a circle from 2*pi to 2*pi/31) with a radius of 11.25.

It should be:

const double radius = ??;const int maxSides = 32;for ( int i = 0; i < 32; ++i ) {	double angle = (PI * 2) * static_cast<double>(i)/maxSides;	double y = sin(angle) * radius;	double x = cos(angle) * radius;	player.AddPoint((float)x, (float)y, sf::Color(0, 0, 255));}

But as Wh1sp3r stated:

const float radius = ??;sf::Shape player = sf::Shape::Circle( 0, 0, radius, sf::Color( 0, 0, 255 ) );

Does the same thing (but with 40 segments instead of 32).

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633768
• Total Posts
3013743
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!