检查多边形是否在多边形内部

发布于 2024-10-15 03:29:05 字数 363 浏览 13 评论 0原文

昨天我想检查一个点是否在多边形内,发现了这个很棒的脚本:https://github .com/tparkin/Google-Maps-Point-in-Polygon

但今天在工作时,我被告知我们的客户需要检查一个多边形是否在另一个多边形内部。我想知道是否有一个公式可以让我采用两个坐标(而不是一个来检查点),并从这两个坐标生成一个矩形并检查该矩形是否在多边形内。

我不知道我是否在问一个愚蠢的问题(高中老师曾经说过“没有愚蠢的问题,只有不问的傻瓜”),但如果你完全不明白我的意思,但是只是一点点,如果您能告诉我从哪里开始,我将不胜感激。

Yesterday I was looking to check if a point was inside a polygon and found this great script: https://github.com/tparkin/Google-Maps-Point-in-Polygon

But today at work I was told that our client needs to check if one polygon is inside another polygon. I am wondering if is there a formula where I can take, let's say, two coordinates (instead of one to check a point), and from those two coordinates generate a rectangle and check if that rectangle is inside a polygon.

I don't know if I'm asking a stupid question (a teacher in highschool used to say "there are no stupid questions, there is only fools who don't ask"), but if you don't understand me totally but just a bit, I'd be grateful if you just tell me where to start.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

郁金香雨 2024-10-22 03:29:05

对每对线(每个多边形各一条)执行线相交测试。如果没有成对的线相交,并且多边形 A 的线端点之一位于多边形 B 内部,则 A 完全位于 B 内部。

以上适用于任何类型的多边形。如果多边形是凸的,您可以跳过线相交测试,只测试 A 的所有线端点都在 B 内部。

如果确实有必要,您可以使用 扫线算法

可供参考的多边形示例:

Perform line intersection tests for each pair of lines, one from each polygon. If no pairs of lines intersect and one of the line end-points of polygon A is inside polygon B, then A is entirely inside B.

The above works for any type of polygon. If the polygons are convex, you can skip the line intersection tests and just test that all line end-points of A are inside B.

If really necessary, you can speed up the line intersection tests using the sweep line algorithm.

Example Polygons for reference:

简单爱 2024-10-22 03:29:05

首先使用脚本检查多边形中的一个角点是否位于另一个多边形内部。然后检查多边形中的任何直线是否与另一个多边形中的任何直线相交。如果不是,则该多边形位于另一个多边形内部。

First check that one of the corner points in the polygon is inside the other polygon using the script. Then check if any of the lines in the polygon crosses any of the lines in the other polygon. If they don't, the polygon is inside the other polygon.

他不在意 2024-10-22 03:29:05

多边形是凸的吗因为,如果是的话,您可以为“矩形”的两个“角”运行“多边形中的点”脚本。如果两个角都在里面,并且多边形没有向内的“曲线”,那么整个矩形不就在里面了吗?

Is the polygon convex? Because, if it is, you could just run the "point in polygon" script for both "corners" of your "rectangle." If both corners are in, and the polygon has no "curves" inward, then wouldn't the whole rectangle be in?

孤城病女 2024-10-22 03:29:05

也许这部分代码可以帮助你:

package com.polygons;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

/**
 * Utility to Manipulate Polygons
 * 
 * @author fernando.hernandez
 *
 */

public class PolygonUtils {

    /**
     * Check if  polygon2 is inside polygon to polygon1
     * @param polygon1 polygon that contains other 
     * @param polygon2 polygon that is inner to other
     * @return true if polygon2 is inner to polygon1
     */
    public boolean isInsidePolygon(Polygon polygon1, Polygon polygon2){
        //all points in inner Polygon should be contained in polygon
        int[] xpoints = polygon2.xpoints;
        int[] ypoints = polygon2.ypoints;
        boolean result =  true;
        for (int i = 0, j = 0; i < polygon2.npoints ; i++,j++) {
             result = polygon1.contains(new Point(xpoints[i], ypoints[j]));
             if(!result) break;   
        }
        return result;
    }
}

Maybe this part of the code can help you:

package com.polygons;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

/**
 * Utility to Manipulate Polygons
 * 
 * @author fernando.hernandez
 *
 */

public class PolygonUtils {

    /**
     * Check if  polygon2 is inside polygon to polygon1
     * @param polygon1 polygon that contains other 
     * @param polygon2 polygon that is inner to other
     * @return true if polygon2 is inner to polygon1
     */
    public boolean isInsidePolygon(Polygon polygon1, Polygon polygon2){
        //all points in inner Polygon should be contained in polygon
        int[] xpoints = polygon2.xpoints;
        int[] ypoints = polygon2.ypoints;
        boolean result =  true;
        for (int i = 0, j = 0; i < polygon2.npoints ; i++,j++) {
             result = polygon1.contains(new Point(xpoints[i], ypoints[j]));
             if(!result) break;   
        }
        return result;
    }
}
策马西风 2024-10-22 03:29:05

我必须找到类似的解决方案。这是我到目前为止所拥有的:

  1. 首先,我在数组中获取了所有 1 级多边形坐标[pol1cords[cord1,cord2...],pol2cords[cord1,cord2...],..]
  2. 然后获取所有 3 级多边形并绘制它们
  3. 然后对于每个 1 级多边形,我检查每个多边形坐标是否位于绘制的 3 级坐标内 google.maps.geometry.poly.containsLocation(latLng, pol )
  4. 如果返回true 计数器就会上升
  5. 最后如果计数器等于该数组的长度,结果将为 true(1 级多边形在 3 级多边形内部) 。

我的算法看起来像这样:

""区域(级别 3)->区(级别 2)->VDC(级别 1)"" vdcs = getVDCs();
->在具有名称、id 和多边形坐标的数组中给出 vdcs zone = getZones(); -> 在数组中给出区域,其中包含名称、ID 和
多边形坐标

foreach(zones as zone){
    drawPolygon(zone[coordinates]);
    foreach(vdcs as vdc){
        foreach(vdc[coordinates] as coordinate){
            result = checkLocation(zone, coordinate);
            if(result) counter++;
        }
        if(counter = vdc[coordinates].length){writeConsole(vdc_id+"true in"+zone_id)}
    }
}

I had to find a similar solution. Here is what i have so far :

  1. First i fetched all the level 1 polygon coordinates in an array[pol1cords[cord1,cord2...],pol2cords[cord1,cord2...],..]
  2. Then fetched all the level 3 polygons and plotted them
  3. Then for each level 1 polygon, i checked if each of the polygon coordinates was inside the plotted level 3 coordinate with google.maps.geometry.poly.containsLocation(latLng, pol)
  4. If it returned true counter would go up
  5. At last if the counter was equal to the length of that array, the result would be true(the level 1 polygon is inside the level3 polygon).

My algorithm looks something like this:

""Zone(level 3)->District(level 2)->VDC(level 1)"" vdcs = getVDCs();
-> gives vdcs in an array which has name, id and polygon coordinates zones = getZones(); ->gives zones in an array which has name, id and
polygon coordinates

foreach(zones as zone){
    drawPolygon(zone[coordinates]);
    foreach(vdcs as vdc){
        foreach(vdc[coordinates] as coordinate){
            result = checkLocation(zone, coordinate);
            if(result) counter++;
        }
        if(counter = vdc[coordinates].length){writeConsole(vdc_id+"true in"+zone_id)}
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文