BBC BASIC for Windows
Programming >> Graphics and Games >> How to check if a point is inside a polygon?
http://bb4w.conforums.com/index.cgi?board=graphics&action=display&num=1389242693

How to check if a point is inside a polygon?
Post by knudvaneeden on Jan 9th, 2014, 03:44am

Hi,

How to check if a point is inside a polygon?
http://goo.gl/T6vhHS

with friendly greetings,
Knud van Eeden


Re: How to check if a point is inside a polygon?
Post by admin on Jan 9th, 2014, 03:52am

on Jan 9th, 2014, 03:44am, knudvaneeden wrote:
How to check if a point is inside a polygon?

Whenever I've wanted to do that I've used the CreatePolygonRegion and PtInRegion Windows API functions:

http://msdn.microsoft.com/en-us/library/windows/desktop/dd183512.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/dd162883.aspx

I don't know how efficient that approach is, but it's simple to use and has always been fast enough for my purposes (I make heavy use of it in my Quadrant Editor application which is part of my suite of Colour Recovery programs).

Richard.
Re: How to check if a point is inside a polygon?
Post by knudvaneeden on Jan 9th, 2014, 08:09am

Would it be possible to show a simplest example in BBC Basic using that API
PtInRegion()?

Thanks

Re: How to check if a point is inside a polygon?
Post by admin on Jan 9th, 2014, 09:17am

on Jan 9th, 2014, 08:09am, knudvaneeden wrote:
Would it be possible to show a simplest example in BBC Basic using that API PtInRegion()?

Here's a simple program which changes the shape of the mouse pointer depending on whether it is inside or outside the polygon:

Code:
      DIM Poly{(4) x%, y%}
      DATA 650, 680, 440, 470, 800, 230, 1300, 640, 930, 600
      FOR I% = 0 TO 4
        READ Poly{(I%)}.x%, Poly{(I%)}.y%
      NEXT

      MOVE Poly{(0)}.x%, Poly{(0)}.y%
      FOR I% = 1 TO 4
        DRAW Poly{(I%)}.x%, Poly{(I%)}.y%
      NEXT
      DRAW Poly{(0)}.x%, Poly{(0)}.y%

      SYS "CreatePolygonRgn", Poly{(0)}, DIM(Poly{()},1)+1, 1 TO hrgn%

      REPEAT
        MOUSE X%, Y%, B%
        SYS "PtInRegion", hrgn%, X%, Y% TO inside%
        IF inside% MOUSE ON 137 ELSE MOUSE ON 0
        WAIT 1
      UNTIL FALSE 

Another use of the function can be found in the Rosetta Code example Honeycombs:

http://rosettacode.org/wiki/Honeycombs#BBC_BASIC

Richard.