Graphics 三维点到多边形线段的距离算法

Graphics 三维点到多边形线段的距离算法,graphics,vector,3d,Graphics,Vector,3d,我有一条路径,由n个3d坐标组成,所有坐标串联在一起,可以在中看到 我想找到我的点和多边形线段之间的最短距离。我可以计算一个点到一条线段的距离,但我想对更复杂的路径进行计算 是否有一种算法不依赖于测试每个线段到点的距离并存储最小值?任何指向正确方向的指针都会很棒 这是一个游戏项目,我想计算玩家与游戏中存在的河流之间的距离。河流将用多边形线段表示 谢谢/** *计算从点到直线分段路径的欧氏距离。 * *@param v测量点与距离的距离 *@param path当由线段顺序连接时,形成路径的点阵列

我有一条路径,由n个3d坐标组成,所有坐标串联在一起,可以在中看到

我想找到我的点和多边形线段之间的最短距离。我可以计算一个点到一条线段的距离,但我想对更复杂的路径进行计算

是否有一种算法不依赖于测试每个线段到点的距离并存储最小值?任何指向正确方向的指针都会很棒

这是一个游戏项目,我想计算玩家与游戏中存在的河流之间的距离。河流将用多边形线段表示

谢谢

/**
*计算从点到直线分段路径的欧氏距离。
*
*@param v测量点与距离的距离
*@param path当由线段顺序连接时,形成路径的点阵列
*@从v到形成线段的最近路径的返回距离
*
*@作者阿方索·桑托斯
*/
公共静电
双重的
距离路径(最终R3 v,最终R3[]路径)
{
double minDistance=double.MAX_值;
对于(int-pathPointIdx=1;pathPointIdx
R3 3D矢量代数java包的其余部分(自包含,无外部依赖)如下所示:

开源库的一部分

/**
*计算从点到直线分段路径的欧氏距离。
*
*@param v测量点与距离的距离
*@param path当由线段顺序连接时,形成路径的点阵列
*@从v到形成线段的最近路径的返回距离
*
*@作者阿方索·桑托斯
*/
公共静电
双重的
距离路径(最终R3 v,最终R3[]路径)
{
double minDistance=double.MAX_值;
对于(int-pathPointIdx=1;pathPointIdx
R3 3D矢量代数java包的其余部分(自包含,无外部依赖)如下所示:

开源库的一部分

也许扫描线算法合适?也许扫描线算法合适?
/**
 * Calculates the euclidean distance from a point to a line segmented path.
 *
 * @param v     the point from with the distance is measured
 * @param path  the array of points wich, when sequentialy joined by line segments, form a path
 * @return      distance from v to closest of the path forming line segments
 *
 * @author      Afonso Santos
 */
public static
double
distanceToPath( final R3 v, final R3[] path )
{
    double minDistance = Double.MAX_VALUE ;

    for (int pathPointIdx = 1  ;  pathPointIdx < path.length  ;  ++pathPointIdx)
    {
        final double d = distanceToSegment( v, path[pathPointIdx-1], path[pathPointIdx] ) ;

        if (d < minDistance)
            minDistance = d ;
    }

    return minDistance;
}


/**
 * Calculates the euclidean distance from a point to a line segment.
 *
 * @param v     the point
 * @param a     start of line segment
 * @param b     end of line segment
 * @return      distance from v to line segment [a,b]
 *
 * @author      Afonso Santos
 */
public static
double
distanceToSegment( final R3 v, final R3 a, final R3 b )
{
    final R3 ab = b.sub( a ) ;
    final R3 av = v.sub( a ) ;

    if (av.dot(ab) <= 0.0)              // Point is lagging behind start of the segment, so perpendicular distance is not viable.
        return av.modulus( ) ;          // Use distance to start of segment instead.

    final R3 bv = v.sub( b ) ;

    if (bv.dot(ab) >= 0.0)              // Point is advanced past the end of the segment, so perpendicular distance is not viable.
        return bv.modulus( ) ;          // Use distance to end of the segment instead.

    // Point is within the line segment's start/finish boundaries, so perpendicular distance is viable.
    return (ab.cross( av )).modulus() / ab.modulus() ;      // Perpendicular distance of point to segment.
}