Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend a line segment a specific distance

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.

like image 687
goodgulf Avatar asked Oct 12 '11 13:10

goodgulf


People also ask

How far can a line segment be extended?

Line Segment: A line segment is part of a line or a ray which has two distinct unique bounding end points. It can't be extended in either direction and it is of fixed length.

How do you extend a line in Python?

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. If necessary, you can add an extra pair of parentheses around an expression, but sometimes using a backslash looks better. Make sure to indent the continued line appropriately.

What is the distance of a line segment?

The distance between any two points is the length of the line segment joining the points. There is only one line passing through two points. So, the distance between two points can be calculated by finding the length of this line segment connecting the two points.


2 Answers

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; 
like image 135
saeedn Avatar answered Sep 23 '22 20:09

saeedn


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.

like image 34
cnicutar Avatar answered Sep 25 '22 20:09

cnicutar