线段上的点距一点的距离

发布于 2024-10-03 17:04:12 字数 363 浏览 4 评论 0原文

我正在寻找一种算法(或算法的名称),该算法将在线段上找到一个点,如果存在这样的点,则该点与不在线段上的另一个点相距一定的给定距离。

即存在三个点ABC;可能还有第四个D。其中AB构成一条线段,点C是线段AB之外的另一个点。找到一个点 D(如果存在),该点出现在线段 AB 上,距离点 给定距离 distance >C

I am looking for an algorithm (or the name of the algorithm) that will find a point on a line-segment, if such a point exists, that is some given distance away from another point that not on the line-segment.

i.e., There exist, three points A, B, C; and possibly a fourth D. Where AB makes up a line-segment, and point C is another point somewhere OFF of the line-segment AB. Find a point D, if such point exists, that appears on the line-segment AB that is a given distance distance away from point C.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦上情书 2024-10-10 17:04:12

看这里: 圆线交点

C 是圆的中间distance 是半径。

请注意,可能有两个结果点,您必须检查该点是否实际上位于您的直线上(或者位于通过扩展它得到的直线上)。

Look here: Circle-Line Intersection

C is the circles middle and distance is the radius.

Note that there may be two resulting points and that you have to check whether the point is actually on your line (or on the line that you would get by extending it).

独自←快乐 2024-10-10 17:04:12

我花了太长时间来解决这个问题,似乎无法在任何地方找到简单的答案,所以我想我应该将其发布在这里并为一些人节省很多时间。尽管最初的帖子很旧,但我确实希望有人很久以前就发布了一个简单的答案。这本来可以节省我几天的实验时间。

public static Point PointFromEndOfLine(Point start, Point end, double distance)
{
    double x = end.X-start.X;   
    double y = end.Y-start.Y;
    double z = Math.Sqrt(x * x + y * y);  //Pathagrean Theorum for Hypotenuse
    double ratio = distance / z;
    double deltaX = x * ratio;
    double deltaY = y * ratio;

    return new Point(end.X-deltaX, end.Y-deltaY);
}

上面的函数需要一个起点 (x,y) 和一个终点 (x,y) 以及一个距离(距终点的距离)。如果您的距离为负,则返回的点将沿着同一条线超出终点。如果您的距离是大于 startPoint 和 endPoint 之间的距离,返回点将在起点之前,但仍在同一条线上。如果您的距离是正数并且小于 startPoint 和 endPoint 之间的距离,则返回点将在起点上。距离端点“距离”处的起点和终点之间的线段。

它起作用的原因是“相似三角形”,通过该三角形绘制一条与 X 轴和 x、y、z 平行的直线。你的大三角形和你画的线创建的小三角形的值将彼此成比例

:x/X == y/Y == z/Z

希望这可以帮助别人。

I spent WAY too long figuring this out and couldn't seem to find a simple answer anywhere, so I figured I'd post it here and save some people a lot of time. Even though the original posting is old, I sure wish somebody had posted a simple answer long ago. It would have saved me a couple days of experimenting.

public static Point PointFromEndOfLine(Point start, Point end, double distance)
{
    double x = end.X-start.X;   
    double y = end.Y-start.Y;
    double z = Math.Sqrt(x * x + y * y);  //Pathagrean Theorum for Hypotenuse
    double ratio = distance / z;
    double deltaX = x * ratio;
    double deltaY = y * ratio;

    return new Point(end.X-deltaX, end.Y-deltaY);
}

The function above takes a startPoint (x,y) and an endPoint (x,y) and a distance (from the endpoint. If your distance is negative, the returned point will be beyond the endPoint along the same line. If your distance is greater than the distance between startPoint and endPoint, the return point will be before your startpoint, but still on the same line. If your distance is a positive number and is less than the distance between startPoint and endPoint, the point returned will be on the line segment between the startPoint and endPoint at 'distance' from the endpoint.

The reason it works is 'Similar Triangles'. Imagine a large right triangle. Draw a strait line through the triangle parallel to the X axis and the x,y,z values of your big triangle and the smaller one created by the line you drew will be proportional to each other.

In other words: x/X == y/Y == z/Z

Hope this helps somebody out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文