5

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!

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
braden.groom
  • 305
  • 2
  • 5
  • 16
  • Which direction will the line be projected if `distance` is positive? negative? – justderb Jun 28 '12 at 22:14
  • This translation is ambiguous without another direction. There are an infinite number of lines that are distance `d` away from and parallel to the line. – cheeken Jun 29 '12 at 04:35
  • 2
    I want it to be a line segment of the same length and parallel to the original. and If you were to draw a line between the old and new points, it should form a perpendicular line with the original line. Im not sure if that is clear. I want the line seen in the image though. – braden.groom Jun 29 '12 at 04:58

2 Answers2

3

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.

Community
  • 1
  • 1
Aesthete
  • 18,622
  • 6
  • 36
  • 45
2

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
mathematician1975
  • 21,161
  • 6
  • 59
  • 101