# 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.

## 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 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 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

##### 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 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!

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 9
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632922
• Total Posts
3009218
• ### Who's Online (See full list)

There are no registered users currently online

×