要知道该点在哪个多边形中,可以使用“ 射线投射”算法。
为了找到最接近的边缘,您可以使用幼稚的方法来计算到每个边缘的距离,然后取最小值。只要记住检查与边的交点是否在边之外(请检查此)。如果在外面,则将其距离边缘的最近端点。
可以用一些伪代码更好地解释直觉:
dot(u,v) --> ((u).x * (v).x + (u).y * (v).y)norm(v) --> sqrt(dot(v,v)) // norm = length of vectordist(u,v)--> norm(u-v) // distance = norm of difference// Vector contains x and y// Point contains x and y// Segment contains P0 and P1 of type Point// Point = Point ± Vector// Vector = Point - Point// Vector = Scalar * VectorPoint closest_Point_in_Segment_to(Point P, Segment S){ Vector v = S.P1 - S.P0; Vector w = P - S.P0; double c1 = dot(w,v); if ( c1 <= 0 ) // the closest point is outside the segment and nearer to P0 return S.P0; double c2 = dot(v,v); if ( c2 <= c1 ) // the closest point is outside the segment and nearer to P1 return S.P1; double b = c1 / c2; Point Pb = S.P0 + b * v; return Pb;}[Point, Segment] get_closest_border_point_to(Point point, Polygon poly) { double bestDistance = MAX_DOUBLE; Segment bestSegment; Point bestPoint; foreach s in poly.segments { Point closestInS = closest_Point_in_Segment_to(point, s); double d = dist(point, closestInS); if (d < bestDistance) { bestDistance = d; bestSegment = s; bestPoint = closestInS; } } return [bestPoint, bestSegment];}
我认为这个伪代码应该可以帮助您,当然,一旦有了要插入的多边形!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)