// LexUtils v0.46
// LTe version
// (c) cdmlex 2010
//__________________________

unit LexUtils;

interface

type
  TMRect = record
    X, Y, H, W : word;
  end;

  TM2DRealPoint = record
    X, Y : Single;
  end;

  TM2DPoint = record
    X, Y : Integer;
  end;

  TM2DRealLine = record
    A, B : TM2DRealPoint;
  end;

  TM2DObj = record
    X, Y : Integer;
    R, M : Single;
  end;

  TM2DRealObj = record
    P, F : TM2DRealPoint;
    A, S : Single;
  end;
 {
  CL2DRealObj = class
    P,
    S,
    M : TM2DRealPoint;
    R : Single;
    procedure Move;
    procedure SetM(inR, pM : Single);
  end;   }

  function Set2DRealPoint(X, Y : Single) : TM2DRealPoint;
  function Set2DPoint(X, Y : Integer) : TM2DPoint;
  procedure M2DPoint_App(var A : TM2DRealPoint; B : TM2DRealPoint);

function  Int2S    (I : integer)              : string;
function  S2Int    (S : string)               : Integer;
function  FileEx   (FileName : string)        : Boolean;
procedure App      (var X : Single; I : Single = 1);
function  S2Bool   (S  : string)              : boolean;
function  Bool2Byte(B : Boolean) : Byte;

function PointDistance2P(A, B : TM2DRealPoint) : Single;
function PointDistanceXY(X, Y : Single) : Single;

procedure NormAngle(var A : Integer); overload;
procedure NormAngle(var A : Single); overload;

//function Max(A, B : Single) : Single;
function roll(RA, CA : Single): Single;

function minus(A : single) : shortint;

function LineAngleXY(X, Y : Single) : Single ;

const
  P180 = Pi / 180;  // Для ускорения просчитаем зарание

const
  D180P = 180 / Pi;

implementation
var
  Vrndz : Single = 5;

function Int2S(I : integer): string;
begin
  Str(I, result);
end;

function S2Int(S : string): integer;
var
  O : integer;
begin
  Val(S, result, O);
  if O <> 0 then
  begin
    result := 0;
  end;
end;

function Set2DRealPoint(X, Y : Single) : TM2DRealPoint;
begin
  Result.X := X;
  Result.Y := Y;
end;

function Set2DPoint(X, Y : Integer) : TM2DPoint;
begin
  Result.X := X;
  Result.Y := Y;
end;

procedure M2DPoint_App(var A : TM2DRealPoint; B : TM2DRealPoint);
begin
  A.X := A.X + B.X;
  A.Y := A.Y + B.Y;
end;

function FileEx(FileName : string)        : Boolean;
var
F : textfile;
begin
  Assignfile(F, FileName);
  {$i-}
  Reset(F);
  CloseFile(F);
  {$i+}
  if ioresult = 0 then
    result := true
  else
    result := false;
end;

procedure App(var X : Single; I : Single = 1);
begin
  X := X + I;
end;

function S2Bool(S : string) : Boolean;
begin
  if S = 'TRUE' then
    Result := TRUE
  else
    Result := FALSE;
end;

function Byte2Bool(B : Byte) : Boolean;
begin
  if B > 0 then
    result := TRUE
  else
    result := FALSE;
end;

function Bool2Byte(B : Boolean) : Byte;
begin
  if B then
    Result := 1
  else
    Result := 0;
end;




function PointDistance2P(A, B : TM2DRealPoint) : Single;
begin
  Result := Sqrt ( Sqr( A.X - B.X ) +
                   Sqr( A.Y - B.Y )   );
end;

function PointDistanceXY(X, Y : Single) : Single;
begin
  Result := Sqrt ( Sqr( X ) +
                   Sqr( Y )   );
end;

procedure NormAngle(var A : Integer);
begin
  if A > 360  then
    A := A - 360
  else
  if A < -360 then
    A := A + 360;
end;

procedure NormAngle(var A : Single); overload;
begin
  if A > 360  then
    A := A - 360
  else
  if A < -360 then
    A := A + 360;
end;


  {
function Max;
begin
  if A > B then
    Result := A
  else
    Result := B;
end;   }

function roll(RA, CA : Single): Single;
const
  CF = 1;
var
  F : Single;
begin
  if trunc(RA) <> trunc(RA) then
  begin
    if (RA < -90) and (CA >  90) then
      F := -CF
    else
    if (RA >  90) and (CA < -90)then
      F := -CF
    else
    if (RA > CA) then
      F := -CF
    else
    if (RA < CA) then
      F := CF
    else
      F := 0;
      
    Result := RA + F;
  end
  else
  Result := RA;
end;

function minus(A : single) : shortint;
begin
  if A < 0 then
    Result := -1
  else
  if A > 0 then
    Result := 1
  else
    Result := 0;
end;

function asmTan2(X1, Y1 : Single) : Single;
asm
  FLD Y1
  FLD X1
  FPATAN
  FWAIT
end;

function LineAngleXY(X, Y : Single) : Single ;
begin
  Result := asmTan2(X, Y) * D180P ;
end;

end.
