将线段延长特定距离 [英] Extend a line segment a specific distance

查看:23
本文介绍了将线段延长特定距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种将线段延长特定距离的方法.例如,如果我有一条从 10,10 开始延伸到 20,13 的线段,并且我想将长度延长 3 倍,我该如何计算新端点.在这个例子中,我可以通过 sqrt(a^2 +b^2) 获得长度 10.44 所以如果我想知道长度为 13.44 的 10,10 的新端点,那么计算上最快的方法是什么?我也知道斜率,但不知道在这种情况下是否对我有帮助.

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.

推荐答案

你可以通过找到你的线段的单位向量并将它缩放到你想要的长度,然后用这个向量平移你的线段的端点.假设您的线段端点是 AB 并且您想在端点 B (和 lenAB 是线段的长度).

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;

这篇关于将线段延长特定距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