// LexUtils v0.5
// LTe version
// (c) cdmlex 2013
//__________________________

unit LexUtils;

interface

type
  PInt = ^Integer;

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

  TM2DRealPoint = record
    X, Y : Single;
  end;

  TM2DPoint = record
    X, Y : Integer;
  end;
  PM2DPoint = ^TM2DPoint;

  TM2DRealLine = record
    A, B : TM2DRealPoint;
  end;

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

  TM2DRealObj = record
    P, F : TM2DRealPoint;
    A, S : 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 asmroll(x1, y1 :Real) : Real;
  function roll(var RA : Single; CA, CF : Single): Boolean;

  function minus(A : single) : shortint;

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

  D180P = 180 / Pi;
  c_nul : Integer = 0;

implementation


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 < 0 then
    A := A + 360;
end;

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


function asmroll(x1, y1 :Real) : Real;
asm
  FLD Y1
  FLD X1
  FPATAN
  FWAIT
end;
{
begin
  result:=round(asmroll(x1-x2,y1-y2)*57.32) + 90;
end; }

function roll(var RA : Single; CA, CF : Single): Boolean;
var
  F : Single;
begin
  if trunc(RA / CF) <> trunc(CA / CF) then
  begin
    if ((RA < 90)  and (CA > 270))
    or ((RA < 45)  and (CA > 225))
    or ((RA < 135) and (CA > 315)) then
      F := -CF
    else
    if ((RA > 270) and (CA < 90))
    or ((RA > 225) and (CA < 45))
    or ((RA > 315) and (CA < 135)) then
      F := +CF
    else
    if (RA > CA) then
      F := -CF
    else
    if (RA < CA) then
      F := CF
    else
      F := 0;

    RA := RA + F;
    Result := False;
  end
  else
  begin
    RA := CA;
    Result := True;
  end;
end;

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

end.
