equation of a line for a computer program

240 Views Asked by At

I am writing a computer program which draws the altitudes of triangles and where they intersect. I can use ${y = mx + c}$ to find the equations of two lines and then solve them using simultaneous equations with pen and paper but with code, this is slightly more contrived.

I want to get the equations of a two lines into a 3 x 3 matrix so that I can solve them in code so I need to find the ${y, mx}$ and ${c}$ parts. I found this stackoverflow post where it states:

If you start with

${y-y_1 = (y_2-y_1)\div(x_2-x_1) * (x-x_1)}$

Which is really ${(y_2 - Y_1) = m(x_2 - x_1)}$

through some manipulation you can get

${(y_1-y_2) * x + (x_2-x_1) * y + (x_1-x_2)*y_1 + (y_2-y_1)*x_1 = 0}$

Is this just rearranging the equation to equal 0?

And

${a = y1-y2}$

${b = x_2-x_1}$

${c = (x_1-x_2)*y_1 + (y_2-y_1)*x_1}$

What are a, b and c with respect to the equation of a line? Is c the y-intercept?

1

There are 1 best solutions below

0
On BEST ANSWER

The $y=mx+c$ form is not very good, because it doesn't handle the case where the line is vertical. A better form is $ax+by+c=0$ or $ax +by = c$.

Given two lines in this form, it's easy to find out where they intersect. If the two lines are $$ \begin{align} a_1x +b_1y = c_1 \\ a_2x +b_2y = c_2 \end{align} $$ then you can find the point of intersection by using Cramer's rule. The code is:

d = 1/(a1*b2 - a2*b1)
x = d*(c1*b2 - c2*b1)
y = d*(a1*c2 - a2*c1)

If $a1*b2 - a2*b1 = 0$, this won't work, of course. That's because the two lines are parallel.