Results 1 to 10 of 10

Thread: PolyGon in PolyGon

  1. #1

    Thread Starter
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575

    PolyGon in PolyGon

    Can anyone tell me how to check if one 2D polygon is within another. This function needs to be as quick as possible so preferably C++.

    Thanks

    Rob
    My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]

  2. #2
    Lively Member
    Join Date
    Jun 2001
    Location
    Banana Republic
    Posts
    115
    Don't know if it is of any help..but give it a look.

    http://astronomy.swin.edu.au/pbourke...ry/insidepoly/
    Marriage - is not a word, but a sentence.

  3. #3

    Thread Starter
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575
    Thanks for the reply - but I need to check for an entire polygon inside another polygon. I can't check each point to see if it is inside or out of the other polygon - because while all the points may be inside - a line connecting 2 points may fall outside.

    Any other sites???

    Thanks

    Rob
    My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    A line is totally inside a polygon if doesn't cross any of it's sides, and a line to one of it's end points from an arbitary point outside (preferably vertical) crosses an odd amount of the polygons sides. A whole polygon is inside another if none of it's sides crosses any of the other polygon's and one of the corners can be verified to be inside the polygon using the method I mentioned
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5

    Thread Starter
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575
    Cheers - any sample code?

    Rob
    My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Something like this:

    bool is=false;//inside/intersection

    for (lineiterator i2=plg2.first;i2<pl2.last;i2++){

    is^=
    ((
    (plg1.first.first.x>i2.first.first.x
    )^(
    plg1.first.last.x>i2.first.last.x
    ))&&(
    i2.first.first.y+(i2.first.last.y-i2.first.first.y)/(plg1.first.first.x-i2.first.first.x)*(i2.first.last.x-i2.first.first.x)
    )

    }
    if (!is) return false;

    for (lineiterator i1=plg1.first;i1<pl1.last;i1++)
    for (i2=plg2.first;i2<pl2.last;i2++)
    if(is=i1.iscross(i2)) break;

    return is;
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7

    Thread Starter
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575
    Thanks - I am using Borland so I don't have that lineiterator object - however I'll work it out.

    Thanks

    Rob
    My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    lineiterator doesn't come with as a component in any library, i just invented it for the use of acting as a line and as a iterator in the polygon, the iterator would point to a specific corner in the polygon (and iterate to next corner with ++ operator) while the line part of the interface would provide that corner point first and the next corner point last as two end points of a line.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  9. #9

    Thread Starter
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575
    Thanks - I think I have the bones of a C++ function that will do it.

    Just on another point - if I wanted to place a grid over a polygon and then I just want returned the grid sections that are entirely inside the polygon - do you know how that would work??

    Do you know what I mean?

    Thanks

    Rob
    My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]

  10. #10
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Yep, it's called Bresenham algoritm
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width