User Tools

Site Tools


point-in-polygon_20hit_20testing

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

point-in-polygon_20hit_20testing [2018/03/31 13:19]
127.0.0.1 external edit
point-in-polygon_20hit_20testing [2018/04/13 15:53] (current)
richardrussell Added syntax highlighting
Line 1: Line 1:
 =====Point-in-Polygon hit testing===== =====Point-in-Polygon hit testing=====
  
-//by Richard Russell, September 2010//\\ \\  A common requirement in graphics programming is to determine whether a point is inside or outside a polygon (which can be regular or irregular, concave or convex). Since any closed curve can be approximated by a polygon, given a sufficient number of vertices, this can be extended to testing whether a point is inside or outside an arbitrary boundary.\\ \\  One approach is to use the **Windows API**. The polygon can be converted to a //region// using the **CreatePolygonRgn** API and the hit test then carried out using the **PtInRegion** API; however this is a relatively slow operation. This article presents an alternative method using an assembly language routine to perform the test directly.\\ \\  To use this method the code must first be assembled, during the initialisation phase of your program, by calling **PROCassemblepip** as follows:\\ +//by Richard Russell, September 2010//\\ \\  A common requirement in graphics programming is to determine whether a point is inside or outside a polygon (which can be regular or irregular, concave or convex). Since any closed curve can be approximated by a polygon, given a sufficient number of vertices, this can be extended to testing whether a point is inside or outside an arbitrary boundary.\\ \\  One approach is to use the **Windows API**. The polygon can be converted to a //region// using the **CreatePolygonRgn** API and the hit test then carried out using the **PtInRegion** API; however this is a relatively slow operation. This article presents an alternative method using an assembly language routine to perform the test directly.\\ \\  To use this method the code must first be assembled, during the initialisation phase of your program, by calling **PROCassemblepip** as follows: 
 + 
 +<code bb4w>
         PROCassemblepip         PROCassemblepip
-Once that has been done the test can be carried out simply by making the following **SYS** call:\\ +</​code>​ 
 + 
 +Once that has been done the test can be carried out simply by making the following **SYS** call: 
 + 
 +<code bb4w>
         SYS `PtInPoly`, pt{(0)}, nvert%, x%, y% TO res%         SYS `PtInPoly`, pt{(0)}, nvert%, x%, y% TO res%
-This is directly equivalent to (but much faster than) this code using the Windows API:\\ +</​code>​ 
 + 
 +This is directly equivalent to (but much faster than) this code using the Windows API: 
 + 
 +<code bb4w>
         SYS "​CreatePolygonRgn",​ pt{(0)}, nvert%, 1 TO hrgn%         SYS "​CreatePolygonRgn",​ pt{(0)}, nvert%, 1 TO hrgn%
         SYS "​PtInRegion",​ hrgn%, x%, y% TO res%         SYS "​PtInRegion",​ hrgn%, x%, y% TO res%
         SYS "​DeleteObject",​ hrgn%         SYS "​DeleteObject",​ hrgn%
-In both cases **pt{()}** is an array of POINT structures containing the vertices of the polygon, **nvert%** is the total number of vertices in the polygon and **x%,y%** defines the point to be tested. The returned value **res%** is set non-zero if the point is inside the polygon and zero if it is outside.\\ \\  The array of POINT structures can typically be created as follows (in this case for a hexagon):\\ +</​code>​ 
 + 
 +In both cases **pt{()}** is an array of POINT structures containing the vertices of the polygon, **nvert%** is the total number of vertices in the polygon and **x%,y%** defines the point to be tested. The returned value **res%** is set non-zero if the point is inside the polygon and zero if it is outside.\\ \\  The array of POINT structures can typically be created as follows (in this case for a hexagon): 
 + 
 +<code bb4w>
         nvert% = 6         nvert% = 6
         DIM pt{(nvert%-1) x%,y%}         DIM pt{(nvert%-1) x%,y%}
Line 16: Line 30:
           READ pt{(V%)}.x%,​ pt{(V%)}.y%           READ pt{(V%)}.x%,​ pt{(V%)}.y%
         NEXT         NEXT
-Here the coordinates are assumed to be contained within DATA statements.\\ \\  Finally, here is the code of **PROCassemblepip**:​\\ +</​code>​ 
 + 
 +Here the coordinates are assumed to be contained within DATA statements.\\ \\  Finally, here is the code of **PROCassemblepip**:​ 
 + 
 +<code bb4w>
         DEF PROCassemblepip         DEF PROCassemblepip
         LOCAL L%, P%, code%, pass%         LOCAL L%, P%, code%, pass%
Line 76: Line 94:
         NEXT pass%         NEXT pass%
         ENDPROC         ENDPROC
 +</​code>​
point-in-polygon_20hit_20testing.txt · Last modified: 2018/04/13 15:53 by richardrussell