Distance

From SCAR Divi Manual
Revision as of 23:33, 27 June 2011 by Freddy (talk | contribs) (Availability)
Jump to: navigation, search

Definition

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

Source Code

function Distance(x1, y1, x2, y2: Integer): Integer;
var
  i1, i2: Integer;
  ii1, ii2: Int64;
  t: Extended;
begin
  i1 := x1 - x2;
  i2 := y1 - y2;
  try
    Result := Round(Sqrt(i1 * i1 + i2 * i2));
  except
    ii1 := x1 - x2;
    ii2 := y1 - y2;
    t := (ii1 * ii1 + ii2 * ii2) / 1000000000000000000;
    Result := Round(Sqrt(t) * 1000000000);
  end;
end;

Availability

SCAR Divi 3.00 > Current

  • Before SCAR Divi 3.21 the workaround for larger distances was not yet implemented.

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