gpt4 book ai didi

algorithm - 捕捉点到最近的线

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:33:38 26 4
gpt4 key购买 nike

我有每条线的起点和终点。每条线只能是垂直或水平的。

例子:

enter image description here

Lines = [
((1, 1), (1, 7)), // (start, end)
((1, 1), (7, 1)),
((4, 1), (4, 7))
]

Point = (6, 6)

NearestPointOnLine = (4, 6) // magic here

如何计算任何点在直线上的最近点?

最佳答案

这是一种方式。它不关心行的开始和结束,但您可以添加它以适应。应该让你开始。

注意(line.xline.yline 上的任意点,起点或终点)

var closestLine;
var closestDistance = maxvalue;
foreach line
{
var distance = line.isHorizontal? line.x - point.x : line.y - point.y;
distance = Math.Abs(distance);
if (distance<closestDistance)
{
closestDistance = distance;
closestLine = line;
}
}
var snapX = closestLine.isHorizontal ? point.x : closestLine.x;
var snapY = closestLine.isHorizontal ? closestLine.y : point.y;

关于algorithm - 捕捉点到最近的线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24361080/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com