如何在 C、C# / .NET 2.0 或 Java 中计算所有情况下点和线段之间的最短 2D 距离?

发布于 2024-10-07 15:54:51 字数 711 浏览 2 评论 0原文

可能的重复:
点和线段之间的最短距离 < /p>

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

  1. 图形概念图的解决方案显示点始终与线段垂直,因此它位于“线段端点之间”。我的几何技能很糟糕,所以我无法验证这些解决方案是否适用于所有情况。

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

2 a、b 和 c 的一个很好的例子是

a 之间的最短距离点和线段

我将二维线段作为双精度类型坐标对(x1,y1),(x2,y2),将点作为双精度类型坐标(x3,y3)。 C#/Java/C 解决方案都受到赞赏。

感谢您的回答& BR:马蒂

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔一刀 2024-10-14 15:54:51

还回答了点和线段之间的最短距离 因为它收集了所有语言的解决方案。答案也放在这里,因为这个问题专门询问 C# 解决方案。这是从 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);
} 

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);
} 
如何视而不见 2024-10-14 15:54:51

如果您有线

L: A * x + B * y + C = 0

则从该线到点 (x1, y1) 的距离为 abs(A * x1 + B * y1 + C) / sqrt(A * A + B * B)。在你的情况下,如果你有间隔,(xa, ya); (xb, yb) 你应该找到 min( distance(x1, y1, xa, ya), distance(x1, y1, xb, yb)) 然后看看是否与 (x1,y1)到线L在区间上,那么答案就是距离就是它。否则两个距离的最小值。

If you have line

L: A * x + B * y + C = 0

Then distance from this line to point (x1, y1) is abs(A * x1 + B * y1 + C) / sqrt(A * A + B * B). in your case if you has interval, (xa, ya); (xb, yb) you should find min( distance(x1, y1, xa, ya), distance(x1, y1, xb, yb)) then see if perpendecular from (x1, y1) to line L is on the interval, then the answer is the distance is it. otherwise min of two distances.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文