多边形触摸检测Google Map API V2

多边形触摸检测Google Map API V2,第1张

多边形触摸检测Google Map API V2

您要解决的问题是“ 多边形中点”测试。

为了帮助形象化射线投射的概念:

在一张纸上绘制一个多边形。然后,从任意随机点开始,在页面右侧绘制一条直线。如果线与多边形相交的次数是奇数次,则意味着起点在多边形内。

那么,如何在代码中做到这一点?

您的多边形由一系列顶点组成:

ArrayList<Geopoint> vertices
。您需要
LineSegment
单独查看每个对象,并查看它们是否
Ray
相交

private boolean isPointInPolygon(Geopoint tap, ArrayList<Geopoint> vertices) {    int intersectCount = 0;    for(int j=0; j<vertices.size()-1; j++) {        if( rayCastIntersect(tap, vertices.get(j), vertices.get(j+1)) ) { intersectCount++;        }    }    return (intersectCount%2) == 1); // odd = inside, even = outside;}private boolean rayCastIntersect(Geopoint tap, Geopoint vertA, Geopoint vertB) {    double aY = vertA.getLatitude();    double bY = vertB.getLatitude();    double aX = vertA.getLongitude();    double bX = vertB.getLongitude();    double pY = tap.getLatitude();    double pX = tap.getLongitude();    if ( (aY>pY && bY>pY) || (aY<pY && bY<pY) || (aX<pX && bX<pX) ) {        return false; // a and b can't both be above or below pt.y, and a or b must be east of pt.x    }    double m = (aY-bY) / (aX-bX);    // Rise over run    double bee = (-aX) * m + aY;     // y = mx + b    double x = (pY - bee) / m;       // algebra is neat!    return x > pX;}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5487697.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存