【问题标题】:Closest distance between to finite lines到有限线之间的最近距离
【发布时间】:2017-04-07 03:46:52
【问题描述】:

我有这个函数计算两条无限线之间的最近距离。

    public static double GetClosestDistanceBetweenLines(Vector3 line1Point, Vector3 line1Vector, Vector3 line2Point, Vector3 line2Vector)
    {
        var u = line1Vector;
        var v = line2Vector;
        var w = line1Point- line2Point;

        var a = Vector3.Dot(u, u);         // always >= 0
        var b = Vector3.Dot(u, v);
        var c = Vector3.Dot(v, v);         // always >= 0
        var d = Vector3.Dot(u, w);
        var e = Vector3.Dot(v, w);
        var D = a * c - b * b;        // always >= 0
        double sc, tc;

        // compute the line parameters of the two closest points
        if (D < Epsilon)
        {          // the lines are almost parallel
            sc = 0.0;
            tc = (b > c ? d / b : e / c);    // use the largest denominator
        }
        else
        {
            sc = (b * e - c * d) / D;
            tc = (a * e - b * d) / D;
        }

        // get the difference of the two closest points
        var dP = w + (sc * u) - (tc * v);  // =  L1(sc) - L2(tc)

        return dP.Length;   // return the closest distance
    }

但是,我想计算两条有限线之间的距离。

    public static double GetClosestDistanceBetweenLines(Vector3 line1Point1, Vector3 line1Point2, Vector3 line2Point1, Vector3 line2Point2)

我该怎么做?

【问题讨论】:

标签: c# math distance linear-algebra


【解决方案1】:

你有两个参数 sc 和 tc。

如果两者都在 0..1 范围内,则最近距离点位于线段内,距离有效。

如果一个段的参数超出此范围,则计算从另一段到该段适当末端的距离。例如,如果 sc

如果两个参数都超出范围,则查找线段末端组合的最小距离

【讨论】:

    猜你喜欢
    • 2020-01-26
    • 2023-03-11
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 2016-10-31
    • 2018-09-13
    • 1970-01-01
    • 2014-03-09
    相关资源
    最近更新 更多