{29.8.2003-1.9.2003
Jozef Sivek ml.}
program eli004;
uses graph,crt;
type pole_v=array[1..3] of real;
var  gd,gm,monitor,xb,zb,cspining:integer;{c... su parametre citlivosti pohybu pozorovatela}
     i:char;
     vektorp,vektorpright,vektorpup,P:pole_v;{P=pozorovatel}
     dp,dright,dup,absp,abspright,abspup,cmoveing:real;

procedure nastavenia;
     begin
     setcolor(10);
     setbkcolor(0);
     cmoveing:=0.3;
     cspining:=5;
     vektorp[1]:=0;
     vektorp[2]:=1;
     vektorp[3]:=0;
     vektorpright[1]:=1;
     vektorpright[2]:=0;
     vektorpright[3]:=0;
     vektorpup[1]:=0;
     vektorpup[2]:=0;
     vektorpup[3]:=1;
     P[1]:=0;
     P[2]:=-20;
     P[3]:=0;
     monitor:=1280;
     end;
procedure spinv(zmysel:Shortint);
          var vektor:pole_v;
          pocitadlo:Shortint;
          odchylka:real;
          begin
          if (vektorp[1]+vektorp[2])<>0 then begin
          if cspining<=0 then begin
                              vektorp[1]:=vektorpup[1]*zmysel;
                              vektorp[2]:=vektorpup[2]*zmysel;
                              vektorp[3]:=vektorpup[3]*zmysel;
                              vektorpup[1]:=vektorp[1]*zmysel*(-1);
                              vektorpup[2]:=vektorp[2]*zmysel*(-1);
                              vektorpup[3]:=vektorp[3]*zmysel*(-1);
                              Exit;
                              end;
          vektor[1]:=(vektorpup[1]*zmysel+vektorp[1])*0.707107;
          vektor[2]:=(vektorpup[2]*zmysel+vektorp[2])*0.707107;
          vektor[3]:=(vektorpup[3]*zmysel+vektorp[3])*0.707107;
          if cspining>1 then for pocitadlo:= 2 to cspining do
           begin
           vektor[1]:=(vektor[1]+vektorp[1])/2;
           vektor[2]:=(vektor[2]+vektorp[2])/2;
           vektor[3]:=(vektor[3]+vektorp[3])/2;
           end;
 odchylka:=1/(sqrt(vektor[1]*vektor[1]+vektor[2]*vektor[2]+vektor[3]*vektor[3]));
          vektorp[1]:=vektor[1]*odchylka;
          vektorp[2]:=vektor[2]*odchylka;
          vektorp[3]:=vektor[3]*odchylka;
          vektorpup[1]:=vektorpright[2]*vektorp[3]-vektorp[2]*vektorpright[3];
          vektorpup[2]:=vektorpright[3]*vektorp[1]-vektorp[3]*vektorpright[1];
          vektorpup[3]:=vektorpright[1]*vektorp[2]-vektorp[1]*vektorpright[2];
                               end;
          end;

procedure spinh(zmysel:Shortint);
          var vektor:pole_v;
          pocitadlo:Shortint;
          odchylka:real;
          begin
          if cspining<=0 then cspining:=1;

 if (vektorp[1]+vektorp[2])<>0 then begin
          vektor[1]:=(vektorp[2]*zmysel+vektorp[1])*0.707107;
          vektor[2]:=(-1*vektorp[1]*zmysel+vektorp[2])*0.707107;
          if cspining>1 then for pocitadlo:= 2 to cspining do
           begin
           vektor[1]:=(vektor[1]+vektorp[1])/2;
           vektor[2]:=(vektor[2]+vektorp[2])/2;
           end;
 odchylka:=(sqrt(sqr(vektorp[1])+sqr(vektorp[2])))/(sqrt(sqr(vektor[1])+sqr(vektor[2])));
          vektorp[1]:=vektor[1]*odchylka;
          vektorp[2]:=vektor[2]*odchylka;
                                           end;

          vektor[1]:=(vektorpright[2]*zmysel+vektorpright[1])*0.707107;
          vektor[2]:=(-1*vektorpright[1]*zmysel+vektorpright[2])*0.707107;
          if cspining>1 then for pocitadlo:= 2 to cspining do
           begin
           vektor[1]:=(vektor[1]+vektorpright[1])/2;
           vektor[2]:=(vektor[2]+vektorpright[2])/2;
           end;
 odchylka:=1/(sqrt(sqr(vektor[1])+sqr(vektor[2])));
          vektorpright[1]:=vektor[1]*odchylka;
          vektorpright[2]:=vektor[2]*odchylka;

