# Math wizardry

James Steiner gregortroll at gmail.com
Fri Mar 4 16:45:58 EST 2005

```> To find out whether the
> intersection point lies between the end points of each line you will
> need to test whether the distance between the intersection point and
> ALL the end points is less than the length  of each line
> respectively. (I can't imagine that sentence is clear.)

That's not required, which is good, because the distance function is costly.

First, lets assume the purpose of the function is not only to return
the intersection of the two *lines* but rather to return the
intersection of the two *line segments*, and returns a special value
if the *line segments* do not intersect.

Then, given the three points on a line (and we know that the
calculated intersection point is on the lines, if Ax is between Bx and
Cx and Ay is between By and Cy, then point A must be between points B
and C.

Also, as a side note, sometimes, like when comparing distances, it's
OK to use a partial calculation, rather than the complete distance
calculation. For example, if distance A is equal to distance B, then
it is true that A^2 is equal to B^2. So, rather that performing the
full distance calculation: sqrt ( ( x1 - x2) + (y1 - y2) ) , you can
leave off the sqrt, and compare the partial result, which is really
the sqaure of the distance, saving some calculation time (sqrt is a
much more expensive function than multiplication). In applications
(like games or physics-based simulations) where many of these
calculations are occuring every second, the saved time can be used for
other things.

Hope this helps,
~~James.
```