0

I have two arrays (x1,y1) and (x2,y2). I can plot them individually and see the point of intersection (xi,yi).

But I want to find the point of intersection in the code and not by plotting and seeing them. I wrote:

idx = Numeric.argwhere(np.diff(np.sign(y2-y1))).flatten()
print x[idx]

IS there something wrong I am doing?

Mikev
  • 2,012
  • 1
  • 15
  • 27
user274219
  • 19
  • 1

1 Answers1

0

Using the function given in answer to this question allows you to compute the intersection between the two lines:

# Function from referenced question
def line_intersection(line1, line2):
    xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])
    ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1])

    def det(a, b):
        return a[0] * b[1] - a[1] * b[0]

    div = det(xdiff, ydiff)
    if div == 0:
       raise Exception('lines do not intersect')

    d = (det(*line1), det(*line2))
    x = det(d, xdiff) / div
    y = det(d, ydiff) / div
    return x, y

# Sample line arrays
x1 = (1,5)
y1 = (3,3)

x2 = (1,1)
y2 = (3,6)

# Compute intersection
xi, yi = line_intersection((x1, y1), (x2, y2))

# Visualize results
plt.figure()
plt.plot((x1[0], y1[0]), 
         (x1[1], y1[1]))
plt.plot((x2[0], y2[0]), 
         (x2[1], y2[1]))
plt.scatter(xi, yi, c='k', s=50)

enter image description here

Nathaniel
  • 3,230
  • 11
  • 18