SRL Archive - OSR Help

DTMs closest to center screen


Flight

2011-04-19 23:01:03

Is there a simple bit of code already out there that finds the DTM nearest to the center of MS?

Thanks in advance.

i luffs yeww

2011-04-19 23:01:38

MSCX and MSCY are the main screen points.

Search around there.

Flight

2011-04-19 23:52:14

Yes, but the 'searching' is what I'm asking about. I guess since no one's posted anything then no such code exists.

TomTuff

2011-04-20 01:27:23


{***
function GetClosestDTM(DTMs: TIntegerArray; SearchBox: TBox; P: TPoint; var ResultPoint: TPoint): Boolean;
By: TomTuff
Desc: Finds all the DTMs in the TIA within the SearchBox, then finds the closest one
to point "P". Point is stored in ResultPoint as well.
***}
function GetClosestDTM(DTMs: TIntegerArray; SearchBox: TBox; P: TPoint; var ResultPoint: TPoint): Boolean;
var
ATPA: T2DPointArray;
i: Integer;
TPA: TPointArray;
begin
SetLength(ATPA, Length(DTMs));
for i := 0 to High(DTMs) do
FindDTMs(DTMs[i], ATPA[i], SearchBox.X1, SearchBox.Y1, SearchBox.X2, SearchBox.X2);
Result := (Length(TPA) > 0);
if not(Result) then
Exit;
TPA := MergeATPA(ATPA);
SortTPAFrom(TPA, P);
ResultPoint := TPA[0];
end;

;) Use as you wish

i luffs yeww

2011-04-20 01:40:36

What I would do would be just use a for loop. After each iteration, just increase .x2 and .y2 by ten, and decrease .x1 and .y1 by ten, until your search area is too big or whatever.

I think there's a better way to do it but whatever. :p

Flight

2011-04-20 01:43:05

P (TPoint) would be the center of your search box, or do we leave that bank to get defined later in the function?

Looks awesome by the way, I really appreciate it. I'll test it in a while and see what I come up with.

TomTuff

2011-04-20 01:55:05

From: Flight
P (TPoint) would be the center of your search box, or do we leave that bank to get defined later in the function?

Looks awesome by the way, I really appreciate it. I'll test it in a while and see what I come up with.


It's whatever point to find the closest point to, so it doesn't have to be the middle.

Bonfield

2011-04-20 04:16:05

i would use the tpoint of the middle of the screen, create an array of DTMs then sort by length and go from there, i would handle it the same as searching for TPAs

sorry i didnt post any code, but i dont see how that wont work

TomTuff

2011-04-20 04:18:59

From: Bonfield
i would use the tpoint of the middle of the screen, create an array of DTMs then sort by length and go from there, i would handle it the same as searching for TPAs

sorry i didnt post any code, but i dont see how that wont work


You mean like what i did? :p

Bonfield

2011-04-20 04:21:27

yeah, i skimmed over it cant really make sense of it looking on an iphone =]

Flight

2011-04-20 10:56:54

Wouldn't you need to free the DTMs within this procedure?

Kasi

2011-04-20 12:26:45

From: Flight
Wouldn't you need to free the DTMs within this procedure?


the DTM's are loaded externally from the Function so would there be any reason to free them internally?

TomTuff

2011-04-20 12:55:28

From: pur3b100d
the DTM's are loaded externally from the Function so would there be any reason to free them internally?


Yeah what he said


function Example: boolean;
var
DTMa, DTMb: Integer;
begin
DTMa := ...
DTMb := ...
FindClosestDTM
FreeDTM(DTMa);
FreeDTM(DTMb);
end;

Narcle

2011-04-20 13:59:43

I'd keep the DTMs as a global declaration anyways if you use them a lot.

Flight

2011-04-20 20:58:24

Ok, I'll declare the DTMs globally and free them within the function that calls this. Thanks guys, and rep for you TomTuff. :)

I think just one more question. How are you grabbing the point, or even the x/y of the result point, from your function? I've tried this:

Here's your function that I modified:

Function GetClosestDTM(DTMs: TIntegerArray): Boolean;
Var
i: Integer;
TPA: TPointArray;
ResultPoint: TPoint;
ATPA: T2DPointArray;
begin
SetLength(ATPA, Length(DTMs));
for i := 0 to High(DTMs) do
FindDTMs(DTMs[i], ATPA[i], MSX1, MSY1, MSX2, MSY2);
Result := (Length(TPA) > 0);
if not(Result) then
Exit;
TPA := MergeATPA(ATPA);
SortTPAFrom(TPA, Point(MSCX,MSCY));
ResultPoint := TPA[0];
end;

And in another function, I tried to use this to deprive the X/Y from that:


X := GetClosestDTM(RockDTM).ResultPoint.x;
Y := GetClosestDTM(RockDTM).ResultPoint.y;

Edit: Even tried getting the ResultPoint itself, then drawing the x/y from the point, but I keep getting compile errors and it's beyond me why it doesn't work. It's probably a really simple mistake and I'm sure I'll feel really stupid when someone answers it.

Edit2: Nevermind, I did changed the result type that your function outputs and grabbed the point from it, then deprived the x/y from the result's point. I'll test it to see if it works now. :)