How do i create a circle in SFML?

Started by
5 comments, last by fastcall22 14 years ago
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.
Advertisement
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 :)
Didnt realize SFML had the predefined shapes though. But thanks for explaining how it's done by hand, always a good thing to know.
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:

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

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!
It's not a bug... it's a feature!
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;}
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).

This topic is closed to new replies.

Advertisement