JavaScript 中的多边形命中测试中的点(Chrome 错误)
我正在开发一个用 javaScript / jQuery 编写的游戏。我的代码的一部分在平铺网格上绘制一个随机多边形(一个岛屿)。我需要检查一个点是否在多边形内部。
我正在使用在 几个 Stack Overflow 上的位置(原始 此处)。这在 Firefox 中运行良好。在 Chrome 中,多边形内部有一些点,脚本说这些点不在多边形内部。
在火狐中:
在 Chrome 中(该岛有所不同,因为它们是随机生成的):
请查看此处的源代码,特别是 pointPolygonIntersect 函数: 多边形中的点命中测试
任何人都可以弄清楚为什么会发生这种情况吗?原始脚本是用 C 编写的,而我使用的是 JavaScript 版本 - 这会导致问题吗?
I am working on a game written in javaScript / jQuery. Part of my code draws a random polygon (an island) on a tile grid. I need to check if a point is inside the polygon.
I am using a point-in-polygon intersection script which I found in several places on Stack Overflow (original here). This works fine in Firefox. In Chrome, there are points inside the polygon which the script says are not inside it.
In Firefox:
In Chrome (the island is different because they are randomly generated):
Please take a look at the source here, particularly the pointPolygonIntersect function:
Point in Polygon Hit Test
Can anyone figure out why this is happening? The original script is in C, and I am using a JavaScript version - could this be causing the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
选择一个岛屿并坚持下去。在两个浏览器中跟踪代码并查看它们的不同之处。没有理由与你可以轻松删除的随机性作斗争......
Pick an island and stick with it. Trace the code in both browsers and see where they differ. There's no reason to fight against the randomness that you can easily remove...
我快速浏览了一下。无法跟踪应用程序流程,但看起来奇怪的是
代码行。如果第一次满足条件,为什么不直接设置为true,或者直接返回true呢?我假设,chrome 变为“true”,然后下次在 x/y 范围内时将其反转。
我不熟悉 Raphael 或 SVG,但看起来你的多边形是正方形,因此你可以在测试中做一个简单的测试
I gave it a quick look. Couldn't track the app flow, but what seems strange is the
line of code. Why dont you just set it to true, or directly return true, if you meet the conditions the first time? I'm assuming, that chrome goes to "true" and then inverts it the next time its within the x/y bounds.
I'm not familiar with Raphael or SVG, but it seems your polygons are squares, thus you could do a simple within test