C#C
C#2y ago
7 replies
MechWarrior99

Arrange points in circle with a Sweep parameter

Hey, so I'm positioning some points in a circle. Which is easy, but I want to add a 'sweep' parameter.

The issue is if I just subtract 1 from the point count when calculating the angle increment. Then the last and first point will be at the same position when sweep is
1
. But if I don't, then the points will look like they are 'off by one'. Meaning with sweep at 0.5, the first and last points will not be perfectly inline.

Not sure how to go about fixing this. I included a little graphic showing what is currently happening and what I want to help
public static void PositionPointsInCircle(Vector2[] points, float radius, float sweep)
    {
        int count = points.Length;
        sweep = Math.Clamp(sweep, 0f, 1f);
        float totalAngle = 2 * MathF.PI * sweep;
        float angleIncrement = totalAngle / (count - 1);

        for (int i = 0; i < count; i++)
        {
            float angle = i * angleIncrement;
            points[i] = new Vector2(
                radius * MathF.Cos(angle),
                radius * MathF.Sin(angle)
            );
        }
    }
image.png
Was this page helpful?