线相交

发布于 2024-07-19 00:02:11 字数 22 浏览 5 评论 0原文

如何判断一条线是否被多边形截断

How to find whether a line intercepted in a polygon

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

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

发布评论

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

评论(4

菩提树下叶撕阳。 2024-07-26 00:02:12

根据您到底想要什么(我将假设一个线段,因为我本周刚刚编写了该代码),您可以将其分为两部分:

首先,我建议对行进行编码,

a*X + b*Y - c = 0

因为该形式没有行的极端情况例如 X=5Y=4X=3*Y

  • 测试直线是否与多边形的任意边相交
    • 测试两条线的端点是否位于另一条线的相对两侧。 建议的形式只需检查 LHS 的极性即可轻松实现此目的
  • 如果线上的点位于多边形内部,
    • 测试从输入线上的某个点到多边形外部的一条线是否在奇数个点处与多边形相交。 请注意,您需要检查多个位置是否出现相同的点,并且由于 FP 错误,这无法通过精确匹配测试来完成。

Depending on what exactly you want (I'll assume a line segment as I just wrote that code this week) you can get it in two parts:

first of all I'd suggest encoding lines as

a*X + b*Y - c = 0

because that form has no corner cases for lines like X=5, Y=4 or X=3*Y.

  • Test if the line intersects any side of the polygon
    • Test if the ends of both lines are on opposite sides of the other line. The suggested form makes this easy by just checking the polarity of the LHS
  • Test if a point on the line is inside the polygon
    • Test if a line from some point on your input line to the outside of the polygon corsses the polygon at an odd number of points. Be warned that you will need to check for the same point showing up from several places and because of FP errors this can't be done with an exact match test.
我喜欢麦丽素 2024-07-26 00:02:11

这个问题有点含糊,但我们还是尝试一下:

假设直线上的点 (x,y) 由方程 Ax + By + C = 0 定义。
那么我们显然可以通过评估来确定点 (x,y) 是否在线上
Ax + By + C。如果该点不在直线上,则 Ax + By + C 的符号告诉我们该点位于直线的哪一侧。
因此,通过检查多边形每个顶点 (x,y) 的表达式 Ax + By + C 的符号,我们可以确定多边形的所有点是否在线的同一侧。

(一个稍微不同的问题是确定多边形是否与线段相交。)

The question is a little bit ambiguous but let's try anyway:

Assume the points (x,y) on the line are defined by the equation Ax + By + C = 0.
Then we can obviously determine if a point (x,y) is on the line by evaluating
Ax + By + C. If the point is not on the line then the sign of Ax + By + C tells us on which side of the line the point is.
Hence by inspecting the signs of the expression Ax + By + C for each vertex (x,y) of the polygon, we can determine if all points of the polygon are on the same side of line or not.

(A slightly different problem would be to determine if a polygon intersects a line segment.)

一页 2024-07-26 00:02:11

中找到的此实现中阅读合理的答案

Point  * intersection2(Point * _line1, Point * _line2) {

Point  p1,p2,p3,p4;
p1=_line1[0]; p3=_line2[0];
p2=_line1[1]; p4=_line2[1];

// Store the values for fast access and easy
// equations-to-code conversion
double x1 = p1.x, x2 = p2.x, x3 = p3.x, x4 = p4.x;
double y1 = p1.y, y2 = p2.y, y3 = p3.y, y4 = p4.y;

double A1 = y2-y1;
double B1 = x1-x2;
double C1 = (A1*x1)+(B1*y1);

double A2 = y4-y3;
double B2 = x3-x4;
double C2 = A2*x3+B2*y3;

double det = A1*B2 - A2*B1;

if (det==0){
    return NULL;
}else{
    // Return the point of intersection
    Point  * ret = new CvPoint2D64f ();
    ret->x = (B2*C1 - B1*C2)/det;
    ret->y = (A1*C2 - A2*C1)/det;
    return ret;

}

您可以从某些网页参考


C++ 示例:几何概念、线相交及其应用、2D 绘图
通过 lbackstrom
来自 ucancode 网站

You can read a reasonable answer from this implementation found in some webpage

Point  * intersection2(Point * _line1, Point * _line2) {

Point  p1,p2,p3,p4;
p1=_line1[0]; p3=_line2[0];
p2=_line1[1]; p4=_line2[1];

// Store the values for fast access and easy
// equations-to-code conversion
double x1 = p1.x, x2 = p2.x, x3 = p3.x, x4 = p4.x;
double y1 = p1.y, y2 = p2.y, y3 = p3.y, y4 = p4.y;

double A1 = y2-y1;
double B1 = x1-x2;
double C1 = (A1*x1)+(B1*y1);

double A2 = y4-y3;
double B2 = x3-x4;
double C2 = A2*x3+B2*y3;

double det = A1*B2 - A2*B1;

if (det==0){
    return NULL;
}else{
    // Return the point of intersection
    Point  * ret = new CvPoint2D64f ();
    ret->x = (B2*C1 - B1*C2)/det;
    ret->y = (A1*C2 - A2*C1)/det;
    return ret;

}

}

Reference.
C++ Example: Geometry Concepts Line Intersection and its Applications, 2D drawing
By lbackstrom
from site ucancode

灼疼热情 2024-07-26 00:02:11

您需要坐标图上多边形的点以及直线的斜率以及 x 和 y 截距来查找该信息。 从那里开始就是简单的数学计算。

You need the points of the polygon on a coordinate graph and the slope and x and y intercept of the line to find that information. From there it's simple math.

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