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

Author Topic: Drawing circle segment  (Read 4740 times)

0 Members and 1 Guest are viewing this topic.

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Drawing circle segment
« on: October 17, 2015, 03:01:32 am »
I written a small function to render circle segment. It seems that it works ok.
Please review it, may be I didn't take into account something :)

        private static void DrawCircleSegment(
            RenderWindow window,
            Vector2f origin,
            float radius,
            float angle,
            float length,
            Color color,
            int maxPoints)
        {
            var ptCount = (int)(length * maxPoints / 2D * Math.PI + 0.5F);
            ptCount = Math.Min(ptCount, 2);
            ptCount = Math.Max(ptCount, maxPoints);
            var arc = GeometryHelper.CreateArcGeometry(radius, angle, length, ptCount);
            var center = new Vector2f(0F, 0F);
            var vertices = arc
                .Zip(arc.Skip(1), (p1, p2) => new[] { center, p1, p2, })
                .SelectMany(arg => arg)
                .Select(arg => new Vertex(origin + arg, color));
            window.Draw(vertices.ToArray(), PrimitiveType.Triangles);
        }

    public static class GeometryHelper
    {
        public static Vector2f[] CreateArcGeometry(
            float radius,
            float angle,    // 0F...2*PI
            float length,   // 0F...2*PI
            int segmentCount)
        {
            var theta = length / segmentCount;
            var sin = (float)Math.Sin(theta);
            var cos = (float)Math.Cos(theta);
            var point = new Vector2f(
                radius * (float)Math.Cos(angle),
                radius * (float)Math.Sin(angle));
            var result = new Vector2f[segmentCount + 1];
            for (var i = 0; i < result.Length; i++)
            {
                result[i] = point;
                point = new Vector2f(
                    cos * point.X - sin * point.Y,
                    sin * point.X + cos * point.Y);
            }
            return result;
        }
    }
 

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Drawing circle segment
« Reply #1 on: October 17, 2015, 01:55:10 pm »
Its going to be slow if you don't cache your vertices and recalculate them every time you draw.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: Drawing circle segment
« Reply #2 on: October 17, 2015, 04:23:09 pm »
Its going to be slow if you don't cache your vertices and recalculate them every time you draw.

I know, but I need to render just several segments to get a chart pie image
« Last Edit: October 25, 2015, 02:12:04 am by mkalex777 »