if (vektorpup[1]+vektorpup[2])<>0 then begin
          vektorpup[1]:=vektorpright[2]*vektorp[3]-vektorp[2]*vektorpright[3];
          vektorpup[2]:=vektorpright[3]*vektorp[1]-vektorp[3]*vektorpright[1];
          vektorpup[3]:=vektorpright[1]*vektorp[2]-vektorp[1]*vektorpright[2];
                                              end;
          end;

procedure d;
     begin
     dp:=-vektorp[1]*P[1]-vektorp[2]*P[2]-vektorp[3]*P[3];
     dright:=-vektorpright[1]*P[1]-vektorpright[2]*P[2]-vektorpright[3]*P[3];
     dup:=-vektorpup[1]*P[1]-vektorpup[2]*P[2]-vektorpup[3]*P[3];
     absp:=sqrt(vektorp[1]*vektorp[1]+vektorp[2]*vektorp[2]+vektorp[3]*vektorp[3]);
     abspright:=sqrt(vektorpright[1]*vektorpright[1]+vektorpright[2]*vektorpright[2]+vektorpright[3]*vektorpright[3]);
     abspup:=sqrt(vektorpup[1]*vektorpup[1]+vektorpup[2]*vektorpup[2]+vektorpup[3]*vektorpup[3]);
     end;

procedure longpx3d;
     begin end;
procedure longline(ax,ay,bx,by,l:integer);
     type pole=array[1..2] of integer;
     var u:pole;
     i:real;
     begin
     u[1]:=bx-ax;
     u[2]:=by-ay;
     i:=l/sqrt( sqr(u[1])+sqr(u[2]) );
     u[1]:=round(u[1]*i);
     u[2]:=round(u[2]*i);
     line(ax,ay,ax+u[1],ay+u[2]);
     end;
     {800-900}

procedure px3d(x,y,z:real);
     var i,lp,lpright,lpup:real;
     begin
     lp:=(vektorp[1]*x+vektorp[2]*y+vektorp[3]*z+dp)/(sqrt(absp));
     if lp>0 then begin

     lpright:=(vektorpright[1]*x+vektorpright[2]*y+vektorpright[3]*z+dright)/(sqrt(abspright));
     lpup:=(vektorpup[1]*x+vektorpup[2]*y+vektorpup[3]*z+dup)/(sqrt(abspup));

     i:=monitor/lp;
     line(round(i*lpright)+320,240-round(i*lpup),round(i*lpright)+320,240-round(i*lpup));
                  end;

     end;

procedure line3d(x,y,z,x2,y2,z2:real);
     var i,lpa,lprighta,lpupa,lp,lpright,lpup:real;
         a,b:integer;
         label skok;
     begin
     lpa:=(vektorp[1]*x+vektorp[2]*y+vektorp[3]*z+dp)/(sqrt(absp));
     lprighta:=(vektorpright[1]*x+vektorpright[2]*y+vektorpright[3]*z+dright)/(sqrt(abspright));
     lpupa:=(vektorpup[1]*x+vektorpup[2]*y+vektorpup[3]*z+dup)/(sqrt(abspup));

     lp:=(vektorp[1]*x2+vektorp[2]*y2+vektorp[3]*z2+dp)/(sqrt(absp));
     if lp<0 then if lpa<0 then goto skok;{!SKOK/JUMP}
     lpright:=(vektorpright[1]*x2+vektorpright[2]*y2+vektorpright[3]*z2+dright)/(sqrt(abspright));
     lpup:=(vektorpup[1]*x2+vektorpup[2]*y2+vektorpup[3]*z2+dup)/(sqrt(abspup));

     if lp>0 then if lpa>0 then begin
     {i:=monitor/lp;}
     a:=round((monitor/lpa)*lprighta)+320;
     b:=240-round((monitor/lpa)*lpupa);
     i:=monitor/lp;
     line(a,b,round(i*lpright)+320,240-round(i*lpup));
                                end;
     skok:;                               {!SKOK/JUMP}
     end;

