Jump to content
  • Advertisement
Sign in to follow this  
erifash

Line Extention

This topic is 4420 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
As I was posting my last post you replied! That was exactly what I was thinking and I believe it should work. Thanks!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!