c# - 沿着矢量线以大约定义的距离获得均匀分布的点

标签 c# distance vectormath

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

    /// <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 的情况。

关于c# - 沿着矢量线以大约定义的距离获得均匀分布的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26127763/

相关文章:

c# - 程序版本

c# - 获取 Web API 帮助页面以使用自定义路由约束

c# - 找出日期时间在未来多少天

apache-spark - 将两个数据框中的行与最近点连接起来

lua - 3d 中圆上的最近点。少了什么东西?

java - 任意维度数学 vector 的一类

c# - Windows 应用程序认证工具包失败 : Multi user session test

algorithm - 二叉树中节点之间的距离?

excel - 在 Excel 表格中返回超过 1 个值(用于计算总路线距离)

dart - Dart中的Vector3 subVector错误