Thanks for really breaking it down for me guys, and especially thanks for posting that fla.

]]>and heres the corresponding swf for it: http://ngup.net/uploads/mif.swf (drag points)

]]>The point is, if you pick a t greater than 1 or less than zero, you are no longer finding a point that lies partway between the two. What luca is doing is finding two parametrically defined lines that define the two given line segments. In one line, the parameter is t, in the other it is s, he is then finding for what values of t and s these two lines intersect. If either of them are greater than 1 or less than zero, then you have to 'walk outside' the two line segments in order to reach the intersection point. Why? if t > 1, then we are talking about a point that lies further than the whole distance from (a,b) to (c,d), which means they do not intersect within those boundaries. Further, if t < 0, then t is negative, which means we are starting at (a,b) and walking backwards, away from (c,d) which certainly means the intersection point does not lie somewhere between (a,b) and (c,d).

In otherwords, he is defining two infinite lines, finding where they intersect, and then seeing if the lines fall within the boundaries of the line segments.

Does that make sense? Do you understand the vector notation luca is using?

]]>Am I missing something here? That still seems really confusing.

]]>so that you have removing the 0 index for easier reading

then you have the condition:

this isn't nice to work with, so lets look at it a different way:

as you move along the B ray, and take a vector from A[0] to B[s] the point at which the two rays intersect, is when this vector is parallel to V, or that its perpendicular vector is perpendicular to V: the reason for this is that to find if two vectors are parallel, you have to dot them, and check that the value of the dot product is plus-minus the product of the two magnitudes, whereas checking for perpendicular vectors, no matter what their magnitudes are, the dot product is 0 if they are perpendicular, so you don't need to work out their lengths

if we define a unary operator

which is just the same as having the vector transformed by a 90 degree rotation matrix, so all rules apply the same as multiplying with a matrixSo we have:

similarly working for t you get

note that the denominators are the same, so you can pre-compute the inverse of the denominator for faster execution time:

the only time this fails is if Q and V are parallel, in which case the denominator is 0, but in this case it is either that they never intersect (even going past limits of line segment) or that they are the same line segment

then you would do it like this:

```
calculate inverse of denominator
calculate s
if (s < 0 || s > 1) return false
calculate t
return (t >= 0 && t<=1)
```

so with this you can find if they intersect, and if they do intersect, use either the value for s or t to calculate the exact intersection point

]]>I know I can do it, but as I go along things get more and more complicated (which means: more room for bugs) and I keep thinking there's got to be and easier way of doing it.

So I googled it, and came across this essay on a "sweep line algorithm" but that seems even more complicated then the way I was doing it.

Is there an easier way to do it? I know Actionscript isn't a very popular programming language (unless you're into game design) so if someone can just give me the theory, or maybe an example in C++ (that's the only other language I know) I would really appreciate it.

I really feel like I'm missing something here, this should be real easy, and yet...

]]>