# Talk:IsPointInPolygon2D

**Very nice**, would be superb if the creator could put it under the geometric library, but I am not putting anyone under pressure to do so. Just would be better in my honest opinion if all these useful functions would be put under a general heading instead of splattered into their own pages. --Nexii Malthus 16:11, 18 August 2008 (PDT)

- It can be included but not until it has been properly attributed and the license fulfilled. It is an illegal (since the licensing restriction were ignored) port of PNPOLY. I've written a new port from the original C code, I just haven't tested it yet. --
**Strife**^{(talk|contribs)}13:37, 19 August 2008 (PDT)

```
//Copyright (c) 1970-2003, Wm. Randolph Franklin
//Copyright (c) 2008, Strife Onizuka (porting to LSL)
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimers.
// 2. Redistributions in binary form must reproduce the above copyright
// notice in the documentation and/or other materials provided with the
// distribution.
// 3. The name of W. Randolph Franklin may not be used to endorse or promote
// products derived from this Software without specific prior written
// permission.
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
integer isPointInPolygon2D(list vertx, list verty, float testx, float testy)
{//Copyright (c) 1970-2003, Wm. Randolph Franklin; 2008, Strife Onizuka
integer i = ~(polygon != []);
integer c = 0;
if(i < -2)
{
float vxi = llList2Float(vertx, -1);
float vyi = llList2Float(verty, -1);
float vyj;
do{
float vxj = vxi;
vxi = llList2Float(vertx, i);
if(((vyi = llList2Float(verty, i)) > testy) ^ ((vyj = vyi) > testy))
{
if(vyj != vyi)
c = c ^ (testx < (((vxj - vxi) * (testy - vyi) / (vyj - vyi)) + vxi));
else//working around lack of infinity in LSL
c = c ^ (0 < ((vxj-vxi) * (testy-vyi)));
}
}while (++i);
}
return c;
}
integer isPointInPolygon2D(list vert, vector test)
{//Copyright (c) 1970-2003, Wm. Randolph Franklin; 2008, Strife Onizuka
integer i = ~(polygon != []);
integer c = 0;
if(i < -2)
{
vector vi = llList2Vector(vert, -1);
do {
vector vj = vi;
vi = llList2Vector(vert, i);
if((vi.y > test.y) ^ (vj.y > test.y))
{
if(vj.y != vi.y)
c = c ^ (test.x < (((vj.x - vi.x) * (test.y - vi.y) / (vj.y - vi.y)) + vi.x));
else//working around lack of infinity in LSL
c = c ^ (0 < ((vj.x-vi.x) * (test.y-vi.y)));
}
} while (++i);
}
return c;
}
```

- Ok, coo, added isPointInPolygon2D( list vert, vector test ) into Geometric library as well as a isLineSegIntersectingPolygon2D function from softsurfer.com, added a new section for polygons and one for handling licenses effectively. Finally, added a small note in left bottom corner wether geometric function is 2D or 3D based for ease. --Nexii Malthus 06:53, 11 December 2008 (UTC)

## Correcting Library Scripts?

(= the front page was linked in the forums, and I decided to dig into this one... the version on the main page may be wrong (iy <= y instead of iy > y != jy > y)... **should I be updating the page or no? are library pages fair game, or do they fall under personal page guidelines?** tested versus Strife's untested port (that I found after my port)... doesn't need x/0 protection, because the first test short circuits it anyway (one must be greater and the other less than the text value, ie impossible to be equal) ... it also properly reports false for lists under 2 points for the poly (for every test I could think of), so that test wasn't needed. oh and to my surprise, the if run faster overall compared to assignment in the second statement. **it might bear mentioning**: the polygon list of vertices seems to be expected in particular order, ie around the edge (CW or CCW doesn't seem to matter), or possibly only expect triangle composites (it doesn't say, I didn't test, but it could be inferred from the examples, and it looks like 0,0 shouldn't be a vertice in that same inference

-- **Void** ^{(talk|contribs)} 11:00, 3 June 2009 (UTC)

```
integer uPointInPolygon2D( list vLstPolygon, vector vPosTesting ){
integer vBooInPlygn;
integer vIntCounter = ([] != vLstPolygon);
vector vPosVertexA = llList2Vector( vLstPolygon, vIntCounter );
vector vPosVertexB;
while (vIntCounter){
vPosVertexB = vPosVertexA;
vPosVertexA = llList2Vector( vLstPolygon, ++vIntCounter );
if ((vPosVertexA.y > vPosTesting.y) ^ (vPosVertexB.y > vPosTesting.y)){
if (vPosTesting.x < (vPosVertexB.x - vPosVertexA.x) * (vPosTesting.y - vPosVertexA.y) /
`(vPosVertexB.y - vPosVertexA.y) + vPosVertexA.x ){
vBooInPlygn = !vBooInPlygn;
}
}
}
return vBooInPlygn;
}
integer uPointInPolygon2D_XY( list vLstPolygonXY, float vFltTesting_X, float vFltTesting_Y ){
integer vBooInPlygnXY;
integer vIntCounterXY = ([] != vLstPolygonXY);
float vFltVertexA_X = llList2Float( vLstPolygonXY, -2 );
float vFltVertexA_Y = llList2Float( vLstPolygonXY, -1 );
float vFltVertexB_X;
float vFltVertexB_Y;
while (vIntCounterXY){
vFltVertexB_X = vFltVertexA_X;
vFltVertexB_Y = vFltVertexA_Y;
vFltVertexA_X = llList2Float( vLstPolygonXY, vIntCounterXY++ );
vFltVertexA_Y = llList2Float( vLstPolygonXY, vIntCounterXY++ );
if ((vFltVertexA_Y > vFltTesting_Y) ^ (vFltVertexB_Y > vFltTesting_Y)){
if (vFltTesting_X < (vFltVertexB_X - vFltVertexA_X) * (vFltTesting_Y - vFltVertexA_Y) /
`(vFltVertexB_Y - vFltVertexA_Y) + vFltVertexA_X ){
vBooInPlygnXY = !vBooInPlygnXY;
}
}
}
return vBooInPlygnXY;
}
/*//-- LSL Port 2009 Void Singer --//*/
/*//-- Copyright (c) 1970-2003, Wm. Randolph Franklin --//*/
/*
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimers.
2. Redistributions in binary form must reproduce the above copyright notice
in the documentation and/or other materials provided with the distribution.
3. The name of W. Randolph Franklin may not be used to endorse or promote
products derived from this Software without specific prior written permission.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
```

- Below is Haravikk's original, included here as am archival note, because I didn't get any feedback to the change, and didn't want to leave an incorrect version. please feel free to revert if needed

--**Void**^{(talk|contribs)}06:09, 9 December 2009 (UTC)

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