% platobuild.m Erzeugen der Platonischen Koerper
%  mit Elementar-Rotationen
%  benoetigt: RZ.m, addanode.m, goback.m, addapoint.m
n = input('Erzeugen der Platonischen Koerper:  n  der n-Ecke = ?');
e = input(' wieviele Flaechen stossen an einer Ecke zusammen?');
% Legalitaetspruefung und Verbindungstopologie
cond = 0;
if n < 6 && n > 2
 if n > 3 && e == 3
   cond = 1;
   if n == 4
     % Hexaeder , Wuerfel
     typ = 3;
'Hexaeder, Wuerfel e=8, k=12, f=6 '
   else
     % dodekaeder
     typ = 5;
'Dodekaeder, Pentagon-Dodekaeder, e=20, k=30, f=12 '
   end
 else 
  if (n == 3) && (e > 2) && (e < 6)
    cond = 1;
    if e == 3
      %  tetraeder
      typ = 1;
'Tetraeder, e=4, k=6, f=4'
    else 
      if e == 4
        %  oktaeder
        typ = 2;
'Oktaeder,  e=6, k=12, f=8'
      else
%  ikosaeder
        typ = 4;
'Ikosaeder, e=12, k=30, f=20'
      end
    end
  end  
 end
end
%
cols = ['r','g','b','c','m'];
lpa = zeros(1,e+1);
if cond == 1
k=1;  % Kantenlaenge
rf = k/2/sin(pi/n) ;
se = 2*rf*sin(2*pi/n);
rp = se/2/sin(pi/e) ;  % Pyramidenradius
hp = sqrt(1-rp^2);
%rf ; rp; hp;
rk = k^2/2/hp;   % Radius der umschreibenden Kugel
rl= 1.2*rk;   % Plotgrenzen
'Radius der umschreibenden Kugel:', rk
wk = 2*asin(k/2/rk);
wkg = wk*180/pi;
% dreibein
figure(1);
clf;
tri = [ 0 1.2 0  0   0 0; ...
        0 0   0  1.2 0 0; ...
        0 0   0  0   0 1.2];
%
plot3( tri(1,:),tri(2,:),tri(3,:),'k','linewidth',1.3 );
hold on
axis ([ -1.3 1.3 -1.3 1.3 -1.3 1.3]);
axis square
view(70,22)
plhand = 0;
% oberster Punkt
ppo = [0 0 rk]';
plot3( ppo(1,1),ppo(2,1),ppo(3,1),'ok' );
% Grundtransformation
trtot = eye(3);
  Royf = [cos(wk/2) 0 -sin(wk/2)  ; 0 1 0; sin(wk/2) 0 cos(wk/2)  ];
  Rozf = [ cos(k*2*pi/e) sin(k*2*pi/e) 0 ; ...
    -sin(k*2*pi/e) cos(k*2*pi/e) 0 ; 0 0 1 ];
  Rozh = [-1 0 0   ; 0 -1 0; 0 0 1  ];
%
rzac = 1;
if typ == 1
  ppt = ppo;
  ndepth = 1;
  bkmat(1:3,1:3,ndepth) = eye(3);
%  
  i1 = 1; i2 = 0;
  for nod = 1:3
    RZ ; addanode;
%
       RZ ; addanode;
       goback; addapoint;
%
    goback; addapoint; 
    sz = size(ppt); if i2 == 0 i2 = sz(2); end
    plot3(ppt(1,i1:i2),ppt(2,i1:i2),ppt(3,i1:i2),...
	  cols(nod),'LineWidth',2);
   pause
  end
%
elseif typ == 2
  ppt = ppo;
  ndepth = 1;
  bkmat(1:3,1:3,ndepth) = eye(3);
%  
  i1 = 1; i2 = 0;
  for nod = 1:4
    RZ ; addanode;
%
    for nodb = 1:2
               RZ ; addanode;
      goback; addapoint;
    end
%
    goback; addapoint;
    sz = size(ppt);    if i2 == 0 i2 = sz(2); end
    plot3(ppt(1,i1:i2),ppt(2,i1:i2),ppt(3,i1:i2), ...
	  cols(nod),'LineWidth',2);
    pause
  end
elseif typ == 3
  ppt = ppo;
  ndepth = 1;
  bkmat(1:3,1:3,ndepth) = eye(3);
%  
  i1 = 1; i2 = 0; 
  for nod = 1:3
    RZ ; addanode;
%
    RZ ; addanode;
%
    for nodb = 1:2
      RZ ; addanode;
      goback; addapoint;
    end
%
    goback; addapoint;
%
    goback; addapoint;
    sz = size(ppt); if i2 == 0 i2 = sz(2); end
    plot3(ppt(1,i1:i2),ppt(2,i1:i2),ppt(3,i1:i2),...
      cols(nod),'LineWidth',2); 
   pause
  end
elseif typ == 4
  ppt = ppo;
  ndepth = 1;
  bkmat(1:3,1:3,ndepth) = eye(3);
%  
  i1 = 1; i2 = 0;
  for nod = 1:5
    RZ ; addanode;
%
    RZ ; addanode;
%
    goback; addapoint;
%
    RZ ; addanode;
%
    for nodb = 1:3
      RZ ; addanode;
      goback; addapoint;
    end
%
    goback; addapoint;
%
    goback; addapoint;
    sz = size(ppt); if i2 == 0 i2 = sz(2); end
    plot3(ppt(1,i1:i2),ppt(2,i1:i2),ppt(3,i1:i2), ...
	  cols(nod),'LineWidth',2);
   pause
  end
elseif typ == 5
  ppt = ppo;
  ndepth = 1;
  bkmat(1:3,1:3,ndepth) = eye(3);
%  
  i1 = 1; i2 = 0;
  for nod = 1:3
    RZ ; addanode;
%
      RZ ; addanode;
%lev2
          for nodb = 1:2
% 2x lev3 am 1. lev2
            RZ ; addanode;
            goback; addapoint;
          end

      goback; addapoint;

      RZ ; addanode;
%lev2
        RZ ; addanode;
% lev3 
%   1. lev 4 am lev3 
          RZ ; addanode;
          goback; addapoint;
% 2. lev 4 
          RZ ; addanode;

            for nodc = 1:2
%  lev5 2x
              RZ ; addanode;
              goback; addapoint;
            end

          goback; addapoint;

        goback; addapoint;

      goback; addapoint;

%
    goback; addapoint;
    sz = size(ppt); if i2 == 0 i2 = sz(2); end
    plot3(ppt(1,i1:i2),ppt(2,i1:i2),ppt(3,i1:i2),...
      cols(nod),'LineWidth',2)
    pause
  end  % for
end


else
 input('sorry, diesen regulaeren Polyeder gibt es nicht!')
end
