P
P
Pragma Games2021-11-08 20:01:34
Geometry
Pragma Games, 2021-11-08 20:01:34

How to evenly place N units on a circle?

There is a central object "Officer", "Soldiers" are moving around it. Soldiers should be placed evenly and at the same distance around the "Officer" forming a circle. If there are too many soldiers to fit in this circle, it expands. Initially, there is a default radius of this circle and 3 soldiers. How to calculate at what points of this circle they should be located? As far as I understand for 3 soldiers, these should be the vertices of an inscribed equilateral triangle

public class OfficerComponent : MonoBehaviour
    {
        private Transform _collectionСenter;
        private float _radiusCollection;
        private List<Vector3> _locationPoints;
        
        private LinkedList<SolderLogicComponent> _solderLogicComponents;

        private void Awake()
        {
            _locationPoints = new List<Vector3>();
        }

        public void Init(Transform collectionСenter, float radiusCollection)
        {
            _collectionСenter = collectionСenter;
            _radiusCollection = radiusCollection;
        }

        private void RecalculationLocationPoints()
        {
            var countSection = _solderLogicComponents.Count;
        }

        private void DistributeTargetLocation()
        {
            var index = 0;
            
            for (var node = _solderLogicComponents.First; node != null; node = node.Next)
            {
                node.Value.TargetLocationPoint = _locationPoints[index];
                index++;
            }
        }

        public void AddSolder(SolderLogicComponent solderLogicComponent)
        {
            _solderLogicComponents.AddLast(solderLogicComponent);
            RecalculationLocationPoints();
            DistributeTargetLocation();
        }

        public void RemoveSolder(SolderLogicComponent solderLogicComponent)
        {
            _solderLogicComponents.Remove(solderLogicComponent);
            RecalculationLocationPoints();
            DistributeTargetLocation();
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2021-11-08
@PragmaGames

In the polar coordinate system, it is elementary to calculate their positions: the i-th soldier 2pi*i/nhas a fixed angle and radius.
To switch from the polar coordinate system to the usual one, use the trigonometric functions:

x_i = x_o+cos(alpha_i)*r_i
y_i = y_o+sin(alpha_i)*r_i

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question