IsPointInPolygon2D

From Second Life Wiki

Second Life Wiki > LSL Portal > Library > IsPointInPolygon2D
Jump to: navigation, search

Template:Needs Translation/LSL/de Template:Needs Translation/LSL/es Template:Needs Translation/LSL/el Template:Needs Translation/LSL/fr Template:Needs Translation/LSL/he Template:Needs Translation/LSL/it Template:Needs Translation/LSL/ja Template:Needs Translation/LSL/ko Template:Needs Translation/LSL/nl Template:Needs Translation/LSL/hu Template:Needs Translation/LSL/no Template:Needs Translation/LSL/da Template:Needs Translation/LSL/sv Template:Needs Translation/LSL/tr Template:Needs Translation/LSL/pl Template:Needs Translation/LSL/pt Template:Needs Translation/LSL/ru Template:Needs Translation/LSL/uk Template:Needs Translation/LSL/zh-Hans Template:Needs Translation/LSL/zh-Hant

Contents

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

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");

Deep Notes

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

// 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;
}

Float-pairs

// 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;
}
In other languages