获取矩形和直线的交点

Iev*_*ida 7 c# math wpf

我需要得到矩形和直线的交点.在矩形(矩形的中心)内有B点,在外面有A点.我需要在一个矩形边框上找到C点.我也得到了矩形的宽度和高度.

在此输入图像描述

所有这些都将是WPF应用程序,所以如果任何构建功能我将非常高兴.

Iev*_*ida 4

C#、WPF 的解决方案:

 /// <summary>
    /// Get Intersection point
    /// </summary>
    /// <param name="a1">a1 is line1 start</param>
    /// <param name="a2">a2 is line1 end</param>
    /// <param name="b1">b1 is line2 start</param>
    /// <param name="b2">b2 is line2 end</param>
    /// <returns></returns>
    public static Vector? Intersects(Vector a1, Vector a2, Vector b1, Vector b2)
    {
        Vector b = a2 - a1;
        Vector d = b2 - b1;
        var bDotDPerp = b.X * d.Y - b.Y * d.X;

        // if b dot d == 0, it means the lines are parallel so have infinite intersection points
        if (bDotDPerp == 0)
            return null;

        Vector c = b1 - a1;
        var t = (c.X * d.Y - c.Y * d.X) / bDotDPerp;
        if (t < 0 || t > 1)
            {
            return null;
        }

        var u = (c.X * b.Y - c.Y * b.X) / bDotDPerp;
        if (u < 0 || u > 1)
        {
            return null;
        }

        return a1 + t * b;
    }
Run Code Online (Sandbox Code Playgroud)

编辑 找到上述答案来自的问题的链接。