Uses mouse,keyboard,crt,graph,tuspace;
const maxd=3000000;
type Scart=record x,y,z:longint;end;
     Sungl=record a,b:real;d:longint;end;
     Coord=record px,py:integer;end;
var {Screen:^array[1..1280,1..1024]of longint;}
    c:record x,y,z:longint;a,b:real;end;
    crd:coord;
    p,p1,p2,p3:Punct;
    speed:real;
    deeph,viewu:real;
    px,py:integer;


function tex(x:longint):string;
var st:string;
begin
str(x,st);tex:=st;
end;

Procedure Info;
begin
with c do begin
outtextxy(2,2,'a:'+tex(round(g(a))));
outtextxy(2,2+textheight('a'),'b:'+tex(round(g(b))));
outtextxy(2,2+textheight('a')*2,'x:'+tex(x));
outtextxy(2,2+textheight('a')*3,'y:'+tex(y));
outtextxy(2,2+textheight('a')*4,'z:'+tex(z));
outtextxy(2,my-textheight('a'),'Speed:'+tex(Round(Speed)));
outtextxy(mx-textwidth('Deeph: '+tex(round(deeph))),my-textheight('a'),'Deeph:'+tex(Round(deeph)));
end;
end;
 function Unghi(a:real):real;
 Begin
 if a>pi then begin a:=a-pi;end;
 if a<-pi then begin a:=a+pi;end;
 Unghi:=a;
 a:=Unghi(a);
 End;

  Procedure SpPixel(p:punct);
Var Pdx,pdz,d:real;
function v(a:real):real;
begin
v:=arctg(ey,deeph)*cos(a);
end;
 Begin
       with p do begin
  dx:=x+c.x;dz:=z+c.z;
  da:=(arctg(dx,dz)-c.a);
   if abs(da)<=viewu then begin
      dy:=y-c.y;pdx:=dx*dx;pdz:=dz*dz;
      db:=(arctg(dy,sqrt(pdx+pdz))-c.b);
      if abs(db)<=v(da) then begin
         d:=sqrt(pdx+pdz+dy*dy);
         if (d>deeph)and(d<maxd) then begin
         px:=round(sin(da)*deeph/cos(da))+ex;
         py:=round(sin(db)*deeph/(cos(db)*cos(da)))+ey;
         putpixel(px,py{,random($FFFFFF)});
         end;
      end;
   end; end;
 End;

 Procedure gCrd(p:punct;s:coord);
Var Pdx,pdz,d:real;
function v(a:real):real;
begin
v:=arctg(ey,deeph)*cos(a);
end;
 Begin
       with p do begin
  dx:=x+c.x;dz:=z+c.z;
  da:=(arctg(dx,dz)-c.a);
   {if abs(da)<=viewu then} begin
      dy:=y-c.y;pdx:=dx*dx;pdz:=dz*dz;
      db:=(arctg(dy,sqrt(pdx+pdz))-c.b);
      {if abs(db)<=v(da) then} begin
         d:=sqrt(pdx+pdz+dy*dy);
        { if (d>deeph)and(d<maxd) then} with s do begin
         px:=round(sin(da)*deeph/cos(da))+ex;
         py:=round(sin(db)*deeph/(cos(db)*cos(da)))+ey;
          end;
      end;
   end; end;
 End;

Procedure SpLine(p1,p2:punct);
const sl=300;
var lx,ly,lz,l:real; cd1,cd2:coord;
 begin
  lx:=p2.x-p1.x;ly:=p2.y-p1.y;lz:=p2.z-p1.z;l:=sqrt(lx+ly+lz);
   gcrd(p1,cd1); gcrd(p2,cd2);
   line(cd1.px,cd1.py,cd2.px,cd2.py);
 end;
 Procedure SetViewU(a:integer);
 Begin
   if a <1 then else begin
  a:=(a-1) mod 80+1;
  viewu:=r(a);
  deeph:=ex*cos(viewu)/sin(viewu);
   end;
 End;

 Procedure Perpend(p1,p2:punct;var p:punct);
Var xy,yz,zx:real;
 begin
  dl:=distp(p1,p2);dx:=p2.x-p1.x;dy:=p2.y-p1.y;dz:=p2.z-p1.z;
  xy:=p1.x*p2.y-p2.x*p1.y;
  yz:=p1.y*p2.z-p2.y*p1.z;
  zx:=p1.z*p2.x-p2.z*p1.x;
    with p do begin
    x:=(dy*xy-dz*zx)/dl;
    y:=(dz*yz-dx*xy)/dl;
    z:=(dx*zx-dy*yz)/dl;
    end;
 end;
begin
init(5);
randomize;
initmouse;
 SetMouseRange(0, 0, 1444, 1444); SetMousePos(720,720);

speed:=1000;
SetViewU(10);

with c do begin x:=0;y:=0;z:=0;a:=0;b:=0;end;
  with p1 do begin x:=-100;y:=0;z:=1000;end;
  with p2 do begin x:=100;y:=0;z:=1000;end;
repeat
repeat
for i:=-10 to 10 do for j:=1 to 15 do for h:= 1 to 15 do
    with p do begin x:=(i)*800;y:=(j)*800;z:=h*800;SpPixel(p);end;
  {  perpend(p1,p2,p3);
    spline(p1,p2);
    spline(o,p3);      }

info;
changepage;
if getmousex=1444 then setmousepos(4,getmousey);
if getmousex=0 then setmousepos(1440,getmousey);
if getmousey=1444 then setmousepos(getmousex,4);
if getmousey=0 then setmousepos(getmousex,1440);
with c do begin
a:=r(getmousex/4-181);
b:=r(getmousey/4-181);
{For i:=1 to 100 do putpixel(ex+i div 2,ey+i*2);}
end;
until (keypressed)or(LeftButtonPressed)or(RightButtonPressed);
if LeftButtonPressed then key:=#80 else if RightButtonPressed then key:=#72 else
key:=readkey;
with c do case key of
#80:begin z:=z-round(cos(a)*cos(b)*speed);x:=x-round(sin(a)*cos(b)*speed);y:=y+round(sin(b)*speed)end;
#72:begin z:=z+round(cos(a)*cos(b)*speed);x:=x+round(sin(a)*cos(b)*speed);y:=y-round(sin(b)*speed)end;
#75:begin x:=x+round(cos(a)*cos(b)*speed);z:=z-round(sin(a)*cos(b)*speed)end;
#77:begin x:=x-round(cos(a)*cos(b)*speed);z:=z+round(sin(a)*cos(b)*speed)end;
'8':b:=b+r(1);'5':b:=b-r(1);
'4':a:=a+r(1);'6':a:=a-r(1);
's':speed:=speed+2;'S':speed:=speed-2;
'u':SetViewU(Round(G(ViewU)+1));'U':SetViewU(Round(G(ViewU)-1));
end;
until key=#27;

closegraph;
donemouse;
end.