intersection of two lines intuition I was reading a programming and found an exercise that asked for calculating the intersection of two lines, according to the following formula:


I know that the formula can be solved by using Cramer's rule, and I actually know how to program it, but the problem is I do not recall how that formula is derived mathematically. Maybe it sounds like a silly question, but does it has something to do with the conversion of a line to the following equation that includes the slope $y=mx+b$?
 A: In your picture, with $(x,y)$ being the desired intersection point, the segment from that point to $(x_1,y_1)$ has the same slope as the segment from $(x_2,y_2)$ to  $(x_1,y_1)$, because they're parts of the same line. So
$$
\frac{y-y_1}{x-x_1}=\frac{y_2-y_1}{x_2-x_1}.
$$
Clear fractions and rearrange terms to get the first of your two equations. The second is obtained the same way, using the other line in the picture.
A: Take the line through $(x_1,y_1)$ and $(x_2,y_2)$. If this line is non-vertical (i.e. $x_1 \ne x_2)$, then the slope is given by:
$$m = \frac{y_2-y_1}{x_2-x_1}$$
and the equation of the line becomes:
$$y-y_1=\frac{y_2-y_1}{x_2-x_1} \left(x-x_1\right)\tag{$*$}$$
You can rearrange this to the standard form $y=mx+b$ if you want.
Multiplying both sides of $(*)$ by $x_2-x_1$:
$$\left(y-y_1\right)\left(x_2-x_1\right)=\left(y_2-y_1\right)\left(x-x_1\right)$$
Rearranging a bit, this is equivalent to:
$$\left(y_1-y_2\right)x-\left(x_1-x_2\right)y=\left(y_1-y_2\right)x_1-\left(x_1-x_2\right)y_1 \tag{$\star$}$$
And this is exactly the (first) equation used in your given code.
The advantage of using the equation $(\star)$ over $(*)$ is that $(\star)$ also works for vertical lines so it is more general; that system will work for any lines through two sets of two (different) points.
