Results 1 to 7 of 7

Thread: Point in a predefined polygon

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    Brighton, England
    Posts
    112

    Question Point in a predefined polygon

    I have a text file containing polygon data for an entire area, ie each polygon represents a zip or postal code area. I have another file which contains a load of co-ordiantes, I need to write a VB app to work out which polygon each set of co-ordinates falls within. ie I need to calculate whether a point lies inside or outside of a polygon

    any help much appreciated

    thanks

    john

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Are the polygons convex?
    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.

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Choose a point outside the polygon, easiest would be say leftmost vertex -1 or if you are sure for instance that the polygon won't be located in origo, put it there for ease of calculations. A straight line (vertical or horizontal) would probably be even better (since this algoritm requires you calculate line intersections)

    for a polygon convex polygon, there will be 1 intersection if the point is inside, 0 or 2 if not. You do this by testing each line in the polygon if it intersects with the line, this would probably be fastest if the line is vertical or horizontal

    for a concave polygon there will be odd amount of intersection if the point is inside, odd if not.
    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.

  4. #4
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    If you weren't really concerned about speed (since it seems like you're determining this for something that's just for information and not speed purposes), you could take your points and create a region from them using CreatePolygonRgn(), and then test a given point by using PtInRgn() and giving it the handle to the region you just created. If your points are just x,y values this would be pretty easy to do. If the regions are complex though, what Kedaman mentioned would be better since it needs only the points of the polygon. I remembered seeing that before, so I went looking and I found it. The article id is Q121960 in MSDN, and titled "Alternative to PtInRegion() for Hit-Testing."
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    PtinRegion will just eat up resources for you The regions might either get inprecise or huge bastards if you don't keep them in reasonable scale
    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.

  6. #6
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575
    Here is a C function to determine if a point lies within a polygon:
    Code:
    c=0;
                    for (i = 0, j = nbpol-1; i < nbpol; j = i++) {
                            if ((((tY[i]<=Y) && (Y<tY[j])) || ((tY[j]<=Y) && (Y<tY[i]))) &&
                            (X < (tX[j] - tX[i]) * (Y - tY[i]) / (tY[j] - tY[i]) + tX[i]))
                             c = !c;
                    }
    where tX and tY contain the points of the polygon, nbpol is the number of points, X and Y are the point.

    if c is true the point is within.

    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]

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    Brighton, England
    Posts
    112
    Thanks guys for all your replies, much appreciated, I'll start playing and see how I get on

    cheers


    john

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