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;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…