Difference between revisions of "IsPointInPolygon2D"

From Second Life Wiki
Jump to navigation Jump to search
Line 15: Line 15:


= Implementations =
= Implementations =
== Copyright ==
This implementation is adapted from one Copyright (c) 1970-2003, Wm. Randolph Franklin (see [http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html here]. Please retain the comments above the function if used.
== Vectors ==
== Vectors ==
<lsl>integer isPointInPolygon2D(vector point, list polygon) {
<lsl>// Original C code Copyright (c) 1970-2003, Wm. Randolph Franklin, ported to LSL by Haravikk Mistral
integer isPointInPolygon2D(vector point, list polygon) {
     integer l = polygon != [];  
     integer l = polygon != [];  
     integer i = 0; integer j = l - 1; integer inPoly = FALSE;  
     integer i = 0; integer j = l - 1; integer inPoly = FALSE;  
Line 38: Line 42:


== Float-pairs ==
== Float-pairs ==
<lsl>integer isPointInPolygon2D(float x, float y, list polygon) {
<lsl>// Original C code Copyright (c) 1970-2003, Wm. Randolph Franklin, ported to LSL by Haravikk Mistral
integer isPointInPolygon2D(float x, float y, list polygon) {
     integer l = polygon != [];
     integer l = polygon != [];
     integer i = 0; integer j = l - 2; integer inPoly = FALSE;
     integer i = 0; integer j = l - 2; integer inPoly = FALSE;

Revision as of 05:19, 1 June 2009

Summary

Function: integer isPointInPolygon2D( vector point, list polygon );

Tests a polygon (described as a list of 2D vectors) to see if a 2D point lies within it. Useful for detecting where an event occurred within 2D space.
Returns an integer TRUE if the point lies within the described polygon, FALSE if not

• vector point The point to test for, described by a 2D vector (a vector using only the x and y values)
• list polygon A polygon described as a list of 2D vectors for its points

It is possible (and fairly easy) to adapt this function to use a list of paired floats, both implementations are provided.

Examples

<lsl>if (isPointInPolygon2D(<0.5,0.5,0>, [<0,0,0>, <0,1,0>, <1,1,0>, <1,0,0>])) llOwnerSay("Point is in polygon");

else llOwnerSay("Point is not in polygon");</lsl>

Implementations

Copyright

This implementation is adapted from one Copyright (c) 1970-2003, Wm. Randolph Franklin (see here. Please retain the comments above the function if used.

Vectors

<lsl>// Original C code Copyright (c) 1970-2003, Wm. Randolph Franklin, ported to LSL by Haravikk Mistral integer isPointInPolygon2D(vector point, list polygon) {

   integer l = polygon != []; 
   integer i = 0; integer j = l - 1; integer inPoly = FALSE; 
   vector iv; vector jv; float diffY;
   while (i < l) { 
       iv = llList2Vector(polygon, i); 
       jv = llList2Vector(polygon, j); 
    
       diffY = (jv.y - iv.y); 
       if (diffY == 0.0) diffY = 1000.0; 
    
       if ((((iv.y <= point.y) && 
           (point.y < (jv.x - iv.x) * (point.y - iv.y) / diffY + iv.x)))) 
           inPoly = !inPoly; 
        
       j = i++;
   }
   return inPoly != 0;

}</lsl>

Float-pairs

<lsl>// Original C code Copyright (c) 1970-2003, Wm. Randolph Franklin, ported to LSL by Haravikk Mistral integer isPointInPolygon2D(float x, float y, list polygon) {

   integer l = polygon != [];
   integer i = 0; integer j = l - 2; integer inPoly = FALSE;
   float ix; float iy; float jx; float jy; float diffY;
   while (i < l) {
       ix = llList2Float(polygon, i);
       iy = llList2Float(polygon, ++i);
       jx = llList2Float(polygon, j);
       jy = llList2Float(polygon, ++j);
    
       diffY = (jy - iy); 
       if (diffY == 0.0) diffY = 1000.0; 
    
       if ((((iy <= y) && 
           (y < (jx - ix) * (y - iy) / diffY + ix)))) 
           inPoly = !inPoly; 
        
       j = i++;
   }
   return inPoly != 0;

}</lsl>