I'm working on a JS program which I need to have determine if points are within four corners in a coordinate system.
我正在研究一个JS程序,我需要确定点是否在坐标系的四个角内。
Could somebody point me in the direction of an answer?
有人能指出我的答案吗?
I'm looking at what I think is called a convex quadrilateral. That is, four pretty randomly chosen corner positions with all angles smaller than 180°.
我正在看我认为被称为凸四边形的东西。也就是说,四个相当随机选择的角位置,所有角度都小于180°。
Thanks.
3 个解决方案
#1
9
There are two relatively simple approaches. The first approach is to draw a ray from the point to "infinity" (actually, to any point outside the polygon) and count how many sides of the polygon the ray intersects. The point is inside the polygon if and only if the count is odd.
有两种相对简单的方法。第一种方法是从点到“无限”(实际上,到多边形外的任何点)绘制光线,并计算光线相交多边形的边数。当且仅当计数为奇数时,该点在多边形内。
The second approach is to go around the polygon in order and for every pair of vertices vi and vi+1 (wrapping around to the first vertex if necessary), compute the quantity (x - xi) * (yi+1 - yi) - (xi+1 - xi) * (y - yi). If these quantities all have the same sign, the point is inside the polygon. (These quantities are the Z component of the cross product of the vectors (vi+1 - vi) and (p - vi). The condition that they all have the same sign is the same as the condition that p is on the same side (left or right) of every edge.)
第二种方法是按顺序绕过多边形,对于每对顶点vi和vi + 1(必要时环绕第一个顶点),计算数量(x - xi)*(yi + 1 - yi) - (xi + 1 - xi)*(y - yi)。如果这些数量都具有相同的符号,则该点位于多边形内。 (这些量是向量(vi + 1-vi)和(p-vi)的叉积的Z分量。它们都具有相同符号的条件与p在同一侧的条件相同每个边缘(左或右)。)
Both approaches need to deal with the case that the point is exactly on an edge or on a vertex. You first need to decide whether you want to count such points as being inside the polygon or not. Then you need to adjust the tests accordingly. Be aware that slight numerical rounding errors can give a false answer either way. It's just something you'll have to live with.
两种方法都需要处理点恰好位于边缘或顶点上的情况。首先需要确定是否要将这些点计数在多边形内部。然后你需要相应地调整测试。请注意,轻微的数字舍入错误可能会给出错误的答案。这只是你必须要忍受的东西。
Since you have a convex quadrilateral, there's another approach. Pick any three vertices and compute the barycentric coordinates of the point and of the fourth vertex with respect to the triangle formed by the three chosen vertices. If the barycentric coordinates of the point are all positive and all less than the barycentric coordinates of the fourth vertex, then the point is inside the quadrilateral.
由于你有一个凸四边形,还有另一种方法。选择任意三个顶点并计算点和第四个顶点相对于由三个选定顶点形成的三角形的重心坐标。如果该点的重心坐标都是正的并且都小于第四个顶点的重心坐标,那么该点在四边形内部。
P.S. Just found a nice page here that lists quite a number of strategies. Some of them are very interesting.
附:刚刚在这里找到一个很好的页面列出了很多策略。其中一些非常有趣。
#2
0
You need to use winding, or the ray trace method.
您需要使用绕线或光线跟踪方法。
With winding, you can determine whether any point is inside any shape built with line segments.
通过缠绕,您可以确定是否有任何点位于使用线段构建的任何形状内。
Basically, you take the cross product of each line segment with the point, then add up all the results. That's the way I did it to decide if a star was in a constellation, given a set of constellation lines. I can see that there are other ways..
基本上,您将每个线段的叉积与该点相乘,然后将所有结果相加。这就是我用它来确定一颗恒星是否在一个星座中的方式,给定了一组星座线。我可以看到还有其他方法..
http://en.wikipedia.org/wiki/Point_in_polygon
There must be some code for this in a few places.
在一些地方必须有一些代码。
#3
0
It is MUCH easier to see if a point lies within a triangle.
如果一个点位于三角形内,则更容易看到。
Any quadrilateral can be divided into two triangles.
任何四边形都可以分为两个三角形。
If the point is in any of the two triangles that comprise the quadrilateral, then the point is inside the quadrilateral.
如果该点位于构成四边形的两个三角形中的任何一个中,则该点位于四边形内。
#1
9
There are two relatively simple approaches. The first approach is to draw a ray from the point to "infinity" (actually, to any point outside the polygon) and count how many sides of the polygon the ray intersects. The point is inside the polygon if and only if the count is odd.
有两种相对简单的方法。第一种方法是从点到“无限”(实际上,到多边形外的任何点)绘制光线,并计算光线相交多边形的边数。当且仅当计数为奇数时,该点在多边形内。
The second approach is to go around the polygon in order and for every pair of vertices vi and vi+1 (wrapping around to the first vertex if necessary), compute the quantity (x - xi) * (yi+1 - yi) - (xi+1 - xi) * (y - yi). If these quantities all have the same sign, the point is inside the polygon. (These quantities are the Z component of the cross product of the vectors (vi+1 - vi) and (p - vi). The condition that they all have the same sign is the same as the condition that p is on the same side (left or right) of every edge.)
第二种方法是按顺序绕过多边形,对于每对顶点vi和vi + 1(必要时环绕第一个顶点),计算数量(x - xi)*(yi + 1 - yi) - (xi + 1 - xi)*(y - yi)。如果这些数量都具有相同的符号,则该点位于多边形内。 (这些量是向量(vi + 1-vi)和(p-vi)的叉积的Z分量。它们都具有相同符号的条件与p在同一侧的条件相同每个边缘(左或右)。)
Both approaches need to deal with the case that the point is exactly on an edge or on a vertex. You first need to decide whether you want to count such points as being inside the polygon or not. Then you need to adjust the tests accordingly. Be aware that slight numerical rounding errors can give a false answer either way. It's just something you'll have to live with.
两种方法都需要处理点恰好位于边缘或顶点上的情况。首先需要确定是否要将这些点计数在多边形内部。然后你需要相应地调整测试。请注意,轻微的数字舍入错误可能会给出错误的答案。这只是你必须要忍受的东西。
Since you have a convex quadrilateral, there's another approach. Pick any three vertices and compute the barycentric coordinates of the point and of the fourth vertex with respect to the triangle formed by the three chosen vertices. If the barycentric coordinates of the point are all positive and all less than the barycentric coordinates of the fourth vertex, then the point is inside the quadrilateral.
由于你有一个凸四边形,还有另一种方法。选择任意三个顶点并计算点和第四个顶点相对于由三个选定顶点形成的三角形的重心坐标。如果该点的重心坐标都是正的并且都小于第四个顶点的重心坐标,那么该点在四边形内部。
P.S. Just found a nice page here that lists quite a number of strategies. Some of them are very interesting.
附:刚刚在这里找到一个很好的页面列出了很多策略。其中一些非常有趣。
#2
0
You need to use winding, or the ray trace method.
您需要使用绕线或光线跟踪方法。
With winding, you can determine whether any point is inside any shape built with line segments.
通过缠绕,您可以确定是否有任何点位于使用线段构建的任何形状内。
Basically, you take the cross product of each line segment with the point, then add up all the results. That's the way I did it to decide if a star was in a constellation, given a set of constellation lines. I can see that there are other ways..
基本上,您将每个线段的叉积与该点相乘,然后将所有结果相加。这就是我用它来确定一颗恒星是否在一个星座中的方式,给定了一组星座线。我可以看到还有其他方法..
http://en.wikipedia.org/wiki/Point_in_polygon
There must be some code for this in a few places.
在一些地方必须有一些代码。
#3
0
It is MUCH easier to see if a point lies within a triangle.
如果一个点位于三角形内,则更容易看到。
Any quadrilateral can be divided into two triangles.
任何四边形都可以分为两个三角形。
If the point is in any of the two triangles that comprise the quadrilateral, then the point is inside the quadrilateral.
如果该点位于构成四边形的两个三角形中的任何一个中,则该点位于四边形内。