简体   繁体   English

是否在多边形内找到位置C#

[英]location is found within a polygon or not c#

I'm looking for a perfect algorithm in C# which determines whether a location (lat/long) is found within a polygon or not. 我正在C#中寻找一种完美的算法,该算法确定是否在多边形内找到位置(经/纬度)。

Over the Google map I have created different zones in shape of a polygon. 在Google地图上,我创建了多边形的不同区域。 Each zone can be represented by set of coordinates. 每个区域可以由一组坐标表示。 For instance below are the Coordinates for Montego bay (Zone A), Jamaica (longitude,latitude combination): 例如,以下是牙买加蒙特哥湾(A区)的坐标(经度,纬度组合):

 -77.9531479,18.4565699,-77.9482339,18.4579542,-77.9443393,18.4615874,-77.9399726,18.4627373,-77.933943,18.4653526,-77.931272,18.4652763,-77.9282138,18.46578,-77.9267121,18.4645891,-77.924619,18.4625118,-77.923131,18.4689837,-77.924646,18.4727285,-77.924112,18.4763919,-77.9255882,18.4793735,-77.927573,18.4833522,-77.9234638,18.4888979,-77.92363,18.492311,-77.914973,18.4951459,-77.918355,18.4971759,-77.972733,18.4985953,-77.916291,18.527569,-77.8997725,18.56178,-77.8962851,18.58578,-77.873843,18.517512,-77.8772736,18.5228297,-77.9253387,18.521568,-77.9531479,18.4565699 

Now lets say we need to find out if Sunset beach Hotels, Montego bay, jamaica whose coordinates are (long/lat): -95.030710, 29.148650 belongs to Zone A or not? 现在让我们说我们需要找出坐标为(长/纬):-95.030710、29.148650的牙买加蒙特哥湾日落海滩酒店是否属于A区?

I have tested so many algorithms available around but none of them is working perfectly. 我已经测试了很多可用的算法,但是没有一个能完美地工作。 It works for some of the hotels, but not for all. 它适用于某些酒店,但不适用于所有酒店。

Any help would be appriciated. 任何帮助将被申请。

I've had success using the Winding Number: http://geomalgorithms.com/a03-_inclusion.html 我已经成功使用了绕线号码: http : //geomalgorithms.com/a03-_inclusion.html

Step 1: Define the direction the polygon was drawn. 步骤1:定义多边形的绘制方向。

Fundamentally, the polygon is static, and does not have a direction. 从根本上讲,多边形是静态的,没有方向。 However, consider the movements you perform when drawing a polygon on a piece of paper. 但是,请考虑在一张纸上绘制多边形时执行的移动。 You begin at one position, and move your pen to draw a line for each side of the polygon. 您从一个位置开始,然后移动笔为多边形的每一侧画一条线。 If you do not take your pen off the paper, each side of the polygon will either be all drawn in a clockwise direction, or all drawn in a counter-clockwise direction. 如果不将笔从纸上取下,则多边形的每一边将全部沿顺时针方向绘制,或全部沿逆时针方向绘制。

We can use this 'direction' to determine how the sides of the polygon 'wind' around the point (clockwise winding or counter-clockwise winding). 我们可以使用此“方向”来确定多边形的边如何围绕点(顺时针缠绕或逆时针缠绕)缠绕。

With the real polygon in question (which has not been drawn in a direction), it is not important which direction you consider it to have been drawn, but it is important that each side is considered to be drawn in the same direction. 对于实际的多边形(尚未在一个方向上绘制),考虑要绘制到哪个方向并不重要,但是将每一边都视为在同一方向上绘制很重要。

Step 2: Consider each side of the polygon as an individual object with two vertices. 步骤2:将多边形的每一边视为具有两个顶点的单个对象。

If we know the vertices of the polygon, we can determine the endpoints of each side of the polygon. 如果我们知道多边形的顶点,则可以确定多边形每一侧的端点。

Step 3: Determine the direction that each side crosses the point (if the sides do cross the point). 步骤3:确定每边与该点相交的方向(如果两边确实与该点相交)。

Example using clockwise winding to find whether a polygon surrounds a point : 使用顺时针缠绕查找多边形是否围绕point示例:

Define a counter to keep track of winding number. 定义一个counter来跟踪绕组编号。

For each side of the polygon, take the first vertex vertex1 and second vertex vertex2 (these points define the end of each side). 对于多边形的每一边,取第一顶点vertex1和第二顶点vertex2 (这些点定义了每一边的末端)。 If vertex1.x < point.x then side begins to the left of point.x . 如果vertex1.x < point.x则side开始于point.x的左侧。 If vertex2.x > point.x then side ends to the right of point.x . 如果vertex2.x > point.x则侧面在point.x的右侧结束。 If the side begins to the left AND ends to the right, the line crosses the point. 如果边开始于左侧,而结束于右侧,则线与该点交叉。

If the side crosses the point , we need to determine in which direction it crosses. 如果该边与该point相交,则需要确定它与该point相交的方向。 Using a clockwise as positive direction, the side must cross above the point to be considered positive. 使用顺时针方向作为正方向,该边必须在被视为正的point上方交叉。 The line crosses above the point if the y value of the line at the point 'sx position is more than the point 'sy value. 如果线在point “ sx” point的y值大于point “ sy”值,则线越过该point Else it crosses below the point. 否则,它越过该点。

If it crosses above, increment the counter . 如果超过,则增加counter If it crosses below, decrement the counter . 如果它越过下方,则使counter递减。

NOTE: Notice that if the line is passing from left to right, then positive direction sees the line pass above the point, but if the line goes from right to left (wrapping back under the point) then positive direction sees the line pass below the point. 注意:请注意,如果直线从左向右穿过,则正方向会看到该线在该点上方通过,但是,如果直线从右向左(绕回该点下方),则正方向会看到该线通过下方点。 Positive direction is clockwise. 正方向为顺时针方向。

Now do the same thing with the next side of the polygon, where vertex1 would be the previous side's vertex2 , and the new vertex2 is the next corner of the polygon when travelling in a clockwise direction. 现在,对多边形的下一个面执行相同的操作,其中vertex1将是前一个面的vertex2 ,而新的vertex2是沿顺时针方向移动时多边形的下一个角。

Do this for all sides of the polygon. 对多边形的所有面都执行此操作。

At the end, you will have a counter with either a positive, negative, or 0 value. 最后,您将拥有一个带有正,负或0值的计数器。

If the value of the counter is 0, the polygon does not contain the point, else, it does. 如果计数器的值为0,则多边形不包含点,否则包含点。

This is because, a polygon which contains a point will always have an even number of its sides pass the point. 这是因为包含点的多边形将始终具有偶数个通过该点的边。 Either 2 sides, or 4 sides, etc. Of these sides, half will pass from left to right above the point, and half will pass from right to left below the point. 可以是2面,也可以是4面,等等。在这些面中,一半将从该点上方从左到右,而另一半将从该点下方从右到左。

A result of 0 means either that no sides pass the point (therefore the polygon never crosses the axis of the point), or the polygon passes on one side of the point in two directions (eg above the point left to right, then above the point right to left). 结果为0表示没有任何边通过该点(因此多边形永远不会穿过该点的轴),或者该多边形沿两个方向在该点的一侧经过(例如,从左到右,然后在该点的上方)从右指向左)。 Therefore the winding numbers cancel each other out. 因此,绕组数彼此抵消。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM