SRL Archive - Bugs

Distance gives the wrong answer


KingKong

2011-05-19 02:53:28

This is the inbuilt function:

function Distance(xs:LongInt; xs:LongInt; xs:LongInt; xs:LongInt):LongInt

It returns an integer instead of an extended type, something like this would be a good one:


function Distance(xs, ys, xe, ye: Extended): Extended;
var
distx, disty: Extended;
begin
distx := Abs(xs - xe);
disty := Abs(ys - ye);

Result:= Sqrt(Sqr(distx) + Sqr(disty));
end;

Naike

2011-05-19 03:17:42

Distance has always returned an integer?

Just rename your function to DistanceEx and use that in your script :)

Markus

2011-05-19 03:48:16

You can leave out the abs if you want to save a few picoseconds, after all squaring always results in a positive number.

Echo_

2011-05-19 15:50:45

Wierd, I was just fooling around with that function today :p

I like this idea though because the hypotenuse will not always be a whole number, and sometimes you want to obtain that absolute decimal value.

KingKong

2011-05-19 18:24:15

Now that you're talking about decimals, how come we don't have a function that rounds a decimal to x digits or something like that?

Daniel

2011-05-20 09:36:45

From: KingKong
Now that you're talking about decimals, how come we don't have a function that rounds a decimal to x digits or something like that?


//Returns the fractional part of the parametre X.
//e.g: X = 1.2, Result := 0.2.
function Frac(const X: Extended): Extended;
begin
Result := X - Int(X);
end;

//Converts parametre X to D decimal places.
function ToDecPla(X: Extended; D: Integer): Extended;
var
n, bX: Extended;
begin
n := Pow(10, d);
bX := x * n;
Result := (Int(bX) + Int(Frac(bX) * 2)) / n;
end;

Taken from my script: http://villavu.com/forum/showthread.php?t=48719 , use at will :)

nielsie95

2011-05-20 09:53:17

You could use Hypot(x2 - x1, y2 - y1).