Difference between revisions of "Distance"

From SCAR Divi Manual
Jump to: navigation, search
(Definition)
(Source Code)
Line 6: Line 6:
 
===Source Code===
 
===Source Code===
 
<source lang="scar">
 
<source lang="scar">
function Distance(x1, y1, x2, y2: LongInt): Integer;
+
function Distance(x1, y1, x2, y2: Integer): Integer;
 
var
 
var
 
   i1, i2: Integer;
 
   i1, i2: Integer;

Revision as of 00:16, 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

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