Check if polygon is inside a polygon

后端 未结 5 1117
鱼传尺愫
鱼传尺愫 2020-12-01 06:35

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 wo

相关标签:
5条回答
  • 2020-12-01 07:16

    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.

    0 讨论(0)
  • 2020-12-01 07:16

    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)}
        }
    }
    
    0 讨论(0)
  • 2020-12-01 07:27

    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.

    0 讨论(0)
  • 2020-12-01 07:36

    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?

    0 讨论(0)
  • 2020-12-01 07:37

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题