如何计算C,C#/ .NET 2.0或Java中所有情况下点和线段之间的最短2D距离? [英] how to calculate shortest 2D distance between a point and a line segment in all cases in C, C# / .NET 2.0 or Java?

查看:159
本文介绍了如何计算C,C#/ .NET 2.0或Java中所有情况下点和线段之间的最短2D距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

点和线段之间的最短距离


我正在寻找一种方法来计算所有情况下的最小距离。我发现的解决方案存在的问题是:


  1. 图解概念图的解决方案始终显示垂直于线段的点,因此它介于线段的终点。我的几何技巧非常糟糕,所以我无法验证这些解决方案是否适用于所有情况。

  2. 算法解决方案是:使用fortran或其他语言不完全理解,
    b:被人标记为不完整,c:调用没有以任何方式描述的方法/函数(被认为是微不足道的)。


2 a,b和c的好例子是

点与线段之间的最短距离



我有二维线段作为双类型坐标对(x1,y1),(x2,y2),并指向双类型坐标(x3,y3)。 C#/ Java / C解决方案都值得赞赏。



感谢您的回答& BR:Matti

解决方案

也回答点与线段之间的最短距离,因为它收集所有语言的解决方案。答案也放在这里,因为这个问题具体要求C#解决方案。这是从 http://www.topcoder.com/ tc?d1 = tutorials& d2 = geometry1& module = Static

  //计算点积AB。 BC 
private double DotProduct(double [] pointA,double [] pointB,double [] pointC)
{
double [] AB = new double [2];
double [] BC = new double [2];
AB [0] = pointB [0] - pointA [0];
AB [1] = pointB [1] - pointA [1];
BC [0] = pointC [0] - pointB [0];
BC [1] = pointC [1] - pointB [1];
double dot = AB [0] * BC [0] + AB [1] * BC [1];

return dot;
}

//计算交叉产品AB x AC
private double CrossProduct(double [] pointA,double [] pointB,double [] pointC)
{
double [] AB = new double [2];
double [] AC = new double [2];
AB [0] = pointB [0] - pointA [0];
AB [1] = pointB [1] - pointA [1];
AC [0] = pointC [0] - pointA [0];
AC [1] = pointC [1] - pointA [1];
double cross = AB [0] * AC [1] - AB [1] * AC [0];

返回交叉;
}

//计算从A到B
的距离double距离(double [] pointA,double [] pointB)
{
double d1 = pointA [0] - pointB [0];
double d2 = pointA [1] - pointB [1];

返回Math.Sqrt(d1 * d1 + d2 * d2);
}

//计算从AB到C
的距离//如果isSegment为真,则AB是段而不是线。
double LineToPointDistance2D(double [] pointA,double [] pointB,double [] pointC,
bool isSegment)
{
double dist = CrossProduct(pointA,pointB,pointC)/距离(pointA,pointB);
if(isSegment)
{
double dot1 = DotProduct(pointA,pointB,pointC);
if(dot1> 0)
return Distance(pointB,pointC);

double dot2 = DotProduct(pointB,pointA,pointC);
if(dot2> 0)
return Distance(pointA,pointC);
}
返回Math.Abs​​(dist);
}


Possible Duplicate:
Shortest distance between a point and a line segment

i am looking for a way to calculate the minimum distance in all cases. the problems with solutions i found are:

  1. Solutions with graphical conceptual drawings show point always on perpendicular from line segment so it's "between line segment's end points". My geometry skills are horrible so i can't verify that these solutions work in all cases.

  2. Algorithm solutions are a: with fortran or some other language i don't fully understand, b: are flagged as incomplete by people, c: calling methods/functions that are not described in any way (considered trivial).

Good example of 2 a, b and c is

Shortest distance between a point and a line segment

i have the 2D line segment as double-type co-ordinate pair (x1, y1), (x2,y2) and point as double type co-ordinate (x3,y3). C#/Java/C solutions are all appreciated.

Thanks for your answers & BR: Matti

解决方案

Answered also Shortest distance between a point and a line segment because that gathers solutions in all languages. Answer put also here because this questions asks specifically a C# solution. This is modified from http://www.topcoder.com/tc?d1=tutorials&d2=geometry1&module=Static :

//Compute the dot product AB . BC
private double DotProduct(double[] pointA, double[] pointB, double[] pointC)
{
    double[] AB = new double[2];
    double[] BC = new double[2];
    AB[0] = pointB[0] - pointA[0];
    AB[1] = pointB[1] - pointA[1];
    BC[0] = pointC[0] - pointB[0];
    BC[1] = pointC[1] - pointB[1];
    double dot = AB[0] * BC[0] + AB[1] * BC[1];

    return dot;
}

//Compute the cross product AB x AC
private double CrossProduct(double[] pointA, double[] pointB, double[] pointC)
{
    double[] AB = new double[2];
    double[] AC = new double[2];
    AB[0] = pointB[0] - pointA[0];
    AB[1] = pointB[1] - pointA[1];
    AC[0] = pointC[0] - pointA[0];
    AC[1] = pointC[1] - pointA[1];
    double cross = AB[0] * AC[1] - AB[1] * AC[0];

    return cross;
}

//Compute the distance from A to B
double Distance(double[] pointA, double[] pointB)
{
    double d1 = pointA[0] - pointB[0];
    double d2 = pointA[1] - pointB[1];

    return Math.Sqrt(d1 * d1 + d2 * d2);
}

//Compute the distance from AB to C
//if isSegment is true, AB is a segment, not a line.
double LineToPointDistance2D(double[] pointA, double[] pointB, double[] pointC, 
    bool isSegment)
{
    double dist = CrossProduct(pointA, pointB, pointC) / Distance(pointA, pointB);
    if (isSegment)
    {
        double dot1 = DotProduct(pointA, pointB, pointC);
        if (dot1 > 0) 
            return Distance(pointB, pointC);

        double dot2 = DotProduct(pointB, pointA, pointC);
        if (dot2 > 0) 
            return Distance(pointA, pointC);
    }
    return Math.Abs(dist);
} 

这篇关于如何计算C,C#/ .NET 2.0或Java中所有情况下点和线段之间的最短2D距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