Check if polygon is inside a polygon

33,127

Solution 1

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.

Solution 2

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.

Share:
33,127

Related videos on Youtube

chuysbz
Author by

chuysbz

Updated on September 08, 2020

Comments

  • chuysbz
    chuysbz over 3 years

    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.

    • Admin
      Admin over 13 years
      Check if all points of polygon A are inside polygon B
    • Phrogz
      Phrogz over 13 years
      I would first check to see if corners of the bounding box of one polygon are inside the other; that will be a fast test. After that, though, follow @M28's advice and check every point of one polygon inside the other.
    • payne
      payne over 13 years
      @M28 Checking just the vertex points doesn't work. If B is not convex, then you have (many) cases where all of A vertices are in B, but a portion of A still crosses outside of B.
    • Admin
      Admin over 13 years
      @payne True, but he said he would only use rectangles
    • payne
      payne over 13 years
      @M28: he said he's checking to see if a rectangle is inside a polygon. Consider a polygon that's a star-like shape: all the corners of the rectangle could be inside the star, but portions of the rectangle could lie outside the star.
    • Admin
      Admin over 13 years
      @payne Yep, Now I noticed that he said a rectangle inside a polygon.
  • sdleihssirhc
    sdleihssirhc over 13 years
    If there are no line intersections, then wouldn't you only need to check one point?
  • brian-d
    brian-d over 13 years
    That would do it. Very slow algorithm though, isn't it? O(n!) I think.
  • moinudin
    moinudin over 13 years
    @Hops O(NM) where A has N sides, B has M. You can speed it up if necessary, but since OP mentions one of the polygons is a rectangle I don't see the point since it makes the solution a lot more complex. I've edited in how to do this.
  • brian-d
    brian-d over 13 years
    Gah. Brain fart on my side. That's what I get for trying to think this through on a Friday afternoon. Sides... not points. For some reason I was thinking you'd have to check every possible line in the polygon... not the lines actually making up the polygon. Been one of those days, thanks for setting me straight :)
  • Yochai Timmer
    Yochai Timmer over 13 years
    If there's no intersection, they could also be near each-other. They don't have to contain one another.
  • moinudin
    moinudin over 13 years
    @Yochai That's why you test if one of the polygon's points is insider the other polygon.
  • Igor Brejc
    Igor Brejc over 11 years
    @marcog: what if one polygon is sharing some of its vertices with another and is just touching it? In that case if you chose a wrong (=shared) vertex for testing, you'd come with a wrong conclusion that one polygon is actually inside the other.
  • thelastshadow
    thelastshadow over 8 years
    This only works for convex outer shapes. Concave outer shapes can contain all the points of the inner shape, but have overlapping edges.
  • greybeard
    greybeard over 7 years
    @IgorBrejc: I can imagine vertices to share (have identical) coordinates (coincide). What does it mean for polygons to share vertices?
  • Pablo H
    Pablo H almost 7 years
    In some applications polygons can have holes. In that case the above algorithm would need to be adapted. Also, depending on the application, you should be careful about numerical precision when computing intersections of degenerate cases (e.g point on line).
  • kelunik
    kelunik about 6 years
    This algorithm works only for polygons with no holes in any polygon and only for one polygon being truly inside the other. A polygon that shares edges (example) isn't detected correctly and requires all points of one polygon to be inside the other (in case there are any overlapping edges) as far as I can tell.