首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何检查geopoint是否位于osmdroid奖励包中绘制的闭合多边形内

如何检查geopoint是否位于osmdroid奖励包中绘制的闭合多边形内
EN

Stack Overflow用户
提问于 2015-11-30 21:34:40
回答 2查看 546关注 0票数 0

我正在使用android中的osmdroid库以及osmdroid奖励包。我已经使用命名状态多边形点绘制了状态的多边形。现在,我想检查Geopoint是否位于绘制的多边形内。我该怎么做呢?

EN

回答 2

Stack Overflow用户

发布于 2016-03-21 18:36:00

请使用Java尝试以下算法

代码语言:javascript
复制
class Point
{
    int x, y;

    Point()
    {}

    Point(int p, int q)
    {
        x = p;
        y = q;
    }
}

public class Position_Point_WRT_Polygon
{

    public static boolean onSegment(Point p, Point q, Point r)
    {
        if (q.x <= Math.max(p.x, r.x) && q.x >= Math.min(p.x, r.x)
                && q.y <= Math.max(p.y, r.y) && q.y >= Math.min(p.y, r.y))
            return true;
        return false;
    }

    public static int orientation(Point p, Point q, Point r)
    {
        int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);

        if (val == 0)
            return 0;
        return (val > 0) ? 1 : 2;
    }

    public static boolean doIntersect(Point p1, Point q1, Point p2, Point q2)
    {

        int o1 = orientation(p1, q1, p2);
        int o2 = orientation(p1, q1, q2);
        int o3 = orientation(p2, q2, p1);
        int o4 = orientation(p2, q2, q1);

        if (o1 != o2 && o3 != o4)
            return true;

        if (o1 == 0 && onSegment(p1, p2, q1))
            return true;

        if (o2 == 0 && onSegment(p1, q2, q1))
            return true;

        if (o3 == 0 && onSegment(p2, p1, q2))
            return true;

        if (o4 == 0 && onSegment(p2, q1, q2))
            return true;

        return false;
    }

    public static boolean isInside(Point polygon[], int n, Point p)
    {
        int INF = 10000;
        if (n < 3)
            return false;

        Point extreme = new Point(INF, p.y);

        int count = 0, i = 0;
        do
        {
            int next = (i + 1) % n;
            if (doIntersect(polygon[i], polygon[next], p, extreme))
            {
                if (orientation(polygon[i], p, polygon[next]) == 0)
                    return onSegment(polygon[i], p, polygon[next]);

                count++;
            }
            i = next;
        } while (i != 0);

        return (count & 1) == 1 ? true : false;
    }

    public static void main(String args[])
    {
        Point polygon1[] = { new Point(0, 0), new Point(10, 0),
                new Point(10, 10),new Point(5, 5), new Point(0, 10) };
        int n = 5;

        Point p = new Point(5, 7);
        System.out.println("Point P(" + p.x + ", " + p.y
                + ") lies inside polygon1: " + isInside(polygon1, n, p));
        p = new Point(11, 11);
        System.out.println("Point P(" + p.x + ", " + p.y
                + ") lies inside polygon1: " + isInside(polygon1, n, p));

        Point polygon2[] = { new Point(0, 0), new Point(5, 5), new Point(5, 0) };
        n = 3;

        p = new Point(3, 3);
        System.out.println("Point P(" + p.x + ", " + p.y
                + ") lies inside polygon2: " + isInside(polygon2, n, p));
        p = new Point(5, 1);
        System.out.println("Point P(" + p.x + ", " + p.y
                + ") lies inside polygon2: " + isInside(polygon2, n, p));
        p = new Point(8, 1);
        System.out.println("Point P(" + p.x + ", " + p.y
                + ") lies inside polygon2: " + isInside(polygon2, n, p));

        Point polygon3[] = { new Point(0, 0), new Point(10, 0),
                new Point(10, 10), new Point(0, 10), new Point(5, 5) };
        n = 5;

        p = new Point(-1, 10);
        System.out.println("Point P(" + p.x + ", " + p.y
                + ") lies inside polygon3: " + isInside(polygon3, n, p));
    }
}
票数 2
EN

Stack Overflow用户

发布于 2015-12-02 01:57:08

OSMBonusPack多边形提供了一个“包含”方法。

但是,如javadoc中所述,只有在之前绘制过多边形并且MapView定位没有更改的情况下,此方法才会返回正确的结果。

因此,它对于触摸事件检测是很好的,但可能不适用于各种用途。

如果它不适合您的上下文,您将不得不实现该功能。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33999885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档