Difference between revisions of "Distance"

From SCAR Divi Manual
Jump to: navigation, search
(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,...")
 
(Definition)
Line 8: Line 8:
 
function Distance(x1, y1, x2, y2: LongInt): Integer;
 
function Distance(x1, y1, x2, y2: LongInt): Integer;
 
var
 
var
   a, b: Int64;
+
   i1, i2: Integer;
   c, d: LongInt;
+
   ii1, ii2: Int64;
   f: Extended;
+
   t: Extended;
 
begin
 
begin
   c := x1 - x2;
+
   i1 := x1 - x2;
   d := y1 - y2;
+
   i2 := y1 - y2;
 
   try
 
   try
     Result := Round(Sqrt(c * c + d * d));
+
     Result := Round(Sqrt(i1 * i1 + i2 * i2));
 
   except
 
   except
     a := x1 - x2;
+
     ii1 := x1 - x2;
     b := y1 - y2;
+
     ii2 := y1 - y2;
     f := (a * a + b * b) / 1000000000000000000;
+
     t := (ii1 * ii1 + ii2 * ii2) / 1000000000000000000;
     Result := Round(Sqrt(f) * 1000000000);
+
     Result := Round(Sqrt(t) * 1000000000);
 
   end;
 
   end;
 
end;
 
end;

Revision as of 23:14, 27 June 2011

Definition

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

Source Code

function Distance(x1, y1, x2, y2: LongInt): 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