PROGRAM VG3;
{\$N+,E+}

USES Graph,Crt;

TYPE
CartType = RECORD
X, Y: Real;
END;

PolarType = RECORD
R, A: Real;
END;

PhysType = RECORD
X, Y: Integer;
END;

CONST
MC : Integer = 1;
MX : Real = 10;
MY : Real = 10;

{* An ArcTan function good for the range from -PI to PI *}

FUNCTION ArcTan2(X,Y: Real): Real;

BEGIN
IF (X = 0) AND (Y = 0) THEN
ArcTan2 := 0
ELSE
IF X = 0 THEN
IF Y > 0 THEN
ArcTan2 :=PI/2
ELSE
ArcTan2 := -PI/2
ELSE
IF X > 0 THEN
ArcTan2 := ArcTan(Y/X)
ELSE
ArcTan2 := ArcTan(Y/X) + PI;
END;

PROCEDURE PolarToCart(P: PolarType; VAR C: CartType);
BEGIN
C.X := Cos(P.A*PI/180) * P.R;
C.Y := SIN(P.A*PI/180) * P.R
END;

PROCEDURE CartToPhys(C: CartType; VAR P: PhysType);
BEGIN
P.X := Trunc((C.X + 1.0) * MX / 2);
P.Y := Trunc((1.0 - C.Y) * MY / 2)
END;

PROCEDURE PolarToPhys(Po: PolarType; VAR Ph: PhysType);
VAR
C: CartType;
BEGIN
PolarToCart(Po,C);
CartToPhys(C,Ph)
END;

PROCEDURE CartToPolar(C: CartType; VAR P: PolarType);
BEGIN
P.R := Sqrt(Sqr(C.X) + Sqr(C.Y));
P.A := ArcTan2(C.X,C.Y) * 180/PI
END;

{\$I VGObject}
{\$I VGInit}
{\$I VGGetDsp}

{************************* M A I N ***************************}

VAR
T : Text;
VgErr : Integer;

BEGIN
ClrScr;
Assign(T,'DRAWING');
Reset(T);
VgErr := VGInit;
IF VgErr <> 0 THEN BEGIN
WriteLn('BGI Initialization error ',VgErr);
Halt(1)
END;
GetDspObjs(T);
Close(T);