SRL Archive - Research & Development Lounge

Some TBox Functions


Light

2007-09-07 17:43:32

Some TBox functions dan cardin requested.

function OrderTBox(TBoxInput : TBox) : TBox;
function CheckTBoxIntersection(TBox1, TBox2 : TBox) : boolean;
function CollideTBoxs(TBox1, TBox2 : TBox) : TBox;

{*******************************************************************************
function OrderTBox(TBoxInput : TBox) : TBox;
By: Light
Description: Orders a TBox so it's upper left point is (x1,y1) and it's lower
right point is (x2,y1)
*******************************************************************************}

function OrderTBox(TBoxInput : TBox) : TBox;
var returnTBox : TBox;
begin
if(TBoxInput.x1 < TBoxInput.x2)then
begin
returnTBox.x1 := TBoxInput.x1;
returnTBox.x2 := TBoxInput.x2;
end else
begin
returnTBox.x1 := TBoxInput.x2;
returnTBox.x2 := TBoxInput.x1;
end;
if(TBoxInput.y1 < TBoxInput.y2)then
begin
returnTBox.y1 := TBoxInput.y1;
returnTBox.y2 := TBoxInput.y2;
end else
begin
returnTBox.y1 := TBoxInput.y2;
returnTBox.y2 := TBoxInput.y1;
end;
Result := returnTBox;
end;

{*******************************************************************************
function CheckTBoxIntersection(TBox1, TBox2 : TBox) : boolean;
By: Light
Description: Checks if two TBoxs intersect with an area. Two sides touching is
not an intersection.
*******************************************************************************}

function CheckTBoxIntersection(TBox1, TBox2 : TBox) : boolean;
var orderedTBox1, orderedTBox2 : TBox;
begin
orderedTBox1 := OrderTBox(TBox1);
orderedTBox2 := OrderTBox(TBox2);
if((orderedTBox1.x1 >= orderedTBox2.x2)OR(orderedTBox1.x2 <= orderedTBox2.x1)
OR(orderedTBox1.y1 >= orderedTBox2.y2)OR(orderedTBox1.y2 <= orderedTBox2.y1))
then
begin
Result := False;
end else
begin
Result := True;
end;
end;

{*******************************************************************************
function CollideTBoxs(TBox1, TBox2 : TBox) : TBox;
By: Light
Description: Finds the TBox of intersection for two TBoxs. If there is no area
of intersection, then the return TBoxs x1, y1, x2 and y2 are all set to 0.
__________
| |
| |
| _|______
|_______|_| |
| |
| |
|_______|

*******************************************************************************}

function CollideTBoxs(TBox1, TBox2 : TBox) : TBox;
var returnTBox, orderedTBox1, orderedTBox2 : TBox;
begin
if(CheckTBoxIntersection(TBox1, TBox2))then
begin
orderedTBox1 := OrderTBox(TBox1);
orderedTBox2 := OrderTBox(TBox2);
if(orderedTBox1.x1 < orderedTBox2.x1)then
begin
returnTBox.x1 := orderedTBox2.x1;
end else
begin
returnTBox.x1 := orderedTBox1.x1;
end;
if(orderedTBox1.y1 < orderedTBox2.y1)then
begin
returnTBox.y1 := orderedTBox2.y1;
end else
begin
returnTBox.y1 := orderedTBox1.y1;
end;
if(orderedTBox1.x2 < orderedTBox2.x2)then
begin
returnTBox.x2 := orderedTBox1.x2;
end else
begin
returnTBox.x2 := orderedTBox2.x2;
end;
if(orderedTBox1.y2 < orderedTBox2.y2)then
begin
returnTBox.y2 := orderedTBox1.y2;
end else
begin
returnTBox.y2 := orderedTBox2.y2;
end;
end else
begin
returnTBox.x1 := 0;
returnTBox.y1 := 0;
returnTBox.x2 := 0;
returnTBox.y2 := 0;
end;
Result := returnTBox;
end;

Dan Cardin

2007-09-07 17:55:22

why thanks:D!

pwnaz0r

2007-09-07 17:55:38

pretty nice. have you thought about applying to members?

most nonmembers dont even understand tbox, much less make functions like these

Light

2007-09-07 18:05:53

Thanks pwnaz0r! I think I need to make a full script first (correct me if I'm wrong). I'm in the process of creating my first full script, which will also go into my member application.

Hopefully I can get your vote when I do apply :)

mastaraymond

2007-09-07 18:06:29

From: pwnaz0r
pretty nice. have you thought about applying to members?

most nonmembers dont even understand tbox, much less make functions like these
Applying for member with 3 functions, doesn't get you in..

pwnaz0r

2007-09-07 18:16:24

From: mastaraymond
Applying for member with 3 functions, doesn't get you in..


Point being? I didnt say apply with the three functions

Bobarkinator

2007-09-07 23:38:06

