Triangle Button

From Second Life Wiki
Jump to navigation Jump to search

Triangle

This is a function to tell if a point is within a 2d triangle.
Code converted from http://www.blackpawn.com/texts/pointinpoly/ to LSL by Richardjrn Weatherwax

<lsl> //Converted to LSL by Richardjrn Weatherwax from http://www.blackpawn.com/texts/pointinpoly/ //Done on request of Ezri Fairy (snow.gravois)

vector pointA = <0.17810, 0.36214, 0.00000>; vector pointB = <0.81144, 0.38803, 0.00000>; vector pointC = <0.50132, 0.71068, 0.00000>;

integer SameSide(vector p1, vector p2, vector a, vector b) {

   vector cp1 = (b-a)%(p1-a);
   vector cp2 = (b-a)%(p2-a);
   if((cp1*cp2)>=0)return TRUE;
   else return FALSE;

}

integer PointInTriangle(vector p, vector a, vector b, vector c) {

   if(SameSide(p,a, b,c)&&SameSide(p,b, a,c)&&SameSide(p,c, a,b))return TRUE;
   else return FALSE;

}

default {

   state_entry()
   {
       llSetTexture("b2977bf6-b9af-c516-2e19-a5c451914227",ALL_SIDES); //Triangle texture to match buttons triangle.
   }
   touch_start(integer total_number)
   {
       if(PointInTriangle(llDetectedTouchST(0),pointA,pointB,pointC))llOwnerSay("In the triangle");
       else llOwnerSay("Outside the triangle");
   }

} </lsl>

Hexagon

An expansion for a hexagon

The hexagon with its points labeled

<lsl> //Converted to LSL by Richardjrn Weatherwax from http://www.blackpawn.com/texts/pointinpoly/ //Done on request of Ezri Fairy (snow.gravois)

vector pointA = <0.34309, 0.74541, 0.00000>; vector pointB = <0.71578, 0.72976, 0.00000>; vector pointC = <0.85278, 0.50606, 0.00000>; vector pointD = <0.69276, 0.25484, 0.00000>; vector pointE = <0.44234, 0.31546, 0.00000>; vector pointF = <0.33040, 0.46631, 0.00000>;

integer SameSide(vector p1, vector p2, vector a, vector b) {

   vector cp1 = (b-a)%(p1-a);
   vector cp2 = (b-a)%(p2-a);
   if((cp1*cp2)>=0)return TRUE;
   else return FALSE;

}

integer PointInHex(vector p, vector a, vector b, vector c, vector d, vector e, vector f) {

   if(SameSide(p,c, a,b)&&SameSide(p,d, b,c)&&SameSide(p,e, c,d)&&SameSide(p,f, d,e)&&SameSide(p,a, e,f)&&SameSide(p,b, f,a))return TRUE;
   else return FALSE;

}

default {

   state_entry()
   {
       llSetTexture("bac72ce8-d6f6-4360-6e24-4652b3ab9f24",ALL_SIDES); //Hexagon texture to match buttons Hexagon.
   }
   touch_start(integer total_number)
   {
       if(PointInHex(llDetectedTouchST(0),pointA,pointB,pointC,pointD,pointE,pointF))llOwnerSay("In the hexagon");
       else llOwnerSay("Outside the hexagon");
   }

} </lsl>