验证2个LineString(形状)是否平行

时间:2018-03-05 17:02:55

标签: python numpy shapely

我有2 shapely.LineString,我想验证它们是否平行。

a = LineString(((0, 0), (0  , 1)))
b = LineString(((1, 0), (0.9, 1))) #Not intersecting, but also not parallel)

当我使用交叉点intersects时,它返回False,因为2条线不相交,但它们也不是平行的。

Shapely中是否有方法来验证并行性?我可以使用角度(通过np.arctan)。

2 个答案:

答案 0 :(得分:0)

我想出的最好的:

def isparrallel(ls1, ls2):
    xs1, xe1 = ls1.xy[0]
    ys1, ye1 = ls1.xy[1]
    xs2, xe2 = ls2.xy[0]
    ys2, ye2 = ls2.xy[1]

    if xs1 == xe1:
        angle1 = np.pi/2
    else:
        angle1 = np.arctan((ye1-ys1)/(xe1-xs1))
    if xs2 == xe2:
        angle2 = np.pi/2
    else:
        angle2 = np.arctan((ye2-ys2)/(xe2-xs2))

    return True if angle1 == angle2 else False

>> isparrallel(a,b)
True

欢迎改进: - )

答案 1 :(得分:0)

a = [(0,1), (0,0)]
b = [(0,0), (0,-1)]
c = [(2,1), (2,0)]

def is_parallel(line1, line2):
    vector_a_x = line1[1][0] - line1[0][0]
    vector_a_y = line1[1][1] - line1[0][1]
    vector_b_x = line2[1][0] - line2[0][0]
    vector_b_y = line2[1][1] - line2[0][1]
    
    if vector_a_x * vector_b_y == vector_a_y * vector_b_x:
        return True 
    else:
        return False 

is_parallel(a, b) 
# True