# Creating vector points which are a certain distance away from each other

Hi guys!

I want to create 4 vector points which are a certain distance away from each other e.g magnitude of 2f.
I know the forLoop part to create the 1st point, but checking the distance from all the other points and creating other points is the tough part…

``````void getPoints(){

for(int i = 0; i < 4; i++ ){
point = new Vector3(Random.Range(minX,maxX), Random.Range(minY, maxY),0f);
if(i=0){
}
}
}
``````

Greetings, @wacasce

Just to be clear, you are talking about a 2D space. There is no way in 2D that four points can all be equidistant. You need a regular tetrahedron to have four points all equidistant and that’s 3D by definition. If you mean that you generate four with each successive one being a fixed distance away from its previous one (even though it may be close to one previously created) then, yes you can do it. Here’s the code.

``````using UnityEngine;

public class Temp : MonoBehaviour
{
float distance = 2;
Vector2[] points = new Vector2;

private void Start()
{
points = Vector2.zero;

for (int i = 1; i < 4; i++)
{
Vector2 newPos = Random.insideUnitCircle.normalized;
points _= newPos * distance + points[i - 1];_
``````

}
}
}

The problem is that you could get bunched points so they won’t feel as if they are a fixed distance apart.

@SurreyMuso is correct here, in 2D space you can only have 3 points, where each of them has the exact same distance to the other 2 points. A fourth point would need 3D space.

If we assume that you only want to space them out evenly then you should either have a grid (same distance to all neighbouring points) or all points on a circle (same distance to the next 2 neighbouring points)

so grid:

``````int gridsize = 10;
int distance = 2;
for(int i = 0; i < gridsize; i++ ){
for(int j = 0; j < gridsize; j++ ){
}
}
``````

circle:

``````int points = 4;
float circleSize = 2f;
for(int i = 0; i < points; i++ ){
float angle = i*2*Mathf.PI/points;
}
``````

dunno if that helps, let us know if this is what you were searching for.

OK. Let’s try this. imagine a game of Noughts and Crosses (tic-tac-toe). The four corner squares are all separated by the inner squares. Let’s get the coordinates of the corner squares and then find random points within a circle around those coordinates. That’s the basis of my code.

``````using UnityEngine;

public class CreatePoints : MonoBehaviour
{
void Start()
{
float starter = 4;   // Use this as the basis for the outer coordinates
float radius = 2;    // Use this as the radius for the random circle

Vector2[] origins = new Vector2
{
new(-starter,  starter),
new( starter,  starter),
new(-starter, -starter),
new( starter, -starter)
};

Vector2[] points = new Vector2;
for (int i = 0; i < 4; i++)
{
points _= Random.insideUnitCircle * radius + origins*;*_
``````

}
}
}
So now you have the array called points[] with the random points that you were after. I’m assuming 2D but this could be adapted to 3D, quite easily…