SRL Archive - Help and Support

Closest number to x?


Infantry001

2007-02-17 14:51:25

Anyone have a function finding the closest number to "x" given a set of numbers?

Edit: How can i compare which point in a TPointArray is closer to a certain TPoint?

dontpanic

2007-02-17 15:09:12

For 2 numbers:

function aIsCloser(a,b,x:integer):boolean;
begin
Result:=abs(a-x)<abs(b-x);
end;

Infantry001

2007-02-17 15:12:01

Well, actually not what i was looking for., but thanks anyways. I guess i should be more specific about my needs.

Here is my real question: How can i compare which point in a TPointArray is closer to a certain TPoint?

P.S. Abs is absolute value, right?

Dark_Sniper

2007-02-17 15:15:44

P.S. Abs is absolute value, right?


ya… you coulda pressed f1 while in scar the ctrl + f while in browser to find the function :p

Yakman

2007-02-17 15:36:55

try this

declare a var
go though the array
if you find a point closer than that var, store it in var

the value of the var at the end should be the closest
tip: use SRL's function Distance, it works by pythagoras' theorm


var
Closest,f:integer;
ThePoint:TPoint
ThePointArray:TPointArray;

for f:=0 to GetArrayLength(ThePointArray)-1 do
begin
if( Distance(ThePoint.x,ThePoint.y,ThePointArray[f].x,ThePointArray[f].y) < closest )then
Closest := Distance(ThePoint.x,ThePoint.y,ThePointArray[f].x,ThePointArray[f].y);
end
writeln('the closest distance is '+inttostr(closest));

Infantry001

2007-02-17 18:51:57

Thanks, Yakman! Really appreciate it!