【问题标题】:Algorithm to find all line segment intersections given n lines给定n条线找到所有线段交点的算法
【发布时间】:2015-11-20 06:03:28
【问题描述】:

我正在寻找一种算法来找到给定 n 条线段的所有交点。 下面是http://jeffe.cs.illinois.edu/teaching/373/notes/x06-sweepline.pdf的伪代码

输入 S[1 .. n] 是一个行数组 段。 label[i] 是第 i 个最左边端点的标签。

sort the endpoints of S from left to right
create an empty label sequence
for i ← 1 to 2n
    line ← label[i]
    if isLeftEndPoint[i]
        Insert(line)
        if Intersect(S[line], S[Successor(line)])
            return TRUE
        if Intersect(S[line], S[Predecessor(line)])
            return TRUE
    else
        if Intersect(S[Successor(line)], S[Predecessor(line)])
            return TRUE
        Delete(label[i])

return FALSE

将算法应用于下面设置的线,只检查一个交点。我应该怎么做才能知道其他 2 个交叉点的存在?

  1. 第[1]行进入
  2. line[2]进入,line[1]和line[2]的交点被检查。
  3. line[3]进入,line[2]和line[3]的交点被检查。
  4. line[4]进入,line[4]和line[1]的交点被检查。找到交叉口 A。
  5. line[4] 离开,没有任何检查。
  6. line[1] 离开,没有任何检查。
  7. line[2] 离开,没有任何检查。
  8. line[3] 离开,没有任何检查。

【问题讨论】:

  • 我认为所描述的算法只检查一组线是否相交。它没有找到交叉点,也没有计算交叉点。
  • @MOehm 我认为你是对的。我错误地认为该算法旨在找到所有的交点。
  • 寻找 Bentley-Ottmann 算法
  • 您声明要寻找algorithm to find all the intersection points given n line segments - 假设是二维的。你提出了一个过程,省略了它的名字(AnyIntersections)。 5.8. 不会发生,因为 AnyIntersections 以 4. 终止,返回 TRUE。 你的问题是什么?

标签: algorithm geometry computational-geometry intersection line-segment


【解决方案1】:

标准线方程 Ax+By=C

由方程标准线定义的直线的斜率(m)为

m = -(A/B)

点斜线方程 y-y1=m(x-x1)

在点-斜线方程中代入 m = (-A/B) y2-y1 = (A/-B)*(x2-x1)

(y2-y1)/(x2-x1) = A/-B

因此:

A = y2-y1
B = x1-x2 C = 斧头+By

x = (C-By)/A

y = (C-Ax)/B

给定两行方程 A1x1+B1y1=C1 和 A2x2+B2y2=C2。
然后指定线之间的交点 通过使
A1x+B1y-C1 = A2x+B2y-C2

的点

A1x+B1y=C1
A2x+B2y=C2

A1B2x+B1B2y=B2C1(将第一个方程乘以 B2)
A1B2x+B1B2y-B2C1=0

A2B1x+B1B2y=B1C2(将第二个方程乘以 B1)
A2B1x+B1B2y-B1C2=0

等式两个方程
A1B2x+B1B2y-B2C1=A2B1x+B1B2y-B1C2
A1B2x+B1B2y-B2C1-A2B1x-B1B2y+B1C2=0
A1B2x-B2C1-A2B1x+B1C2=0
A1B2x-A2B1x=B2C1-B1C2
x(A1B2-A2B1)=B2C1-B1C2

x = (B2C1-B1C2)/A1B2-A2B1

A1x+B1y=C1
A2x+B2y=C2

A1A2x+A2B1y=A2C1(将第一个方程乘以 A2)
A1A2x+A2B1y-A2C1=0

A1A2x+A1B2y=A1C2(将第二个方程乘以 A1)
A1A2x+A1B2y-A1C2=0

两个方程相等

A1A2x+A2B1y-A2C1=A1A2x+A1B2y-A1C2
A1A2x+A2B1y-A2C1-A1A2x-A1B2y+A1C2=0
A1C2-A2C2=A1B2y-A2B1y
A1B2y-A2B1y=A1C2-A2C2
y(A1B2-A2B1)=A1C2-A2C1
y(A1B2-A2B1)=A1C2-A2C1
y = (A1C2-A2C1)/(A1B1-A2B1)

y 和 x 的分母是一样的 分母 = A1B1-A2B1

因此:

x = (B2C1-B1C2)/分母
y = (A1C2-A2C1)/分母

这些是两条线与点 (x1, y1), (x2, y2)
和 (x3, y3), (x4, y4) 相交的 x 和 y 坐标

现在对于一条线段,它是相同的,但我们需要检查 x 或 y 坐标是否在两个线段中。这意味着在两个值较小的线段的x坐标和两个值较大的线段的x坐标之间

这是一个 C++ 程序,如果段相交则返回 true,否则返回 false。如果线段相交,则将交点存储在变量 i 中。

struct Point
{
    float x, y;
};

//p1 and p2 are the points of the first segment
//p3 and p4 are the points of the second segment
bool intersection(Point p1, Point p2, Point p3, Point p4, Point &i)
{
    float max1; //x-coordinate with greater value in segment 1
    float min1; //x-coordinate with lesse value in segment 1
    float max2; //x-coordinate with greater value in segment 2
    float min2; //x-coordinate with lesser value in segment 2
    float A1 = p2.y - p1.y;
    float B1 = p1.x - p2.x;
    float C1 = A1 * p1.x + B1 * p1.y;
    float A2 = p4.y - p3.y;
    float B2 = p3.x - p4.x;
    float C2 = A2 * p3.x + B2 * p3.y;
    float denom = A1 * B2 - A2 * B1;

if (denom == 0.0) //When denom == 0, is because the lines are parallel
    return false; //Parallel lines do not intersect

i.x = (C1 * B2 - C2 * B1) / denom;
i.y = (A1 * C2 - A2 * C1) / denom;

if (p1.x > p2.x)
{
    max1 = p1.x;
    min1 = p2.x;

}
else
{
    max1 = p2.x;
    min1 = p1.x;
}

if (p3.x > p4.x)
{
    max2 = p3.x;
    min2 = p4.x;

}
else
{
    max2 = p4.x;
    min2 = p3.x;
}

//check if x coordinate is in both segments
if (i.x >= min1 && i.x <= max1 &&
    i.x >= min2 && i.x <= max2)
    return true;
return false; //Do no intersect, intersection of the lines is not between the segments

}

现在您只需在循环中比较所有段并将交点存储在数组中。

【讨论】:

  • (第一次编辑find all the intersection points given n line segments?)你需要检查多少对? (对于一对段,我会先做更简单的“范围重叠测试”。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多