Very nice.

NewToAutoing

2007-09-08 02:23:58

Looks awesome, but you don't have to create a TBox to store each result into. Like, you can do this:
function OrderTBox(TBoxInput : TBox) : TBox;
begin
if(TBoxInput.x1 < TBoxInput.x2)then
begin
result.x1 := TBoxInput.x1;
result.x2 := TBoxInput.x2;
end else
begin
result.x1 := TBoxInput.x2;
result.x2 := TBoxInput.x1;
end;
if(TBoxInput.y1 < TBoxInput.y2)then
begin
result.y1 := TBoxInput.y1;
result.y2 := TBoxInput.y2;
end else
begin
result.y1 := TBoxInput.y2;
result.y2 := TBoxInput.y1;
end;
end;

Also, you could save a lot of lines by doing this instead:
function OrderTBox(TBoxInput : TBox) : TBox;
begin
result.x1 := min(TBoxInput.x1, TBoxInput.x2);
result.x2 := max(TBoxInput.x1, TBoxInput.x2);
result.y1 := min(TBoxInput.y1, TBoxInput.y2);
result.y2 := max(TBoxInput.y1, TBoxInput.y2);
end;

There might be another way to do it to, that's even shorter, but i'm not sure. Basically min returns the lesser number, and max returns the greater number. Sorry to shorten up your function, but i just thought i should show you that :p

Pentti

2007-09-08 02:51:39

Good job!
Maybe easier for your eyes:
function CheckTBoxIntersection(TBox1, TBox2 : TBox) : boolean;
var
orderedTBox1, orderedTBox2 : TBox;
begin
Result:= (orderedTBox1.x1 >= orderedTBox2.x2)
or (orderedTBox1.x2 <= orderedTBox2.x1)
or (orderedTBox1.y1 >= orderedTBox2.y2)
or (orderedTBox1.y2 <= orderedTBox2.y1)
end;


function CollideTBoxs(TBox1, TBox2 : TBox) : TBox;
var
returnTBox, orderedTBox1, orderedTBox2 : TBox;
begin
if CheckTBoxIntersection(TBox1, TBox2) then
begin
orderedTBox1 := OrderTBox(TBox1);
orderedTBox2 := OrderTBox(TBox2);

if(orderedTBox1.x1 < orderedTBox2.x1)then
Result.x1 := orderedTBox2.x1 else
Result.x1 := orderedTBox1.x1;

if(orderedTBox1.y1 < orderedTBox2.y1)then
Result.y1 := orderedTBox2.y1 else
Result.y1 := orderedTBox1.y1;

if(orderedTBox1.x2 < orderedTBox2.x2)then
Result.x2 := orderedTBox1.x2 else
Result.x2 := orderedTBox2.x2;

if(orderedTBox1.y2 < orderedTBox2.y2)then
Result.y2 := orderedTBox1.y2 else
Result.y2 := orderedTBox2.y2;
end;
end;
:D

n3ss3s

2007-09-08 05:18:54

Id like to see a script from you

Miitchyy

2007-09-14 20:15:54

function OrderTBox(TBoxInput : TBox) : TBox;
begin
result.x1 := min(TBoxInput.x1, TBoxInput.x2); result.x2 := max(TBoxInput.x1, TBoxInput.x2); result.y1 := min(TBoxInput.y1, TBoxInput.y2); result.y2 := max(TBoxInput.y1, TBoxInput.y2);
end;

Even shorter =]


Very good functions, However as NewToAutoing & Pentti pointed out you have some begin & end which aren't needed.

Light

2007-09-16 16:36:36

Thanks to everyone. I'm not sure if it's good to make it that short kevin wolf… but it could be made better. I definitely will use some everyone's suggestions in my next script and hopefully it'll be even better.

@n3ss3s: Thanks, I don't know when I'll be making a full script… There's a lot of SRL stuff I don't know and still aren't comfortable with. I'm learning and hopefully I'll be able to make a good contribution to SRL in the future.

ShowerThoughts

2007-09-16 16:50:38

ehm what are tbox funtions :p

Nava2

2009-03-21 14:32:25

function OrderTBox(TBoxInput : TBox) : TBox;
begin
Result := IntToBox(Min(TBoxInput.x1, TBoxInput.x2), Max(TBoxInput.x1, TBoxInput.x2), Min(TBoxInput.y1, TBoxInput.y2), Max(TBoxInput.y1, TBoxInput.y2));
end;

mastaraymond

2009-03-21 14:38:40

From: Nava2
function OrderTBox(TBoxInput : TBox) : TBox;
begin
Result := IntToBox(Min(TBoxInput.x1, TBoxInput.x2), Max(TBoxInput.x1, TBoxInput.x2), Min(TBoxInput.y1, TBoxInput.y2), Max(TBoxInput.y1, TBoxInput.y2));
end;

Ouch! Huge gravedig..