Sign in to follow this  

Line Intersection

Recommended Posts

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.

Share this post

Link to post
Share on other sites
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)

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this