So i have rolled a method which returns me a set of points at a given distance.
/// <summary>
/// Gets all the points between a two vectors at given distance, including the starting and the ending point.
/// </summary>
public static List<Vector2> GetPointsAlongTwoVectors(Vector2 startingPoint, Vector2 endingPoint, float distance)
{
Vector2 direction = (endingPoint - startingPoint).normalized;
float totalDistance = (endingPoint - startingPoint).magnitude;
float increasingDistance = 0.0f;
List<Vector2> points = new List<Vector2>();
points.Add(startingPoint);
if (totalDistance > distance)
{
do
{
increasingDistance += distance;
points.Add(startingPoint + increasingDistance * direction);
} while (increasingDistance + distance < totalDistance);
}
points.Add(endingPoint);
return points;
}
The method works, but what i eventually want to do next is to spread those points evenly across the given vector.This leads me thinking that the distance will eventually will turn to approximate distance since it will be maybe impossible to get evenly spread points with totally exact distance, but that is alright as soon as the method returns the start point, the end point and the evenly distributed points between them.Can anybody help me out?
Maybe add this code:
...
float totalDistance = (endingPoint - startingPoint).magnitude;
float sectionsCount = (float)Math.Round(totalDistance / distance, MidpointRounding.AwayFromZero);
distance = totalDistance / sectionsCount;
...
Be sure to check the case where sectionsCount
is 0.