我正在尝试找到一种将线段延长特定距离的方法。例如,如果我有一条从 10,10 开始延伸到 20,13 的线段,并且我想将长度延长 3,我该如何计算新端点。在这个例子中,我可以通过 sqrt(a^2 +b^2) 得到长度 10.44 所以如果我想知道 10,10 的新端点长度为 13.44 什么是计算上最快的方法?我也知道坡度,但不知道在这种情况下是否对我有帮助。
最佳答案
您可以通过找到线段的单位 vector 并将其缩放到您想要的长度,然后使用该 vector 平移线段的终点来实现。假设您的线段端点是 A
和 B
并且您想在端点 B
(和 lenAB
是线段的长度)。
#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;
关于c - 将线段延伸特定距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7740507/