两条非相交线之间的角度

时间:2018-09-26 11:43:20

标签: python shapely

请考虑l1l2两行,它们可能是相交的或不相交的。有什么优雅的方法可以找到它们之间的角度吗?谢谢

import numpy as np
from shapely import LineString

l1 = [(0,0), (1,1)]
l2 = [(0.5, 1), (0.5, 2)]

ls1 = LineString(l1)
ls2 = LineString(l2)

angle = compute_angle(ls1, ls2)

# This is I want to avoid because I have very big dataset and performance will degrade
def compute_anlge(l1, l2):
   #Extend line1 and line2 in both direction util they intersect

   # Find intersection point

   # Create new lines with the intersection point

   # find angle and return

2 个答案:

答案 0 :(得分:1)

首先,将每个线段移至原点,以找到角度:

seg = np.array(l2)
seg = seg[1] - seg[0]

然后,使用np.angle

 angle_l2 = np.angle(complex(*(seg), deg=True)

然后,您可以简单地计算角度之间的差。

答案 1 :(得分:0)

使用两条线的斜率,您只需获取反正切线即可找到它们相对于原点的角度。然后,获得两个角度的正差。全部完成!

import math
from math import pi as PI

l1 = [(0,0), (1,1)]
l2 = [(0.5, 1), (0.5, 2)]
m1 = (l1[1][1]-l1[0][1])/(l1[1][0]-l1[0][0])
m2 = (l2[1][1]-l2[0][1])/(l2[1][0]-l2[0][0])

angle_rad = abs(math.atan(m1) - math.atan(m2))
angle_deg = angle_rad*180/PI

有弧度和度值。