Tony
Tony

Reputation: 3638

Even distribution of objects on circle

I'm creating a circle dynamically based on two positions; the first is the centre, and second is the radius. I'm then determining how many of my objects will fit onto the perimeter of the circle by int fillCount = Mathf.RoundToInt(circumference / sizeOfObject);

So now I've got a circle, I know how many things will fit on it, I know the circumference, I know the radius, and I've cobbled together a function that will find the Vector3 of any given point on the circle.

What I can't figure out is how to evenly distribute each object. i.e, place an object every 'n' degrees. I don't think I need to worry about arc-reparameterization, or do I? I think the answer lies in finding the degree of placement by using the inverse cosine/sine, but I'm not sure and my maths isn't that good.

If anyone has any input or advice, I'd greatly appreciate it.

Upvotes: 1

Views: 3590

Answers (1)

ryeMoss
ryeMoss

Reputation: 4343

If you know how many objects are in the circle, you can divide 360deg by the number of objects to determine the angle on the circle each object will be placed. Since we know the point on a circle is located at x=r*cos(angle), y=r*sin(angle) you can add this Vector2 to the Vector2 center of your circle.

As an example, if you have 3 objects to place around the circle. Get your angle by dividing 360/3 = 120. Each object will be placed 120deg from each other.
Pick your starting point (we will pick 0 degrees),
Vector2(r*Mathf.Deg2Rad*cos(0), r*Mathf.Deg2Rad*sin(0)).
The next object will be at 120deg, which will be
Vector2(r*Mathf.Deg2Rad*cos(120), r*Mathf.Deg2Rad*sin(120)).
Similarly, the 3rd object will be at 240deg,
Vector2(r*Mathf.Deg2Rad*cos(240), r*Mathf.Deg2Rad*sin(240))

Upvotes: 4

Related Questions