Numpy and line intersections

前端 未结 7 2014
春和景丽
春和景丽 2020-11-30 00:21

How would I use numpy to calculate the intersection between two line segments?

In the code I have segment1 = ((x1,y1),(x2,y2)) and segment2 = ((x1,

7条回答
  •  抹茶落季
    2020-11-30 00:24

    Here's a (bit forced) one-liner:

    import numpy as np
    from scipy.interpolate import interp1d
    
    x = np.array([0, 1])
    segment1 = np.array([0, 1])
    segment2 = np.array([-1, 2])
    
    x_intersection = interp1d(segment1 - segment2, x)(0)
    # if you need it:
    y_intersection = interp1d(x, segment1)(x_intersection)
    

    Interpolate the difference (default is linear), and find a 0 of the inverse.

    Cheers!

提交回复
热议问题