Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Check if polygon is Inside another

Check if polygon is Inside another

Scheduled Pinned Locked Moved C#
question
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gilvani
    wrote on last edited by
    #1

    Does anyone know a mathematical formula (which can be translated to C #) to check if a polygon is inside another polygon?

    P A 2 Replies Last reply
    0
    • G gilvani

      Does anyone know a mathematical formula (which can be translated to C #) to check if a polygon is inside another polygon?

      P Offline
      P Offline
      pt1401
      wrote on last edited by
      #2

      Regular polygons (where all the angles or equal) or irregular? Needless to say, irregular ones are the PITA...especially where they are the containing polygon. Just to confirm how much of a PITA irregular polygons are, here's a discussion that may may like to read (though you'll probably wish you hadn't) ;) The Polygon Containment Problem[^]

      G 1 Reply Last reply
      0
      • P pt1401

        Regular polygons (where all the angles or equal) or irregular? Needless to say, irregular ones are the PITA...especially where they are the containing polygon. Just to confirm how much of a PITA irregular polygons are, here's a discussion that may may like to read (though you'll probably wish you hadn't) ;) The Polygon Containment Problem[^]

        G Offline
        G Offline
        gilvani
        wrote on last edited by
        #3

        There are irregular polygons.

        1 Reply Last reply
        0
        • G gilvani

          Does anyone know a mathematical formula (which can be translated to C #) to check if a polygon is inside another polygon?

          A Offline
          A Offline
          Alan Balkany
          wrote on last edited by
          #4

          If polygon A is inside polygon B, then: 1. Every vertex of A will be inside B, and 2. No edge of A will intersect an edge of B. So you need two subroutines: One to detect if a point is in a polygon, and one to detect if two line segments intersect each other. Test all points of A to see if they're in B. If one isn't, then B doesn't contain A. Then test all edges of A to see if any intersect an edge of B. If none do, then polygon A is inside polygon B.

          G 1 Reply Last reply
          0
          • A Alan Balkany

            If polygon A is inside polygon B, then: 1. Every vertex of A will be inside B, and 2. No edge of A will intersect an edge of B. So you need two subroutines: One to detect if a point is in a polygon, and one to detect if two line segments intersect each other. Test all points of A to see if they're in B. If one isn't, then B doesn't contain A. Then test all edges of A to see if any intersect an edge of B. If none do, then polygon A is inside polygon B.

            G Offline
            G Offline
            gilvani
            wrote on last edited by
            #5

            Yes you are right, but this is a very costly routine, and it might be performed more than 1000 times during the opening of a shapefile.

            A 1 Reply Last reply
            0
            • G gilvani

              Yes you are right, but this is a very costly routine, and it might be performed more than 1000 times during the opening of a shapefile.

              A Offline
              A Offline
              Alan Balkany
              wrote on last edited by
              #6

              So you need efficiency too. Ok, calculate the bounding rectangles of both polygons. If the bounding rectangles don't overlap, neither polygon contains the other, so you can skip the more expensive test. If they do overlap, check if each polygon contains points of the other in the overlap region. If polygon B contains some points of polygon A, but not others, then B doesn't contain A.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups