Home > Software engineering >  How to find where two lines intersect from the line equation?
How to find where two lines intersect from the line equation?

Time:09-30

Write the function areaWithinThreeLines(m1, b1, m2, b2, m3, b3) that takes six int or float values representing three lines:

y = m1 * x   b1
y = m2 * x   b2
y = m3 * x   b3

Since each pair of lines intersects in a point, these three lines have three points of intersection. Your function should return the area of the triangle formed by connecting these points. If no such triangle exists (if at least two of the lines are parallel), return None.

Hint: Use three helper functions. One to get the x coordinate where two lines intersect, or None if they do not intersect. You will call this function 3 times. Then write another helper function to find the area of a triangle given its side lengths using Heron's formula (you may have already written this in the previous section). You will also need to write distance(x1, y1, x2, y2) for a total of three helper functions.

CodePudding user response:

Consider the following equations
y = m1 * x b1 (1)
y = m2 * x b2 (2)

Then we can eliminate by taking (1)-(2)
0 = (m1-m2) * x (b1-b2) (1)-(2)

From here you can solve for x in your helper functions.

CodePudding user response:

You can use some basic algebra to solve the equations.

import numpy as np

m_list = [[m1, -1], [m2, -1], [m3, -1]]
A = np.array(m_list)
B = np.array([-b1, -b2, -b3])
X = np.linalg.inv(A).dot(B)
  • Related