The code tries to find the intersection point of two segments - AB and CD.
There are many ways to explain how this is done, depending on how you interpret these operations.
Let's say point A has coordinates (xa, ya), B has (xb, yb), etc. Let them talk
dxAB = xb - xa dyAB = yb - ya dxCD = xd - xc dyCD = yd - yc
The following system of two linear equations
| dxAB dxCD | | t | | xc-xa | | | * | | = | | | dyAB dyCD | | u | | yc-ya |
if solved for t and u , it will give you the proportional position of the intersection point on line AB (value t ) and on linear CD (value u ). These values ββwill be in the range [0, 1] if the point belongs to the corresponding segment and outside this range, if the point lies outside the segment (on the line containing the segment).
To solve this system of linear equations, we can use the well-known Cramer rule. For this we need a qualifier
| dxAB dxCD | | | | dyAB dyCD |
which exactly matches the determinant(b - a, c - d) from your code. (Actually, I have determinant(b - a, d - c) , but this is not very important for the purpose of this explanation. For the code that you swap for C and D for some reason, see the PS note below).
And we will need a determinant
| xc-xa dxCD | | | | yc-ya dyCD |
which is exactly determinant(ca,cd) from your code and qualifier
| dxAB xc-xa | | | | dyAB yc-ya |
which is exactly equal to determinant(ba,ca) .
Separation of these determinants in accordance with the Cramer rule will give us the values ββof t and u , which is what is done in the code you published.
The code then checks the values ββof t and u to check if the segments really intersect, i.e. whether t and u correspond to the range [0, 1] . And if they do, he calculates the actual intersection point by estimating a*t+b*(1-t) (equivalently, he could estimate c*u+d*(1-u) ). (Again, see Note PS below).
PS In the source code, points D and C are "replaced" in the sense that the code has c - d , where I have d - c in my explanation. But this does not matter for the general idea of ββthe algorithm, if only be careful with the attributes.
This exchange of points C and D is also the reason for the expression a*(1-t)+t*b , used in evaluating the intersection point. Usually, as in my explanation, you expected to see something like a*t+b*(1-t) . (I doubt it, but I expected to see a*t+b*(1-t) even in your version. There may be a mistake.)
PPS The author, if the code forgot to check det == 0 (or very close to 0), which will happen in the case of parallel segments.