The book Graphics Gems III has the following algorithm:
You have four points: P1, P2, P3, P4.
There's a line segment between P1 and P2.
There's a line segment between P3 and P4.
Their point of intersection P* is described as:
P* = P1 + alpha * (P2 - P1)
P* = P3 + beta * (P4 - P3)
Where alpha and beta are unknown.
The line segments intersect only if alpha is in [0, 1] and beta is in [0, 1].
The book then describes its derivation for alpha and beta:
P* = P1 + alpha * (P2 - P1)
P* = P3 + beta * (P4 - P3)
Subtracting the above equations gives:
0 = (P1 - P3) + alpha * (P2 - P1) + beta * (P3 - P4)
For the sake of simplicity:
Let A = P2 - P1
Let B = P3 - P4
Let C = P1 - P3
So the equation above further simplifies to:
0 = C + alpha * A + beta * B
But all of a sudden, the book states:
alpha = (B.y * C.x - B.x * C.y) / (A.y * B.x - A.x * B.y)
beta = (A.x * C.y - A.y * C.x) / (A.y * B.x - A.x * B.y)
How'd they get that? There's only one equation of two unknowns. I thought you needed two equations to solve for two unknowns.
Given a line in 2-D, you can denote any point on that line using a function of one variable. That's what they're doing in the first equations (of $P*$). For example: line $A$ has endpoints $P_1$ and $P_2$. Then you have
$$f(\alpha) = P_1 + \alpha (P_2 - P_1)$$
which gives you every point on that line. So if you evaluate it using $\alpha=0$, you get $f(0)=P_1$. Similarly, with $\alpha=1$ you get $f(1)=P_1+P_2-P_1=P_2$. So you can see why the lines intersect if $\alpha$ is in the range $[0,1]$, because the intersection of the lines is somewhere on the line, as opposed to off the edge.
Using the parametrized equations for both lines, you can find that $C+\alpha A+\beta B=0$.
Now, each of $A,B,C$ are vectors, so you actually get two equations:
$$\begin{array}{c} \vec{C} + \alpha \vec{A} + \beta \vec{B} = \vec{0} \\ \iff Cx + \alpha Ax + \beta Bx = 0 \text{ and } Cy + \alpha Ay + \beta By = 0 \end{array}$$
So solve (for example) the first equation for $\beta$:
$$ \beta = \frac{-Cx-\alpha Ax}{Bx} $$
And then stick that into the second equation and solve for $\alpha$:
$$ \begin{aligned} Cy + \alpha Ay + By \left(\frac{-Cx-\alpha Ax}{Bx}\right) &= 0 \\ \frac{Cy Bx}{Bx} + \alpha \left( \frac{ Ay Bx}{Bx} \right) - \frac{Cx By}{Bx} - \alpha \left( \frac{Ax By}{Bx} \right) &= 0\\ \alpha \left( \frac{Ay Bx}{Bx} - \frac{Ax By}{Bx}\right) &= -\frac{Cy Bx}{Bx} + \frac{Cx By}{Bx} \\ \alpha \left( \frac{Ay Bx - Ax By}{Bx} \right) &= \frac{Cx By - Cy Bx}{Bx} \\ \alpha &= \frac{Cx By - Cy Bx}{Ay Bx - Ax By} \end{aligned} $$
You can do the same to solve for $\beta$.