Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 895
braden.groom Avatar asked Jun 28 '12 22:06

braden.groom


2 Answers

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.

like image 177
Aesthete Avatar answered Nov 18 '22 10:11

Aesthete


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
like image 30
mathematician1975 Avatar answered Nov 18 '22 10:11

mathematician1975