Function to see if coordinates are within an area?

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

In the huds I make, I make liberal use the llDetectedTouchST()  function. In general, I then check to see if that touch happened within the area of a button on the HUD texture. I'm curious to see if there is a way to write that function that uses less overhead. I'm almost certain one or more of you will have a better way to do it, given all the smarts I see in this forum. So...

Here is an example of how I use in the touch_start() event:

`if (isVectorInArea(<0.01, 0.19, 0.00>,vTouchedAt,<0.03, 0.14, 0.00>)){llSetLinkColor(iMemory1,vRGBActive,0);} //save1`

And here's my function:

```integer isVectorInArea(vector UL, vector test, vector LR){
if (((test.x >= UL.x && test.x <= LR.x)||(test.x >= LR.x && test.x <= UL.x))
&& ((test.y <= UL.y && test.y >= LR.y)||(test.y <= LR.y && test.y >= UL.y))
){return TRUE;} else {return FALSE;}
}```

Note I prefer to not have to figure out ahead of time if the X an Y in one vector are smaller than the X an Y in the second vector.. .because sometimes a face might be rotated on the prim or mesh, making the 0,0 coordinate in somewhere other than the lower left corner

Anyway... if anyone has a more concise way to accomplish the same thing, I'd appreciate the feedback/suggestion. Thanks.

Edited by Galathir Darkstone
shoddy typing
Share on other sites

I too would love to see a clever way of doing this.

Two things, though: you can replace

if (logic) return TRUE;
else return FALSE;

with

return logic;

And if you use round buttons (or round targets over square buttons) llVecDist() saves a lot of work.

• 1
• 1
Share on other sites

For a couple of buttons on a face I turn coordinates into button numbers:

```integer grid_x = 4;
integer grid_y = 3;

touch_start (integer num) {

vector tpos = llDetectedTouchST(0);

integer button = llFloor(tpos.x * (float)grid_x) + (grid_y - 1 - llFloor(tpos.y*(float)grid_y)) * grid_x + 1;

}```

In this case button 1 is top left and button 12 is bottom right. Easy to handle.

If your face is rotated you can rotate the clickpos.

`tpos=(tpos-<0.5,0.5,0>)*llEuler2Rot(<0,0,90>*DEG_TO_RAD)+<0.5,0.5,0>;`

here it's rotated by 90°

Or - easier - use llDetectedTouchUV instead. The coordinates are not bound to the face but to the uv map here.

If I want a big button in the button grid - well it simply occupies 2 or 4 button #'s then.

Of course your design requires a regular layout that orientates at some kind of grid then.

Edited by Nova Convair
• 1
• 1
Share on other sites

Thanks, KT and  Nova. Both of those are approaches I hadn't considered... and pretty that's pretty much exactly what I hoped for posting here! Time to go poke around and see if I can trim some fat off a script =)