point-in-polygon_20hit_20testing
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| point-in-polygon_20hit_20testing [2018/03/31 13:19] – external edit 127.0.0.1 | point-in-polygon_20hit_20testing [2024/01/05 00:21] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| 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:\\ | + | </ |
| + | |||
| + | 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:\\ | + | </ |
| + | |||
| + | This is directly equivalent to (but much faster than) this code using the Windows API: | ||
| + | |||
| + | <code bb4w> | ||
| SYS " | SYS " | ||
| SYS " | SYS " | ||
| SYS " | SYS " | ||
| - | 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):\\ | + | </ |
| + | |||
| + | 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%, | READ pt{(V%)}.x%, | ||
| NEXT | NEXT | ||
| - | Here the coordinates are assumed to be contained within DATA statements.\\ \\ Finally, here is the code of **PROCassemblepip**: | + | </ |
| + | |||
| + | 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 | ||
| + | </ | ||
point-in-polygon_20hit_20testing.1522502374.txt.gz · Last modified: 2024/01/05 00:17 (external edit)