function Platon

% http://geometrie.hs-nb.de/MATLAB-Programme/Platon.m   Bearbeitungsstand: 20.1.2005
% (C) Martin Nitschke, nitschke@hs-nb.de

clc
close('all')
Abb('Tetraeder','1')
Abb('Wuerfel','2')
Abb('Oktaeder','3')
Abb('Ikosaeder','4')
Abb('Pentagondodekaeder','5')

function Abb(Name,suffix)
figure('NumberTitle','off','Name',Name)
[P,V,F]=eval(Name);
P=double(P);
view(-58,28)
axis('equal')
axis([min(P(1,:)) max(P(1,:)) min(P(2,:)) max(P(2,:)) min(P(3,:)) 0.8*max(P(3,:))])
cmap=0.5+0.375*(sin(pi/3*[0:size(F,1)-1]/(size(F,1)-1))).^2;
cmap=[cmap(1:2:end) cmap(2:2:end)];
patch('Vertices',V,'Faces',F,'FaceVertexCData',repmat(cmap',1,3),'FaceColor','flat','EdgeColor','none')
axis('off')
print('-deps',['Platon-' suffix])

function [P,V,F]=Tetraeder
t=linspace(0,2*sym(pi),4);
P=[cos(t(1:end-1));sin(t(1:end-1))];
a=sqrt(dot(P(:,2)-P(:,1),P(:,2)-P(:,1)));
P=[P mean(P')';0 0 0 a*sqrt(sym(6))/3];
V=double(P)';
F=[1 2 3 
  1 2 4
  2 3 4
  3 1 4];

function [P,V,F]=Wuerfel
t=linspace(0,2*sym(pi),5);
P=[cos(t(1:end-1));sin(t(1:end-1));zeros(1,length(t)-1)];
P=[P [P(1:2,:);ones(1,length(t)-1)*sqrt(sym(2))]];
V=double(P)';
F=[1 2 3 4
  1 2 6 5
  2 3 7 6
  3 4 8 7
  4 1 5 8
  5 6 7 8];

function [P,V,F]=Oktaeder
t=linspace(0,2*sym(pi),5);
P=[cos(t(1:end-1));sin(t(1:end-1))];
a=sqrt(dot(P(:,2)-P(:,1),P(:,2)-P(:,1)));
P=[P mean(P')' mean(P')';0 0 0 0 [1 -1]*a*sqrt(sym(2))/2];
V=double(P)';
F=[1 2 5
  2 3 5
  3 4 5
  4 1 5
  1 2 6
  2 3 6
  3 4 6
  4 1 6];

function [P,V,F]=Pentagondodekaeder
t=linspace(0,2*sym(pi),6);
P=[cos(t(1:end-1));sin(t(1:end-1));zeros(1,length(t)-1);ones(1,length(t)-1)];
a=sqrt(dot(P(:,2)-P(:,1),P(:,2)-P(:,1)))
alpha=acos(-1/sqrt(sym(5)))
P=[P ...
    Rot(P(:, 1),P(:, 2),-alpha)*P(:,[3 4 5]) ...
    Rot(P(:, 2),P(:, 3),-alpha)*P(:,[4 5]) ...
    Rot(P(:, 3),P(:, 4),-alpha)*P(:,[5 1]) ...
    Rot(P(:, 4),P(:, 5),-alpha)*P(:,[1 2]) ...
    Rot(P(:, 5),P(:, 1),-alpha)*P(:,3)];
P=[P ...
    Rot(P(:, 6),P(:, 7),alpha)*P(:,[1 8]) ...
    Rot(P(:, 7),P(:, 8),alpha)*P(:,2) ...
    Rot(P(:, 9),P(:,10),alpha)*P(:,2) ...
    Rot(P(:,11),P(:,12),alpha)*P(:,3)];
% c=hsv(size(P,2));for k=1:size(P,2),text(double(P(1,k)),double(P(2,k)),double(P(3,k)),num2str(k),'color',c(k,:),'fontsize',20,'erase','none'),end
V=double(P(1:3,:))';
F=[1  2  3  4  5
   1  2  6  7  8
   2  3  9 10  6
   3  4 11 12  9
   4  5 13 14 11
   5  1  8 15 13
   6  7 17 16 10
   7  8 15 18 17
   9 10 16 19 12
  11 12 19 20 14
  14 13 15 18 20
  16 17 18 20 19];

function [P,V,F]=Ikosaeder
t=linspace(0,2*sym(pi),4);
P=[cos(t(1:end-1));sin(t(1:end-1));zeros(1,length(t)-1);ones(1,length(t)-1)];
a=sqrt(dot(P(:,2)-P(:,1),P(:,2)-P(:,1)))
alpha=acos(-sqrt(sym(5))/3)
P=[P ...
    Rot(P(:, 1),P(:, 2),-alpha)*P(:,3) ...
    Rot(P(:, 2),P(:, 3),-alpha)*P(:,1) ...
    Rot(P(:, 3),P(:, 1),-alpha)*P(:,2)];
P=[P ...
    Rot(P(:, 1),P(:, 4),-alpha)*P(:,2) ...
    Rot(P(:, 2),P(:, 4), alpha)*P(:,1) ...
    Rot(P(:, 3),P(:, 5), alpha)*P(:,2)];
P=[P ...
    Rot(P(:, 4),P(:, 7), alpha)*P(:,1) ...
    Rot(P(:, 5),P(:, 8), alpha)*P(:,2) ...
    Rot(P(:, 6),P(:, 7),-alpha)*P(:,1)];
%c=hsv(size(P,2));for k=1:size(P,2),text(double(P(1,k)),double(P(2,k)),double(P(3,k)),num2str(k),'color',c(k,:),'fontsize',20,'erase','none'),end
V=double(P(1:3,:))';
F=[1  2  3
   1  2  4
   2  3  5
   3  1  6  
   1  4  7
   2  4  8 
   2  5  8
   5  9  3
   3  6  9
   1  6  7
   4  7 10
   4  8 10
   5  8 11
   8 10 11
   5  9 11
   6  7 12
   6  9 12
   7 10 12
   9 11 12
  10 11 12];

function T=Rot(A,B,alpha)
e=B-A;
e=e(1:3);
e=e/sqrt(dot(e',e));
e4=[0;0;0;1];
I=eye(3);
R=cos(alpha)*I+(1-cos(alpha))*e*e'-sin(alpha)*[0 -e(3) e(2);e(3) 0 -e(1);-e(2) e(1) 0];
T=[I A(1:3);e4']*[R zeros(3,1);e4']*[I -A(1:3);e4'];


% Hilfsfunktionen zur Farbgestaltung

function C=topocolors(a,b)
% schwarz-weiss
q=0.3;
C=[q*gray(-a)+(1-q)*ones(-a,3)
  q*gray(b)+(1-q)*ones(b,3)];
% % Farbe
% C=[topomaptrans(1:36,1:-a)
%   topomaptrans(37:64,1:b)];
% % aufhellen
% a=0.3;
% C=a*C+(1-a)*ones(size(C));

function T=topomaptrans(I,J)
if isempty(J)
  T=[];
else
  load('topo','topomap1')
  K=polyval( polyfit(J([1 end]),I([1 end]),1) , J);
  T=[topomap1(I(1),:)
    interp1(I,topomap1(I,:),K(2:end-1))
    topomap1(I(end),:)];
end
