Sign in to follow this  
erifash

Line Extention

Recommended Posts

I have a line segment with endpoints (A,B)(C,D) and I need to extend it P pixels from point (A,B) in the direction of point (C,D). I would like to find the resulting point. What would the algorithm be in order to do this? Thank you for your time.

Share this post


Link to post
Share on other sites
Assuming the coordinate plane is of a scale 1 unit to 1 pixel:

1. Find the vector between the two points.
2. Normalize vector.
3. Scale vector by 5.
4. Add vector to the first point.

Don't know about vectors? Check out this website.

Share this post


Link to post
Share on other sites
Assuming that the vectors you have supplied are specified in pixels, extrapolation/interpolation goes as follows:

Define a difference vector:

(X,Y) = (C-A,D-B)

This is now a [directional] vector from (A,B) to (C,D). To use it further, you'll want to normalise it: Determine the length (L) and divide through:

L = sqrt(X^2 + Y^2) // Pythagoras's Theorem
(X',Y') = (X/L,Y/L) // Normalised Vector

Now (X',Y') points in the same direction as (X,Y) (i.e. from (A,B) towards (C,D)) but it has length 1. In order to travel P pixels from (A,B) towards (possibly past) (C,D) calculate the resulting point:

(P,Q) = (A + P*X', B + P*Y')

Note that if P < L, (P,Q) will lie on the line segment,
if P = L, (P,Q) equals (C,D)
if P > L, (P,Q) is on the same line but lies beyond (C,D).

This is a reasonably common vector operation. If you plan on doing much more work with linear algebra, I recommend you get hold of a good book/tutorial so that these such calculations become second-nature.

Regards
Admiral

Edit: Too slow [sad]

Share this post


Link to post
Share on other sites
I was thinking about finding the slpoe then setting up a proportion or using pythagorean's theorem. The language I am using doesn't support vectors, so if you could explain it to me in equations that would be great! :)

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