braden.groom
braden.groom

Reputation: 325

Function to translate end coordinates of a line

I have two points at the end of a line.
I need to get the end coordinates of a translation of this line.
The translation will be a parallel line that is a distance d from the original line.
here is an image of what i need:

enter image description here

So I need a function that I can pass these two points and the distance and get the two new coordinates in returns.
I have been stuck on this problem for a while. Any help will be appreciated!
Thanks you!

Upvotes: 5

Views: 1838

Answers (2)

Aesthete
Aesthete

Reputation: 18848

The new co-ordinates will be the resulting vector of

distance d multiplied by normalized vector of which direction it's moving, added to the original vector point.

EDIT:

Given the two points of the line, you will need to calculate the normal of the vector joining these points. Information on that is here..

Normalise this vector, multiply by d, add to each point.

Upvotes: 3

mathematician1975
mathematician1975

Reputation: 21351

Calculate the vector (x2-x1,y2-y1). This is a vector in the direction of your line. A normal vector is then given by

    (-(y2-y1),-(x2-x1)) = (y1-y2,x1-x2). 

Divide this vector by its size to get the unit vector in the direction you want

    A = (y1-y2,x1-x2)/|(y1-y2,x1-x2)|

Now given your distance d your translated point will be given by

    NewPoint = OldPoint + d * A

Upvotes: 2

Related Questions