Difference between revisions of "Distance"

From SCAR Divi Manual
Jump to: navigation, search
Line 1: Line 1:
 
==Definition==
 
==Definition==
 
<source lang="scar" lines="false">
 
<source lang="scar" lines="false">
function Distance(const x1, y1, x2, y2: Integer): Integer;
+
function Distance(const X1, Y1, X2, Y2: Integer): Extended;
 
</source>
 
</source>
  
 
===Source Code===
 
===Source Code===
 
<source lang="scar">
 
<source lang="scar">
function Distance(const x1, y1, x2, y2: Integer): Integer;
+
function MyDistance(const X1, Y1, X2, Y2: Integer): Extended;
 
var
 
var
   i1, i2: Integer;
+
   A, B: Extended;
  ii1, ii2: Int64;
 
  t: Extended;
 
 
begin
 
begin
   i1 := x1 - x2;
+
   A := X1 - X2;
   i2 := y1 - y2;
+
   B := Y1 - Y2;
   try
+
   Result := Sqrt(A * A + B * B);
    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;
 
end;
 
</source>
 
</source>
Line 28: Line 19:
 
SCAR Divi 3.00 > Current
 
SCAR Divi 3.00 > Current
  
*Before SCAR Divi 3.21 the workaround for larger distances was not yet implemented.
+
*Before 3.21 the workaround for larger distances was not yet implemented.
 +
*Bedore 3.37 returned an integer value
  
 
==Description==
 
==Description==
Calculates the [http://mathworld.wolfram.com/Distance.html 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.
+
Calculates the [http://mathworld.wolfram.com/Distance.html distance] between 2 given points defined by ('''x1''', '''y1''') and ('''x2''', '''y2''').
  
 
==Example==
 
==Example==
Line 41: Line 33:
  
 
Output:
 
Output:
  4
+
  4.24264068711929
  
 
[[Category:Functions]]
 
[[Category:Functions]]
 
[[Category:Math Functions]]
 
[[Category:Math Functions]]

Revision as of 15:53, 12 November 2012

Definition

function Distance(const X1, Y1, X2, Y2: Integer): Extended;

Source Code

function MyDistance(const X1, Y1, X2, Y2: Integer): Extended;
var
  A, B: Extended;
begin
  A := X1 - X2;
  B := Y1 - Y2;
  Result := Sqrt(A * A + B * B);
end;

Availability

SCAR Divi 3.00 > Current

  • Before 3.21 the workaround for larger distances was not yet implemented.
  • Bedore 3.37 returned an integer value

Description

Calculates the distance between 2 given points defined by (x1, y1) and (x2, y2).

Example

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

Output:

4.24264068711929