2

所以我推出了一个方法,它返回给定距离的一组点。

    /// <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;
    }

该方法有效,但我最终要做的下一步是将这些点均匀分布在给定的向量上。这让我认为距离最终会变成近似距离,因为可能不可能完全均匀分布点确切的距离,但是只要方法返回起点,终点和它们之间均匀分布的点就可以了。有人可以帮我吗?

4

1 回答 1

2

也许添加此代码:

...
float totalDistance = (endingPoint - startingPoint).magnitude;
float sectionsCount = (float)Math.Round(totalDistance / distance, MidpointRounding.AwayFromZero);
distance = totalDistance / sectionsCount;
...

请务必检查sectionsCount为 0 的情况。

于 2014-09-30T19:20:50.957 回答