直接贴代码,摘自问题
import numpy as np
def perp(a):
b = np.empty_like(a)
b[0] = -a[1]
b[1] = a[0]
return b
# line segment a given by endpoints a1, a2
# line segment b given by endpoints b1, b2
# return
def seg_intersect(a1, a2, b1, b2):
'''
查找交点位置两条直线
'''
da = a2 - a1
db = b2 - b1
dp = a1 - b1
dap = perp(da)
denom = np.dot(dap, db)
num = np.dot(dap, dp)
return (num / denom.astype(float)) * db + b1
def main():
p1 = np.array([2.0, 2.0])
p2 = np.array([4.0, 3.0])
p3 = np.array([6.0, 0.0])
p4 = np.array([6.0, 3.0])
print(seg_intersect(p1, p2, p3, p4))
main()
网友评论