Well, originally you were asking how the line intersection worked, so I think I'll explain Kaesebrot's algorithm (and point out the mistake in it). Also, I'm not sure if your solution (swapping points) will always return the correct result.
First of all, we know that if two lines are parallel, then they either intersect on all points (they are the same line) or don't intersect at all. In order to check if two lines are parallel you only need to check if their slopes are parallel. The slope of a line is defined to be the difference between y-values of two points on the line divided by the difference between the x-values of those points. Using this definition you'll see:
Line1: from A(a0/a1) to B(b0/b1)
Line2: from C(c0/c1) to D(d0/d1)
l1 slope: (b1 - a1) / (b0 - a0)
l2 slope: (d1 - c1) / (d0 - c0)
To check if the lines are parallel, set the slopes equal to each other:
(b1 - a1) / (b0 - a0) = (d1 - c1) / (d0 - c0)
(b1 - a1)(d0 - c0) = (b0 - c0)(d1 - c1)
(b1 - a1)(d0 - c0) - (b0 - c0)(d1 - c1) = 0
Note: This is where Kaesebrot's mistake was.
So if we let g = (b1 - a1)(d0 - c0) - (b0 - c0)(d1 - c1) then if g = 0 the lines are parallel and we don't need to do anything else. We don't worry about the case where the lines are the same because you can't return a single point of intersection since there are multiple points. Note: There is one case where there exists a valid intersection; this is when only the endpoints of the two lines are the same, if it is important then you can check for this special case.
Once we have determined the lines aren't parallel we need to find the point of intersection. One way of writing the general form of a line is:
x = x1 + t(x2 - x1)
y = y1 + t(y2 - y1)
Where two points on the line are (x1, y1), (x2 y2) and t is a real number. Since we're only interested in values of x between x1 and x2, t must be between 0 and 1 (the same argument applies with the y values). Using this definition we can say:
l1: x = a0 + t(b0 - a0) l2: x = c0 + s(d0 - c0) y = a1 + t(b1 - a1) y = c1 + s(d1 - c1)
The point of intersection between these two lines will occur where the x and y values of the two lines are equal.
ie.
a0 + t(b0 - a0) = c0 + s(d0 - c1)
a1 + t(b1 - a1) = c1 + s(d1 - c1)
Isolating t, we get:
t = ((c0 - a0) + s(d0 - c0)) / (b0 - a0) = (c1 - a1) + s(d1 - c1) / (b1 - a1)
Isolating s, we get:
s = ((b0 - a0)(c1 - a1) - (b1 - a1)(c0 - a0)) / ((b1 - a1)(d0 - c0) - (b0 - a0)(d1 - c1))
Note: The denominator here equals g, which we used earlier to check if the lines are parallel.
But we know that for all points on l2 0 <= s <= 1, so if s is not in this range then the lines don't intersect. If s is in this range we can just sub s back into the equation for l2 and solve for x and y.
Translating this into code:
g = (b1 - a1) * (d0 - c0) - (b0 - c0) * (d1 - c1);if (g == 0){// Lines don't intersect}s = ((b0 - a0) * (c1 - a1) - (b1 - a1) * (c0 - a0)) / g;if (s < 0 || s > 1){// Lines don't intersect}p0 = c0 + s * (d0 - c0)p1 = c1 + s * (d1 - c1)
Hopefully this will show you what's going on behind the algorithm.
Give a man a fish and you feed him for a day; teach him to use the Net and he won't bother you for weeks.
[edited by - thooot on June 3, 2002 10:08:32 AM]