Line Intersection

Started by
0 comments, last by Iftah 16 years, 10 months ago
In the source code with the book Tricks of the Windows Game Programming Gurus there is a function to clip lines to a predefined clipping rectangle. The function uses the following formula to get the Y co-ordinate where the line crosses the left hand side of the clipping rectangle... yIntercept = y1 + (clipX - x1) * (y2 - y1) / (x2 - x1); Where (x1, y1) and (x2, y2) are the end points of the line being clipped, and clipX is the X co-ordinate of the left hand side of the clipping rectangle (the line x=clipX). I recognise the last part of this formula as being one representation of the equation of a line: gradient m * (y2 - y1) / (x2 - x1). As such I assume the other part of the formula has something to do with the gradient of the line, however my maths is quite rusty and I don't understand exactly what it is doing and how. Can anyone explain this to me? Also while I'm on the subject, can anyone provide me with links to any articles or books that might help me to brush up my maths skills a bit? - I'm about to go searching through the articles section of this site to see what I can find there for now but if anyone can recommend anything from experience that would be awesome, thanks.
Progress is born from the opportunity to make mistakes.

My prize winning Connect 4 AI looks one move ahead, can you beat it? @nickstadb
Advertisement
you are a bit confused its (what you typed isn't even an equation...):
the equation for the gradient m is
m = (y2-y1)/(x2-x1)

that is, the ratio between change on Y to the change on X.
when you change some X the Y changes based on this ratio
(this ratio is the same all over the line)

so in other words:
Y = initial_y + m* delta_x
(delta_x = x2 - initial_x)

This topic is closed to new replies.

Advertisement