找到2段的交点
Find the intersection points of 2 segments
有没有计算2段交点的方法2 segments。假设它们在点 I 相交,我如何计算点 I 的坐标,因为我已经有了点 A、B、C、D 的坐标。
我尝试过的:
我试过这个方法:https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
这是我得到的结果:my result。可以看到,有些角点的2个端点不匹配,那么上面的方法无法计算出角点。
这是我用来求直线交点的方程式。请注意,这些是线,而不是线段,因此即使线段没有一直延伸到那里,它也会连接。
此函数正在寻找点斜率形式的两条线([slope, (x,y)] 的 2 元素列表)。它 returns 交点 [x,y] 或 None 如果线是平行的。
# calculates the intersection of two lines (in point-slope form) [slope, point]
# equation y = m(x - a) + b
# y = mx - ma + b
# where (a,b) is the point and m is the slope
def intersect(l1, l2):
# pull vars
m1 = l1[0];
m2 = l2[0];
a1, b1 = l1[1];
a2, b2 = l2[1];
# get 'x'
denom = m2 - m1;
if denom == 0:
return None; # lines are parallel
numer = m2*a2 - m1*a1 - b2 + b1;
x = numer / denom;
# get 'y'
y = m1 * (x - a1) + b1;
return [x, y];
有没有计算2段交点的方法2 segments。假设它们在点 I 相交,我如何计算点 I 的坐标,因为我已经有了点 A、B、C、D 的坐标。
我尝试过的:
我试过这个方法:https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
这是我得到的结果:my result。可以看到,有些角点的2个端点不匹配,那么上面的方法无法计算出角点。
这是我用来求直线交点的方程式。请注意,这些是线,而不是线段,因此即使线段没有一直延伸到那里,它也会连接。
此函数正在寻找点斜率形式的两条线([slope, (x,y)] 的 2 元素列表)。它 returns 交点 [x,y] 或 None 如果线是平行的。
# calculates the intersection of two lines (in point-slope form) [slope, point]
# equation y = m(x - a) + b
# y = mx - ma + b
# where (a,b) is the point and m is the slope
def intersect(l1, l2):
# pull vars
m1 = l1[0];
m2 = l2[0];
a1, b1 = l1[1];
a2, b2 = l2[1];
# get 'x'
denom = m2 - m1;
if denom == 0:
return None; # lines are parallel
numer = m2*a2 - m1*a1 - b2 + b1;
x = numer / denom;
# get 'y'
y = m1 * (x - a1) + b1;
return [x, y];