38

I am trying to find a way to extend a line segment by a specific distance. For example if I have a line segment starting at 10,10 extending to 20,13 and I want to extend the length by by 3 how do I compute the new endpoint. I can get the length by sqrt(a^2 +b^2) in this example 10.44 so if I wanted to know the new endpoint from 10,10 with a length of 13.44 what would be computationally the fastest way? I also know the slope but don't know if that helps me any in this case.

Mark
  • 394
  • 5
  • 18
goodgulf
  • 403
  • 1
  • 4
  • 5
  • 1
    This is not a programming question, but simple math, which you then have to expand to your code. – Constantinius Oct 12 '11 at 13:08
  • 8
    @Constantinius It's still an algorithm question, just one based in math (which computer science is extremely heavy in). – corsiKa Oct 12 '11 at 13:30

3 Answers3

73

You can do it by finding unit vector of your line segment and scale it to your desired length, then translating end-point of your line segment with this vector. Assume your line segment end points are A and B and you want to extend after end-point B (and lenAB is length of line segment).

#include <math.h> // Needed for pow and sqrt.
struct Point
{
    double x;
    double y;
}

...

struct Point A, B, C;
double lenAB;

...

lenAB = sqrt(pow(A.x - B.x, 2.0) + pow(A.y - B.y, 2.0));
C.x = B.x + (B.x - A.x) / lenAB * length;
C.y = B.y + (B.y - A.y) / lenAB * length;
Joakim
  • 11,468
  • 9
  • 44
  • 50
saeedn
  • 3,288
  • 1
  • 21
  • 20
11

If you already have the slope you can compute the new point:

x = old_x + length * cos(alpha);
y = old_y + length * sin(alpha);

I haven't done this in a while so take it with a grain of salt.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • 7
    where alpha = atan2(y-old_y, x-old_x) – andrew cooke Oct 12 '11 at 14:32
  • Thank you for your help, this solution seems a little slower then the lower solution. I appreciate the help, this worked also. – goodgulf Oct 12 '11 at 18:46
  • this didn't really work for me, the new point was never parallel to the 2 given line points. so it was not a straight line anymore. could be a mistake on my side, maybe doublecheck – Peter Pohlmann Feb 28 '20 at 20:23
6

I just stumbled upon this after searching for this myself, and to give you an out-of-the-box solution, you can have a look at the code inside a standard Vector class (in any language) and cherry pick what parts you need, but I ended up using one and the code looks like this :

vector.set(x,y);
vector.normalize();
vector.multiply(10000);// scale it by the amount that you want

Good luck !

  • I think this is the most elegant answer – John Mott Apr 09 '17 at 15:51
  • Appreciate this answer. Learning vectors (linear algebra) and reaching for that math tool regularly to solve geometry problems is good advice. You will indeed be able to write elegant code. If you work on a team, bringing the whole team’s knowledge up in this area will make the shared code better. – cervezas Aug 25 '22 at 13:12