捕捉点最近的线 [英] Snap point to nearest line

查看:186
本文介绍了捕捉点最近的线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有每一行的起点和终点。每一行只能垂直或水平。

I have a start and end point of every line. Every line can be only vertical or horizontal.

例如:

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

Point = (6, 6)

NearestPointOnLine = (4, 6) // magic here

我如何计算上线的最近点的任何一点?

How do I calculate the closest point on the line for any point?

推荐答案

从迭代段段,每个段,找到最近点从该段点(A,B)

Iterating from segment to segment, for each segment, find the closest point from this segment to the point (a,b)

有三种情况:

  • 水平线(X1,y)提供到(×2,y)和点(A,B)已X1&所述; = A LT = X2,所以最近点为(a,y)的

  • Horizontal line (x1, y) to (x2, y) and point (a,b) has x1 <= a <= x2, so closest point is (a,y)

垂直线(X,Y1)到(x,Y2)和点(A,B)具有Y1&其中; = b将= Y2,所以最近点为(x,B)

Vertical line (x, y1) to (x, y2) and point (a,b) has y1 <= b <= y2, so closest point is (x,b)

没有这两个以上的情况下,在这种情况下,最近点可以是在开始结束点段。你可以很容易地确定通过计算距离(A,B)开始和结束。

None of these two above cases,in this case, the closest point is either the start or the end point of the segment. You can easily determine by calculate the distance from (a,b) to start and end.

让所有的点之后,只返回它具有最短的距离与其他人相比的地步。公式来计算点的距离(A,B),以点(C,D):

After getting all the point, just return the point which has the shortest distance compared with others. Formula to calculate distance from point (a,b) to point(c,d):

int x = a - c;
int y = b - d;
double dist = sqrt(x*x + y*y);

这篇关于捕捉点最近的线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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