C#中如何检测直线是​​否相交?

发布于 2024-12-24 20:18:51 字数 602 浏览 3 评论 0原文

我想在 C# 中添加项目的附加功能,我已经可以在程序中绘制线条,但我想检测绘制的一条线的相交线并显示它们相交的点。是否可以?谢谢

我的程序还包括垂直距离的计算,这里是示例代码:

    public static Double PerpendicularDistance(Point Point1, Point Point2, Point Point)
    {
        Double area = Math.Abs(.5 * (Point1.X * Point2.Y + Point2.X * Point.Y + Point.X * Point1.Y - Point2.X * Point1.Y - Point.X * Point2.Y - Point1.X * Point.Y));
        Double bottom = Math.Sqrt(Math.Pow(Point1.X - Point2.X, 2) + Math.Pow(Point1.Y - Point2.Y, 2));
        Double height = area / bottom * 2;

        return height;
    }
}

这里的 POINT 是我的 X 和 Y 坐标的类。

I want to add additional feature of my project in C#, I can already draw lines in my program but I want to detect INTERSECTING LINES of a one line drawn and display the point they've intersect. Is it possible? Thank you

My program also includes computing for Perpendicular Distance, here is the sample code:

    public static Double PerpendicularDistance(Point Point1, Point Point2, Point Point)
    {
        Double area = Math.Abs(.5 * (Point1.X * Point2.Y + Point2.X * Point.Y + Point.X * Point1.Y - Point2.X * Point1.Y - Point.X * Point2.Y - Point1.X * Point.Y));
        Double bottom = Math.Sqrt(Math.Pow(Point1.X - Point2.X, 2) + Math.Pow(Point1.Y - Point2.Y, 2));
        Double height = area / bottom * 2;

        return height;
    }
}

The POINT here is a class for my X and Y coordinates.

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

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

发布评论

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

评论(1

等风也等你 2024-12-31 20:18:51

如果您试图找到两条线的交点,那么解决方案相当简单。

如果两条线的形式为 Ax + By = C:

float delta = a1*b2 - a2*b1;
if(delta == 0) 
    throw new ArgumentException("Lines are parallel");

float x = (b2*c1 - b1*c2)/delta;
float y = (a1*c2 - a2*c1)/delta;

我担心的是上面的评论,说只有一条画线。我不确定你的意思。这是否意味着应用程序提供一条线,而用户提供另一条线,还是我们正在处理线与自身相交的曲线?

If you are trying to find the intersection of two line, then the solution is fairly trivial.

If the two line are in the form Ax + By = C:

float delta = a1*b2 - a2*b1;
if(delta == 0) 
    throw new ArgumentException("Lines are parallel");

float x = (b2*c1 - b1*c2)/delta;
float y = (a1*c2 - a2*c1)/delta;

My concern is comment above that says there is only one drawn line. I'm not sure what you mean. Does it mean that the app provides one line and the user the other, or are we dealing in curved lines where the line intersects itself?

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