Difference between revisions of "Distance"

From SCAR Divi Manual
Jump to: navigation, search
(Availability)
(Availability)
Line 28: Line 28:
 
SCAR Divi 3.00 > Current
 
SCAR Divi 3.00 > Current
  
*The workaround to work with larger values was only immplemented in SCAR Divi 3.21.
+
*Before SCAR Divi 3.21 the workaround for larger distances was not yet implemented.
  
 
==Description==
 
==Description==

Revision as of 00:33, 28 June 2011

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