SRL Archive - RS3 Help

using Tbox.isPointInside


fady

2015-04-28 09:33:39

So I want to make a script that checks whether my mouse position is in multiple boxes. I'm trying to do it without having to do long if statements.

in this example by Tboxes are labeled: North, South, East, West, NEast, NWest, SEast, SWest.

I need help making a script that will check which box my mouse is in.

So far this is what I could come up with:
procedure whichBox();
var
x, y: Integer;
MPos: Tpoint;
North, South, East, West, NEast, NWest, SEast, SWest: Tbox;
begin
getMousePos(x, y);
MPos.create(x, y);

//Here Id define all the TBoxes


if North.isPointInside(MPos) then
writeLn('Mouse is North')
else
if South.isPointInside(MPos) then
writeLn('Mouse is South')
else
if East.isPointInside(MPos) then
writeLn('Mouse is East');

//And so on....

end;

I was wondering if there was a cleaner way to do this. I tried to make a case statement for it, but I couldn't figure it out.

Thanks

Smidqe

2015-04-28 10:01:22

Arrays are a way to go in this situation.

Here is a example how it would work.
procedure whichBox();
var
i, r : integer;
boxes : array [0..7] of TBox; //length = 8;
MPos : TPoint;
begin
getmousePos(MPos.x, MPos.y); //putting the mouse position directly to the MPos, no need for TPoint.create() function

//Box array defining here
(*
example::
boxes[0].setBounds(0, 0, 1, 1);
boxes[1].setBounds(0, 0, -1, -1);

and so forth...
*)

for i := 0 to high(boxes) do
if boxes[i].isPointInside(MPos) then
begin
r := i; //if the point is inside it puts the index number to r and breaks out of the loop
break();
end;

case r of //depending of the value of r it prints a different message, you can also turn this procedure to function easily.
0 : writeln('Mouse is north');
1 : writeln('Mouse is south');
//and so forth...
end;
end;

Richard

2015-04-28 17:14:00

Are the boxes static and of uniform size? If so, you might be able to calculate which box it is in without having to iterate

fady

2015-04-28 23:30:13

From: Smidqe
Arrays are a way to go in this situation.

Here is a example how it would work.
procedure whichBox();
var
i, r : integer;
boxes : array [0..7] of TBox; //length = 8;
MPos : TPoint;
begin
getmousePos(MPos.x, MPos.y); //putting the mouse position directly to the MPos, no need for TPoint.create() function

//Box array defining here
(*
example::
boxes[0].setBounds(0, 0, 1, 1);
boxes[1].setBounds(0, 0, -1, -1);

and so forth...
*)

for i := 0 to high(boxes) do
if boxes[i].isPointInside(MPos) then
begin
r := i; //if the point is inside it puts the index number to r and breaks out of the loop
break();
end;

case r of //depending of the value of r it prints a different message, you can also turn this procedure to function easily.
0 : writeln('Mouse is north');
1 : writeln('Mouse is south');
//and so forth...
end;
end;


Thank you very much!! that works great!!



From: Richard
Are the boxes static and of uniform size? If so, you might be able to calculate which box it is in without having to iterate


They are static, but they aren't all the same size. However, I am curious how this could be done!

Richard

2015-04-29 11:17:59

From: fady
They are static, but they aren't all the same size. However, I am curious how this could be done!


Might be able to work without uniform sizes, I'm sure the maths would be possible. The below is a basic example of how I'd attempt to do it:

function whichBox(): TBox;
var
mPos: TPoint;
//2D array as we're working with two dimensions
boxes: array [0..2] of array [0..2] of TBox;
i,h: integer;
begin
//box declaration:
//box[0][0].setBounds(0,0,10,10);
//etc.

getMousePos(mPos.x, mPos.y);

result := boxes[mPos.x div 10][mPos.y div 10];
end;

This example would be using an area of 30x30 (so 10x10 for each box). Imagine the mouse was at (5,5), then performing a div on both these numbers would result in (0,0), and so box[0][0] (top left).

Another example, mouse at (14,23). Div would give (1,2), so box[1][2] (middle right).

EDIT, in terms of efficiency, this is O(1), as opposed to O(n), so it's considerably more desirable


(note: haven't programmed simba in about 5 years, so no idea if this is syntactically sound)