# BBC BASIC Programmers' Reference

### Site Tools

point-in-polygon_20hit_20testing

# Differences

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

 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 2018/04/13 15:53 richardrussell Added syntax highlighting2018/03/31 13:19 external edit 2018/04/13 15:53 richardrussell Added syntax highlighting2018/03/31 13:19 external edit 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: + + 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: + + 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: + + 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):\\ + ​ + + 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): + + 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**:​\\ + ​ + + Here the coordinates are assumed to be contained within DATA statements.\\ \\  Finally, here is the code of **PROCassemblepip**:​ + + 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.txt · Last modified: 2018/04/13 15:53 by richardrussell

### Page Tools 