16
Graphics / It seems that CircleShape works not so fast as it can
« on: October 12, 2015, 11:55:57 am »
I looked in the code of CircleShape and it seems that it uses not so fast algorithm to calculate circle points.
Here is the code from SFML 2.3.2:
here is a more fast algorithm:
I taken this algorithm here: http://slabode.exofire.net/circle_draw.shtml
And it works just like a charm
Here is the code from SFML 2.3.2:
Vector2f CircleShape::getPoint(std::size_t index) const
{
static const float pi = 3.141592654f;
float angle = index * 2 * pi / m_pointCount - pi / 2;
float x = std::cos(angle) * m_radius;
float y = std::sin(angle) * m_radius;
return Vector2f(m_radius + x, m_radius + y);
}
{
static const float pi = 3.141592654f;
float angle = index * 2 * pi / m_pointCount - pi / 2;
float x = std::cos(angle) * m_radius;
float y = std::sin(angle) * m_radius;
return Vector2f(m_radius + x, m_radius + y);
}
here is a more fast algorithm:
private void DrawCircle(RenderWindow window, Vector2f pos, float radius, Color color)
{
var list = new List<Vertex>();
const int numSegments = 50;
var theta = 2F * (float)Math.PI / numSegments;
var s = (float)Math.Sin(theta); //precalculate the sine and cosine
var c = (float)Math.Cos(theta);
var vector0 = new Vector2f(radius, 0F); //we start at angle = 0
for (var i = 0; i < numSegments; i++)
{
//apply the rotation matrix
var vector1 = new Vector2f(c * vector0.X - s * vector0.Y, s * vector0.X + c * vector0.Y);
list.Add(new Vertex(pos + vector0, color));
list.Add(new Vertex(pos + vector1, color));
vector0 = vector1;
}
window.Draw(list.ToArray(), PrimitiveType.Lines);
}
{
var list = new List<Vertex>();
const int numSegments = 50;
var theta = 2F * (float)Math.PI / numSegments;
var s = (float)Math.Sin(theta); //precalculate the sine and cosine
var c = (float)Math.Cos(theta);
var vector0 = new Vector2f(radius, 0F); //we start at angle = 0
for (var i = 0; i < numSegments; i++)
{
//apply the rotation matrix
var vector1 = new Vector2f(c * vector0.X - s * vector0.Y, s * vector0.X + c * vector0.Y);
list.Add(new Vertex(pos + vector0, color));
list.Add(new Vertex(pos + vector1, color));
vector0 = vector1;
}
window.Draw(list.ToArray(), PrimitiveType.Lines);
}
I taken this algorithm here: http://slabode.exofire.net/circle_draw.shtml
And it works just like a charm