确定一个点是否位于由给定纬度/经度的 3 个点组成的三角形内
我有 3 个点( lat , lon )形成一个三角形。我如何找到一个点是否在这个三角形内?
I have 3 points ( lat , lon ) that form a triangle.How can i find if a point is inside this triangle?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
Java 代码只是三角形,即 3 个点。
Java Code for just triangle , that is 3 points.
这是重心坐标解决方案的 Javascript 实现此处讨论:
据说速度更快比基于跨产品的解决方案。
Here's a Javascript implementation of the barycentric coordinates solution discussed here:
It's said to be faster than the cross-product based solutions.
您可以使用点多边形测试。
这很简单。从你的点到东画一条线,距离足够远。计算该线与您的多边形相交的次数。如果是偶数,你的点就在外面,如果是奇数,你的点就在里面。
这适用于任何类型的多边形。
You can use point-polygon test.
It's simple. Draw a line from your point to East for a big enough distance. Count the number of times that line intersects with your plygon. If it's even, your point is outside, if odd, its inside.
That works for any type of polygon.
大多数语言都包含此功能。在 Java 中是 Polygon.contains()
http://docs.oracle.com/javase/ 7/docs/api/java/awt/Polygon.html
只需从您的点创建一个多边形,然后在您的测试点上调用 contains() 即可。
Most languages include a function for this. In Java it's Polygon.contains()
http://docs.oracle.com/javase/7/docs/api/java/awt/Polygon.html
Simply create a polygon from your points, and then call contains() on your test point.
主要问题是您是否可以为此使用二维近似(换句话说,您的三角形是否足够小)。
如果是这样,像重心坐标这样的简单东西就可以很好地工作。
The main question is whether you can use a 2D approximation for this (in other words, is your triangle small enough).
If so, something simple like barycentric coordinates will work well.
尝试光线投射算法。
http://en.wikipedia.org/wiki/Point_in_polygon
实现起来非常简单。
Try the ray casting algorithm.
http://en.wikipedia.org/wiki/Point_in_polygon
It is pretty simple to implement.
在下面的链接中进行了解释
http://www.blackpawn.com/texts/pointinpoly/default .html
Explained at the link below
http://www.blackpawn.com/texts/pointinpoly/default.html
我今天就做了这样的事!还有(lat,lon),实际上是(theta,phi),尽管我对我正在使用的网格了解更多。我正在使用 (theta, phi) 0 <= theta <= PI && 0 <= phi <= 2*PI。
您会发现,如果其中一个顶点位于球体的顶部或底部,您可能会遇到一些麻烦,因为在我的情况下 phi 并未真正定义。你最终会在那里得到一个奇点。基本上你已经得到了一个正方形,这样可以更轻松地检查你的点是否位于其中。
在所有其他情况下,如果您已将点转换为 (lat, lon) / (theta, phi)。只需使用@Michelle Six 描述的方法应该很简单。
I've done something like this today! Also with (lat, lon), actually (theta, phi), although I knew a little more about the mesh I was working with. I'm working with (theta, phi) with 0 <= theta <= PI && 0 <= phi <= 2*PI.
You'll find that you might have some trouble if one of the vertices is at the top or bottom of your sphere, since in my case phi isn't really defined. You end up with a singularity there. You've basically got a square, which makes it easier to check whether your point lies within it or not.
In all other cases, if you've converted your point into (lat, lon) / (theta, phi). It should be simple to just use the method as described by @Michelle Six.