begin
{-[]-}
gd:=detect;
InitGraph(gd, gm, 'c:/bp/bgi');
if GraphResult <> grOk then Halt(1);
nastavenia;
d;
{-[]-}
repeat
clearviewport;

{line3d(-5,5,-5,5,5,-5);
line3d(-5,5,5,5,5,5);
line3d(5,5,5,5,5,-5);
line3d(-5,5,5,-5,5,-5);
line3d(5,5,5,5,15,5);
line3d(5,5,-5,5,15,-5);
line3d(-5,5,-5,-5,15,-5);
line3d(-5,5,5,-5,15,5);
line3d(-5,15,-5,5,15,-5);
line3d(-5,15,5,5,15,5);
line3d(5,15,5,5,15,-5);
line3d(-5,15,5,-5,15,-5);
}
{line3d(2,2,0,2,-2,0);
line3d(-2,2,0,-2,-2,0);
line3d(2,2,0,-2,2,0);
line3d(2,-2,0,-2,-2,0);
line3d(2,2,0,2,2,-3);
line3d(2,-2,0,2,-2,-3);
line3d(-2,2,0,-2,2,-3);
line3d(-2,-2,0,-2,-2,-3);}
line3d(0,0,0,3,0,0);
line3d(0,0,0,1.5,2.598,0);
line3d(3,0,0,1.5,2.598,0);
line3d(0,0,0,1.5,1.25,3);
line3d(3,0,0,1.5,1.25,3);
line3d(1.5,2.598,0,1.5,1.25,3);





{ }
{->podmienkovy retazec pohybu pozorovatela}
i:=readkey;
case i of
#32: {navra povodnych hodnot}nastavenia;

#72: {otocka hore/spin up}spinv(1);
#80: {otocka dole/spin down}spinv(-1);
#77: {otocka vpravo/spin right}spinh(1);
#75: {otocka vlavo/spin left}spinh(-1);

#54:begin{doprava/right}
    P[1]:=P[1]+cmoveing*vektorpright[1];
    P[2]:=P[2]+cmoveing*vektorpright[2];
    P[3]:=P[3]+cmoveing*vektorpright[3];
    end;
#52:begin{dolava/left}
    P[1]:=P[1]-cmoveing*vektorpright[1];
    P[2]:=P[2]-cmoveing*vektorpright[2];
    P[3]:=P[3]-cmoveing*vektorpright[3];
    end;
#56:begin{hore/up}
    P[1]:=P[1]+cmoveing*vektorpup[1];
    P[2]:=P[2]+cmoveing*vektorpup[2];
    P[3]:=P[3]+cmoveing*vektorpup[3];
    end;
#50:begin{dole/down}
    P[1]:=P[1]-cmoveing*vektorpup[1];
    P[2]:=P[2]-cmoveing*vektorpup[2];
    P[3]:=P[3]-cmoveing*vektorpup[3];
    end;
#53,#43:begin{priblizit/zoom in}
    P[1]:=P[1]+cmoveing*vektorp[1];
    P[2]:=P[2]+cmoveing*vektorp[2];
    P[3]:=P[3]+cmoveing*vektorp[3];
    end;
#49,#51,#55,#57,#45:begin{oddialit/zoom out}
    P[1]:=P[1]-cmoveing*vektorp[1];
    P[2]:=P[2]-cmoveing*vektorp[2];
    P[3]:=P[3]-cmoveing*vektorp[3];
    end;
end;
d;
{->end}
until i=#27;
 CloseGraph;
end.
