在近似定义的距离处沿矢量线获取均匀分布的点



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

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

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

也许可以添加以下代码:

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

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

相关内容

  • 没有找到相关文章

最新更新