确定坐标是否存在于多边形内部

发布于 2024-08-17 14:00:22 字数 335 浏览 2 评论 0原文

我正在开发一个开源跟踪和地理围栏软件应用程序,但在计算地理围栏的数学时遇到了一些困难。

我需要确定多边形内部是否存在坐标。然而,棘手的部分是多边形没有固定的边数。我需要能够计算五十面或五面。

我的研究表明,最简单的方法是采用我的点(我将其称为 x)和多边形外部的点(称为 y)并确定线 ((xx, xy), (yx, yy)) 是否与多边形的边界。如果相交奇数次,则点 x 必须在多边形内部。

然而,我知道如何在算法中表达这一点。显然,我需要遍历构建多边形的各种线条,但我所做的检查却无法实现。有人可以帮忙吗?请知道我不一定要求解决方案。任何能帮助我找出答案的事情都是巨大的帮助。

非常感谢。

I'm working on an open source tracking and geofence software application and am having a bit of difficulty figuring out the math for the geofencing.

I need to determine whether or not a coordinate exists inside of a polygon. However, the tricky part is that the polygon has no set number of sides. I need to be able to calculate for fifty sides or for five sides.

My research says that the easiest way is to take my point (which I'll call x) and a point outside the polygon (call it y) and determine if line ((xx, xy), (yx, yy)) intersects with the polygon's boundaries. If it intersects an odd number of times, point x must be inside the polygon.

Knowing that, however, I cannot figure out how to express this in an algorithm.. I obviously will need to loop through the various lines constructing the polygon but the check I do eludes me. Can anyone be of assistance? Please know that I'm not asking for the solution necessarily. Anything that will help me figure it out the answer is an enormous help.

Much appreciated.

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

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

发布评论

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

评论(6

云朵有点甜 2024-08-24 14:00:22

请参阅此处基本上

有一种方法(我认为它是乔丹曲线定理)可以计算射线与构成多边形的线段相交的次数。如果结果是偶数,则该点位于多边形外部,否则该点位于多边形内部。

HTH

编辑
还有另一个与此问题相关的问题可以找到 这里

See here

Basically there is an approach (I think its the Jordan Curve Theorem) that counts the number of times a ray intersects the line segments making up the polygon. If the result is even then the point is outside the polygon otherwise the point lies inside the polygon.

HTH

EDIT
There is another SO question that relates to this question that can be found here

紫瑟鸿黎 2024-08-24 14:00:22

这里的关键是要意识到您可以自由选择您喜欢的任何 Y 点。一个非常好的选择是点 (xx, -infinity)。换句话说,该点是从所讨论的点直接向下且无限远的点。现在的问题是:有多少多边形边与相关点下方的 X 坐标相交。因此只需要考虑跨越 X 坐标的线段。

如果点为 P = (x,y),线段端点为 P1 = (x1,y1) 且 P2 = (x2,y2),则与 x 相交的线段的 y 坐标由下式给出: sy = (x- x1)*(y2-y1)/(x2-x1) + y1

检查是否 sy < y(仅当 x1 < x < x2 或 x2 < x < x1 时)。如果其中有奇数个,则 P 位于内部。

当多边形的一个顶点与相关点的 y 位置完全相同时,就会出现一些微妙的问题。对于这种情况你必须要小心。

One key here is to realize that you are free to choose any point Y that you like. A really nice choice is the point (xx, -infinity). In other words, the point directly down from the point in question and infinitely far away. Now the question becomes: how many of the polygon edges cross your X-coordinate below the point in question. So only line segments that straddle the X coordinate need to be considered.

If your point is P = (x,y), and segment end points are P1 = (x1,y1) and P2 = (x2,y2) the y coordinate of the segment where it crosses x is given by sy = (x-x1)*(y2-y1)/(x2-x1) + y1

Check if sy < y (only if x1 < x < x2 or x2 < x < x1). If there are an odd number of these then P is inside.

There are subtle issues with this when one of the verticies of the polygon is at exactly the same y position as the point in question. You'll have to be careful about that case.

风为裳 2024-08-24 14:00:22

贾斯汀,

您可能还需要更好地定义“多边形外部”来构造线段。

采取分钟和;所有 x 和 的最大值y 坐标并构造一个矩形 (xmin,ymin),(xmax,ymin),(xmax,ymax),(xmin,ymax)。矩形之外的任何点肯定都会在多边形之外 - 然后继续,如上面其他人所示。每个多边形线段和构造的线由方程 y = ax + b 以及每个线段的范围 xlo 和 xhi 定义。您构造的线要么穿过范围内的线段,要么不穿过。即两个联立方程在段范围内的解要么存在,要么不存在。只需计算存在的解决方案的数量即可获得交叉点的数量。

Justin,

You may also need to better define "outside the polygon" to construct a segment.

Take the min & max of all the x & y coordinates and construct a rectangle (xmin,ymin),(xmax,ymin),(xmax,ymax),(xmin,ymax). Any point outside the rectangle would definitely be outside the polygon - then continue as others have shown above. Each polygon segment and the constructed line is defined by an equation y = ax + b and, for each segment, a range xlo and xhi. Your constructed line either crosses a segment in the range or not. That is, the solution of the two simultaneous equations in the segment range either exists or not. Just count the number of solutions that exist to get the number of intersections.

一影成城 2024-08-24 14:00:22

我假设你在飞机上(2D)。

  • 计算每条边的斜率(在某些坐标系中)以及从 X 点到 Y 点的直线(XY 线)的斜率。
  • 对于斜率不等于 XY 斜率的所有边,计算交点。
  • 对于每个点,确定交点是否在线段 XY 和定义边的线段上。如果是的话,你就跨过了那一边。 (检查交叉点的坐标,看看 x 和 y 分量是否都包含在每个线段的值范围内。)
  • 计算交叉点的数量,您就会得到答案。

I'll assume you're in a plane (2D).

  • Calculate the slopes of each side (in some coordinate system) and the slope of the line from point X to point Y (line XY).
  • For all sides where the slope does not equal the slope of XY, calculate the point of intersection.
  • For each point, determine whether the point of intersection is on line segment XY and the line segment defining the side. If it is, you crossed that side. (Check the coordinates of the intersection and see if both the x and y components are included in the range of values for each line segment.)
  • Count up the number of crossings, and you have your answer.
人间不值得 2024-08-24 14:00:22

计算多边形和点的绕数

Compute the winding number of the polygon and the point.

剑心龙吟 2024-08-24 14:00:22

试试这个,

public static bool PointinPolygon( Point[] points, Point p )
    {
        bool result = false;

        for( int i = 0; i < points.Length - 1; i++ )
        {
            if( ( ( ( points[ i + 1 ].Y <= p.Y ) && ( p.Y < points[ i ].Y ) ) || ( ( points[ i ].Y <= p.Y ) && ( p.Y < points[ i + 1 ].Y ) ) ) && ( p.X < ( points[ i ].X - points[ i + 1 ].X ) * ( p.Y - points[ i + 1 ].Y ) / ( points[ i ].Y - points[ i + 1 ].Y ) + points[ i + 1 ].X ) )
            {
                result = !result;
            }
        }
        return result;
    }

Try this,

public static bool PointinPolygon( Point[] points, Point p )
    {
        bool result = false;

        for( int i = 0; i < points.Length - 1; i++ )
        {
            if( ( ( ( points[ i + 1 ].Y <= p.Y ) && ( p.Y < points[ i ].Y ) ) || ( ( points[ i ].Y <= p.Y ) && ( p.Y < points[ i + 1 ].Y ) ) ) && ( p.X < ( points[ i ].X - points[ i + 1 ].X ) * ( p.Y - points[ i + 1 ].Y ) / ( points[ i ].Y - points[ i + 1 ].Y ) + points[ i + 1 ].X ) )
            {
                result = !result;
            }
        }
        return result;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文