Distance

From SCAR Divi Manual
Revision as of 00:13, 28 June 2011 by Freddy (talk | contribs) (Created page with "==Definition== <source lang="scar" lines="false"> function Distance(x1, y1, x2, y2: Integer): Integer; </source> ===Source Code=== <source lang="scar"> function Distance(x1, y1,...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Definition

function Distance(x1, y1, x2, y2: Integer): Integer;

Source Code

function Distance(x1, y1, x2, y2: LongInt): Integer;
var
  a, b: Int64;
  c, d: LongInt;
  f: Extended;
begin
  c := x1 - x2;
  d := y1 - y2;
  try
    Result := Round(Sqrt(c * c + d * d));
  except
    a := x1 - x2;
    b := y1 - y2;
    f := (a * a + b * b) / 1000000000000000000;
    Result := Round(Sqrt(f) * 1000000000);
  end;
end;

Availability

SCAR Divi 3.00 > Current

Description

Calculates the distance between 2 given points defined by (x1, y1) and (x2, y2). The result of the calculation is rounded. In case of coordinates that exceed the 32 bits integer value size in the calculation, before grabbing the square root in the calculation, the value is divided by 10E18 after which it's square root is multiplied by 10E9. This workaround might impact the accuracy of the calculations because of the accuracy of floating point memory storage, but due to the result being rounded this will have little or no effect.

Example

begin
  WriteLn(Distance(5, 6, 8, 9));
end.

Output:

4