function O2D

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

global FontSize
clc;
close('all')
global ax
ax=[inf -inf inf -inf inf -inf];
deg=pi/180;
a=20; b=75; c=45; d=33;
P=[0  0  b  b    b-d
   0 -a -a -a+c -a+c
   0  0  0  0    0];
alph=[45 45 45 30  30]*deg;
P=[P P(:,1)];
dP=diff(P')';
N=zeros(size(P));
for k=1:length(alph)
  v=dP(:,k)/norm(dP(:,k));
  w=cos(alph(k))*[-v(2);v(1);0]+sin(alph(k))*[0;0;1];
  n=cross(v,w);
  N(:,k)=n/norm(n);
end
First=[EEE(P,N,1,2,5) EEE(P,N,2,4,5),EEE(P,N,2,3,4)]
v=P(:,3)-P(:,2);
n=P(:,5)-P(:,1);
n=[-n(2);n(1);0];
S=P(:,2)-(((P(:,2)-P(:,1))'*n)/(v'*n))*v;

Name='O2D-2d';
FontSize=18;
figure('numbertitle','off','name',Name);
A5=Abw([P(:,5) First(:,[2 1]) P(:,1)],alph(5),0.7*[1 1 1]);
drawPolygon(@plot3,P,'k','linewidth',2.0);
hold('on')
drawPolygon(@plot3,[P(:,1) First(:,1)     P(:,2)],'k','linewidth',2.0);
drawPolygon(@plot3,[P(:,2) First          P(:,3)],'k','linewidth',2.0);
drawPolygon(@plot3,[P(:,3) First(:,3)     P(:,4)],'k','linewidth',2.0);
drawPolygon(@plot3,[P(:,4) First(:,[3 2]) P(:,5)],'k','linewidth',2.0);
drawPolygon(@plot3,[P(:,5) First(:,[2 1]) P(:,1)],'k','linewidth',2.0);
drawPolygon(@plot3,First,'k','linewidth',2.0);
axis('off')
axis('equal')
Schrift(P(:,1),'{\itA} ','VerticalAlignment','baseline','HorizontalAlignment','right','erase','none')
Schrift(P(:,2),'{\itB}','VerticalAlignment','top','HorizontalAlignment','right','erase','none')
Schrift(P(:,3),'{\itC}','VerticalAlignment','top','HorizontalAlignment','left','erase','none')
Schrift(P(:,4),'{\itD}','VerticalAlignment','baseline','HorizontalAlignment','left','erase','none')
Schrift(P(:,5),'{\itE}','VerticalAlignment','baseline','HorizontalAlignment','right','erase','none')
Schrift(mean(P(:,[1 2])')',sprintf('%.0f%s',round(alph(1)/deg),'^{\circ}'),'VerticalAlignment','middle','HorizontalAlignment','right','erase','none')
Schrift(mean(P(:,[2 3])')',sprintf('%.0f%s',round(alph(2)/deg),'^\circ'),'VerticalAlignment','top','HorizontalAlignment','center','erase','none')
Schrift(mean(P(:,[3 4])')',sprintf(' %.0f%s',round(alph(3)/deg),'^\circ'),'VerticalAlignment','baseline','HorizontalAlignment','left','erase','none')
Schrift(mean(P(:,[4 5])')',sprintf('%.0f%s',round(alph(4)/deg),'^\circ'),'VerticalAlignment','bottom','HorizontalAlignment','center','erase','none')
Schrift((P(:,5)+P(:,1))/2,sprintf('%.0f%s',round(alph(5)/deg),'^\circ'),'VerticalAlignment','baseline','HorizontalAlignment','right','erase','none')
Schrift(First(:,1),'\itF''','VerticalAlignment','bottom','HorizontalAlignment','left','erase','xor')
Schrift(First(:,2),'\itG''','VerticalAlignment','bottom','HorizontalAlignment','left','erase','xor')
Schrift(First(:,3),'\itH''','VerticalAlignment','top','HorizontalAlignment','right','erase','xor')
k=5;
v=dP(:,k)/norm(dP(:,k));
w=[-v(2);v(1);0];
a=P(:,5)-P(:,1);
a=a/norm(a);
GAE=P(:,1)+a*a'*(A5(1:3,2)-P(:,1));
r=norm(A5(1:3,2)-GAE);
t=degGrid(0,alph(k));
K=repmat(GAE,1,length(t))+0.7*r*(repmat(cos(t),3,1).*repmat(w,1,length(t))+repmat(sin(t),3,1).*repmat(v,1,length(t)));
plot3(K(1,:),K(2,:),K(3,:),'k','linewidth',0.5,'erase','xor')
Schrift(K(:,end),sprintf('%.0f%s',round(alph(5)/deg),'^{\circ  }'),...
  'rotation',-atan2(GAE(2)-K(2,end),K(1,end)-GAE(1))/deg,'VerticalAlignment','bottom','HorizontalAlignment','right','erase','none')
G1=[First(1:2,2);0]+sin(alph(k))*r*v;
a1=G1-[First(1:2,2);0];
a2=G1-GAE;
drawPolygon(@plot3,[[First(1:2,2);0] [First(1:2,2);0]+1.15*a1],'k','linewidth',0.5)
drawPolygon(@plot3,[First(:,2) GAE GAE+1.1*a2],'k','linewidth',0.5)
Spitze(G1,G1-First(:,2),5,30*deg);
Spitze(First(:,2),First(:,2)-G1,5,30*deg);
Schrift((G1+First(:,2))/2,'\ith','Rotation',180+atan2(G1(2)-First(2,2),G1(1)-First(1,2))/deg,'VerticalAlignment','top','HorizontalAlignment','center','erase','none')
drawPolygon(@plot3,[1.1*S-0.1*P(:,2) P(:,2)],'k','linewidth',0.5,'erase','xor')
drawPolygon(@plot3,[1.1*S-0.1*P(:,1) P(:,1)],'k','linewidth',0.5,'erase','xor')
drawPolygon(@plot3,[S First(:,1)],'k','linewidth',0.5,'erase','xor')
Schrift(S,'\itS','VerticalAlignment','top','HorizontalAlignment','center','erase','xor')
xRg=ax(1:2)+[-0.05 0.15]*(ax(2)-ax(1));
yRg=ax(3:4)+[0 0.2]*(ax(4)-ax(3));
B=[xRg; yRg(2) yRg(2);0 0];
drawPolygon(@plot3,B,'k')
S2=ord(S,yRg(2));
A2=ord(P(:,1),yRg(2));
D2=ord(P(:,4),yRg(2));
E2=ord(P(:,5),yRg(2));
F2=ord(First(:,1),yRg(2));
G2=ord(First(:,2),yRg(2));
H2=ord(First(:,3),yRg(2));
Schrift(S2,'\itS''''','VerticalAlignment','bottom','HorizontalAlignment','center','erase','xor')
Schrift(A2,'{\itA''''}={\itB''''}','VerticalAlignment','top','HorizontalAlignment','right','erase','xor')
Schrift(D2,'{\itC''''}={\itD''''}','VerticalAlignment','bottom','HorizontalAlignment','left','erase','xor')
Schrift(E2,'\itE''''','VerticalAlignment','bottom','HorizontalAlignment','center','erase','xor')
Schrift(F2,'\itF''''','VerticalAlignment','bottom','HorizontalAlignment','center','erase','xor')
Schrift(G2,'\itG''''','VerticalAlignment','bottom','HorizontalAlignment','center','erase','xor')
Schrift(H2,'\itH''''','VerticalAlignment','bottom','HorizontalAlignment','left','erase','xor')
drawPolygon(@plot3,[S2 F2],'k','linewidth',0.5)
drawPolygon(@plot3,[A2 F2 G2 H2 D2 A2],'k','linewidth',2.0)
Spitze(G2,[0;1;0],5,30*deg);
Spitze([G2(1);yRg(2);G2(3)],-[0;1;0],5,30*deg);
Schrift((G2+[G2(1);yRg(2);G2(3)])/2,' \ith','VerticalAlignment','middle','HorizontalAlignment','left','erase','xor')
ax=ax+[0 0 0 7 0 0];
axis(ax)
ax2d=ax;
view(2)
print('-deps',Name);

Name='O2D-2d-A';
FontSize=14;
figure('numbertitle','off','name',Name);
col=hsv(7);
lHdl=zeros(1,7);
% A5=Abw([P(:,5) First(:,[2 1]) P(:,1)],alph(5),0.7*[1 1 1]);
drawPolygon(@plot3,P,'k','linewidth',2.0);
hold('on')
drawPolygon(@plot3,[P(:,1) First(:,1)     P(:,2)],'k','linewidth',2.0);
drawPolygon(@plot3,[P(:,2) First          P(:,3)],'k','linewidth',2.0);
drawPolygon(@plot3,[P(:,3) First(:,3)     P(:,4)],'k','linewidth',2.0);
drawPolygon(@plot3,[P(:,4) First(:,[3 2]) P(:,5)],'k','linewidth',2.0);
drawPolygon(@plot3,[P(:,5) First(:,[2 1]) P(:,1)],'k','linewidth',2.0);
drawPolygon(@plot3,First,'k','linewidth',2.0);
axis('off')
axis('equal')
Schrift(P(:,1),'{\itA} ','VerticalAlignment','baseline','HorizontalAlignment','right','erase','none')
Schrift(P(:,2),'{\itB}','VerticalAlignment','top','HorizontalAlignment','right','erase','none')
Schrift(P(:,3),'{\itC}','VerticalAlignment','top','HorizontalAlignment','left','erase','none')
Schrift(P(:,4),'{\itD}','VerticalAlignment','baseline','HorizontalAlignment','left','erase','none')
Schrift(P(:,5),'{\itE}','VerticalAlignment','baseline','HorizontalAlignment','right','erase','none')
Schrift(mean(P(:,[1 2])')',sprintf('%.0f%s',round(alph(1)/deg),'^{\circ}'),'VerticalAlignment','middle','HorizontalAlignment','right','erase','none')
Schrift(mean(P(:,[2 3])')',sprintf('%.0f%s',round(alph(2)/deg),'^\circ'),'VerticalAlignment','top','HorizontalAlignment','center','erase','none')
Schrift(mean(P(:,[3 4])')',sprintf(' %.0f%s',round(alph(3)/deg),'^\circ'),'VerticalAlignment','baseline','HorizontalAlignment','left','erase','none')
Schrift(mean(P(:,[4 5])')',sprintf('%.0f%s',round(alph(4)/deg),'^\circ'),'VerticalAlignment','bottom','HorizontalAlignment','center','erase','none')
Schrift((P(:,5)+P(:,1))/2,sprintf('%.0f%s',round(alph(5)/deg),'^\circ'),'VerticalAlignment','baseline','HorizontalAlignment','right','erase','none')
Schrift(First(:,1),'\itF''','VerticalAlignment','bottom','HorizontalAlignment','left','erase','xor')
Schrift(First(:,2),'\itG''','VerticalAlignment','bottom','HorizontalAlignment','left','erase','xor')
Schrift(First(:,3),'\itH''','VerticalAlignment','top','HorizontalAlignment','right','erase','xor')
k=5;
v=dP(:,k)/norm(dP(:,k));
w=[-v(2);v(1);0];
a=P(:,5)-P(:,1);
a=a/norm(a);
GAE=P(:,1)+a*a'*(A5(1:3,2)-P(:,1));
r=norm(A5(1:3,2)-GAE);
t=degGrid(0,alph(k));
K=repmat(GAE,1,length(t))+0.7*r*(repmat(cos(t),3,1).*repmat(w,1,length(t))+repmat(sin(t),3,1).*repmat(v,1,length(t)));
plot3(K(1,:),K(2,:),K(3,:),'k','linewidth',0.5,'erase','xor')
Schrift(K(:,end),sprintf('%.0f%s',round(alph(5)/deg),'^{\circ  }'),...
  'rotation',-atan2(GAE(2)-K(2,end),K(1,end)-GAE(1))/deg,'VerticalAlignment','bottom','HorizontalAlignment','right','erase','none')
G1=[First(1:2,2);0]+sin(alph(k))*r*v;
a1=G1-[First(1:2,2);0];
a2=G1-GAE;
lHdl(2)=drawPolygon(@plot3,[[First(1:2,2);0] [First(1:2,2);0]+1.15*a1],'color',col(2,:),'linewidth',0.5);
drawPolygon(@plot3,[First(:,2) GAE GAE+1.1*a2],'color',col(2,:),'linewidth',0.5)
lHdl(3)=drawPolygon(@plot3,[G1 First(:,2)],'color',col(3,:),'linewidth',0.5);
Spitze(G1,G1-First(:,2),5,30*deg,'color',col(3,:));
Spitze(First(:,2),First(:,2)-G1,5,30*deg,'color',col(3,:));
Schrift((G1+First(:,2))/2,'\ith','color',col(3,:),'Rotation',180+atan2(G1(2)-First(2,2),G1(1)-First(1,2))/deg,'VerticalAlignment','top','HorizontalAlignment','center','erase','none')
lHdl(5)=drawPolygon(@plot3,[1.1*S-0.1*P(:,2) P(:,2)],'color',col(5,:),'linewidth',0.5,'erase','xor');
drawPolygon(@plot3,[1.1*S-0.1*P(:,1) P(:,1)],'color',col(5,:),'linewidth',0.5,'erase','xor')
drawPolygon(@plot3,[S First(:,1)],'color',col(5,:),'linewidth',0.5,'erase','xor')
Schrift(S,'\itS','VerticalAlignment','top','HorizontalAlignment','center','erase','xor')
% xRg=ax(1:2)+[-0.05 0.15]*(ax(2)-ax(1));
% yRg=ax(3:4)+[0 0.2]*(ax(4)-ax(3));
% B=[xRg; yRg(2) yRg(2);0 0];
drawPolygon(@plot3,B,'k')
S2=ord(S,yRg(2),'color',col(5,:));
[A2,lHdl(1)]=ord(P(:,1),yRg(2),'color',col(1,:));
D2=ord(P(:,4),yRg(2),'color',col(1,:));
E2=ord(P(:,5),yRg(2),'color',col(1,:));
F2=ord(First(:,1),yRg(2),'color',col(6,:));
[G2,lHdl(4)]=ord(First(:,2),yRg(2),'color',col(4,:));
H2=ord(First(:,3),yRg(2),'color',col(4,:));
Schrift(S2,'\itS''''','VerticalAlignment','bottom','HorizontalAlignment','center','erase','xor')
Schrift(A2,'{\itA''''}={\itB''''}','VerticalAlignment','top','HorizontalAlignment','right','erase','xor')
Schrift(D2,'{\itC''''}={\itD''''}','VerticalAlignment','bottom','HorizontalAlignment','left','erase','xor')
Schrift(E2,'\itE''''','VerticalAlignment','bottom','HorizontalAlignment','center','erase','xor')
Schrift(F2,'\itF''''','VerticalAlignment','bottom','HorizontalAlignment','center','erase','xor')
Schrift(G2,'\itG''''','VerticalAlignment','bottom','HorizontalAlignment','center','erase','xor')
Schrift(H2,'\itH''''','VerticalAlignment','bottom','HorizontalAlignment','left','erase','xor')
lHdl(6)=drawPolygon(@plot3,[S2 G2],'color',col(6,:),'linewidth',0.5);
lHdl(7)=drawPolygon(@plot3,[A2 F2 G2 H2 D2 A2],'color',col(7,:),'linewidth',2.0);
drawPolygon(@plot3,[G2 H2],'color',col(4,:));
drawPolygon(@plot3,[G2 [G2(1);yRg(2);G2(3)]],'color',col(3,:));
Spitze(G2,[0;1;0],5,30*deg,'color',col(3,:));
Spitze([G2(1);yRg(2);G2(3)],-[0;1;0],5,30*deg,'color',col(3,:));
Schrift((G2+[G2(1);yRg(2);G2(3)])/2,' \ith','color',col(3,:),'VerticalAlignment','middle','HorizontalAlignment','left','erase','xor')
% ax=ax+[0 0 0 7 0 0];
axis(ax)
ax2d=ax;
view(2)
set(legend(lHdl,'1.','2.','3.','4.','5.','6.','7.',4),'FontName','TimesNewRoman','FontSize',10)
print('-depsc',Name);

Name='O2D-Vgb';
FontSize=18;
figure('numbertitle','off','name',Name);
A5=Abw([P(:,5) First(:,[2 1]) P(:,1)],alph(5),0.7*[1 1 1]);
drawPolygon(@plot3,P,'k','linewidth',2.0);
hold('on')
drawPolygon(@plot3,[P(:,1) First(:,1) P(:,2)],'k')
drawPolygon(@plot3,[P(:,3) First(:,3) P(:,4)],'k')
drawPolygon(@plot3,[P(:,5) First(:,2)],'k')
drawPolygon(@plot3,[P(:,1) First(:,1)     P(:,2)],'k','linewidth',2.0)
drawPolygon(@plot3,[P(:,2) First          P(:,3)],'k','linewidth',2.0)
drawPolygon(@plot3,[P(:,3) First(:,3)     P(:,4)],'k','linewidth',2.0)
drawPolygon(@plot3,[P(:,4) First(:,[3 2]) P(:,5)],'k','linewidth',2.0)
drawPolygon(@plot3,[P(:,5) First(:,[2 1]) P(:,1)],'k','linewidth',2.0)
drawPolygon(@plot3,First,'k','linewidth',2.0)
Schrift(P(:,1),'{\itA} ','VerticalAlignment','baseline','HorizontalAlignment','right','erase','none')
Schrift(P(:,2),'{\itB}','VerticalAlignment','top','HorizontalAlignment','right','erase','none')
Schrift(P(:,3),'{\itC}','VerticalAlignment','top','HorizontalAlignment','left','erase','none')
Schrift(P(:,4),'{\itD}','VerticalAlignment','baseline','HorizontalAlignment','left','erase','none')
Schrift(P(:,5),'{\itE}','VerticalAlignment','baseline','HorizontalAlignment','right','erase','none')
Schrift(mean(P(:,[1 2])')',sprintf('%.0f%s',round(alph(1)/deg),'^{\circ}'),'VerticalAlignment','middle','HorizontalAlignment','right','erase','none')
Schrift(mean(P(:,[2 3])')',sprintf('%.0f%s',round(alph(2)/deg),'^\circ'),'VerticalAlignment','top','HorizontalAlignment','center','erase','none')
Schrift(mean(P(:,[3 4])')',sprintf(' %.0f%s',round(alph(3)/deg),'^\circ'),'VerticalAlignment','baseline','HorizontalAlignment','left','erase','none')
Schrift(mean(P(:,[4 5])')',sprintf('%.0f%s',round(alph(4)/deg),'^\circ'),'VerticalAlignment','bottom','HorizontalAlignment','center','erase','none')
Schrift((P(:,5)+P(:,1))/2,sprintf('%.0f%s',round(alph(5)/deg),'^\circ'),'VerticalAlignment','baseline','HorizontalAlignment','right','erase','none')
Schrift(First(:,1),'\itF''','VerticalAlignment','bottom','HorizontalAlignment','left','erase','xor')
Schrift(First(:,2),'\itG''','VerticalAlignment','bottom','HorizontalAlignment','left','erase','xor')
Schrift(First(:,3),'\itH''','VerticalAlignment','top','HorizontalAlignment','right','erase','xor')
axis('off')
axis('equal')
drawPolygon(@plot3,B,'k')
drawPolygon(@plot3,[[B(1,1);P(2,3)-8;0] [B(1,2);P(2,3)-8;0] [B(1,2);G2(2)+8;0] [B(1,1);G2(2)+8;0] [B(1,1);P(2,3)-8;0]],'k')
axis(ax)
view(2)
print('-deps',Name);

function [Q,hdl]=ord(P,b,varargin)
Q=[P(1);b+P(3);0];
if nargin==2
    hdl=drawPolygon(@plot3,[[P(1:2);0] Q],'--k','erase','xor');
else
    hdl=drawPolygon(@plot3,[[P(1:2);0] Q],'--',varargin{:});
end

function Ab=Abw(P,alph,col)
deg=pi/180;
drawPolygon(@fill3,P,col);
v=P(:,end)-P(:,1);
v=v/norm(v);
n=[-v(2);v(1);0];
w=-cos(alph)*[-v(2);v(1);0]-sin(alph)*[0;0;1];
phi=acos(n'*w);
I=eye(3);
D=cos(phi)*I+(1-cos(phi))*v*v'-sin(phi)*[0 v(3) -v(2);-v(3) 0 v(1);v(2) -v(1) 0];
Ab=[I P(:,1);0 0 0 1]*[D zeros(3,1);0 0 0 1]*[I -P(:,1);0 0 0 1]*[P;ones(1,size(P,2))];
% drawPolygon(@fill3,Ab,col,'linewidth',0.5);
% drawPolygon(@plot3,Ab,'-.k','linewidth',2.0);
% T=degGrid(0,phi,3);
% A=zeros(4,length(T));
% for k=2:size(P,2)-1
%   for t=1:length(T)
%     D=cos(T(t))*I+(1-cos(T(t)))*v*v'-sin(T(t))*[0 v(3) -v(2);-v(3) 0 v(1);v(2) -v(1) 0];
%     A(:,t)=[I P(:,1);0 0 0 1]*[D zeros(3,1);0 0 0 1]*[I -P(:,1);0 0 0 1]*[P(:,k);1];
%   end
%   drawPolygon(@plot3,A,':k')
%   Spitze(A(1:3,end),[0;0;-1],4,30*deg)
% end

function A=EEE(P,N,i,j,k);
A=[N(:,i)';N(:,j)';N(:,k)']\[N(:,i)'*P(:,i);N(:,j)'*P(:,j);N(:,k)'*P(:,k)];

function hdl=drawPolygon(draw,P,varargin);
global ax
H=feval(draw,P(1,:),P(2,:),P(3,:),varargin{:});
if nargout
    hdl=H;
end
if min(P(1,:))<ax(1)
  ax(1)=min(P(1,:));
end
if max(P(1,:))>ax(2)
  ax(2)=max(P(1,:));
end
if min(P(2,:))<ax(3)
  ax(3)=min(P(2,:));
end
if max(P(2,:))>ax(4)
  ax(4)=max(P(2,:));
end
if min(P(3,:))<ax(5)
  ax(5)=min(P(3,:));
end
if max(P(3,:))>ax(6)
  ax(6)=max(P(3,:));
end

function Schrift(P,txt,varargin)
global FontSize
text(P(1),P(2),P(3),txt,'FontName','TimesNewRoman','FontSize',FontSize,varargin{:});

function Spitze(P,v,L,phi,varargin);
deg=pi/180;
if nargin<=2
  L=1;
end
if nargin<=3
  phi=7.5*deg;
end
v=L*v./repmat(sqrt(dot(v,v)),size(P,1),1);
Q=P-v;
t=[0:6:360]*deg;
for k=1:size(P,2)
  N=tan(phi/2)*null(v(:,k)');
  for w=2:length(t)
    plot3(...
      [P(1,k) Q(1,k)+cos(t(w))*L(:,k)*N(1,1)+sin(t(w))*L(:,k)*N(1,2) Q(1,k)+cos(t(w-1))*L(:,k)*N(1,1)+sin(t(w-1))*L(:,k)*N(1,2) P(1,k)],...
      [P(2,k) Q(2,k)+cos(t(w))*L(:,k)*N(2,1)+sin(t(w))*L(:,k)*N(2,2) Q(2,k)+cos(t(w-1))*L(:,k)*N(2,1)+sin(t(w-1))*L(:,k)*N(2,2) P(2,k)],...
      [P(3,k) Q(3,k)+cos(t(w))*L(:,k)*N(3,1)+sin(t(w))*L(:,k)*N(3,2) Q(3,k)+cos(t(w-1))*L(:,k)*N(3,1)+sin(t(w-1))*L(:,k)*N(3,2) P(3,k)],...
      'k',varargin{:});
  end
end

function t=degGrid(t1,t2,d);
if nargin<3
  d=1;
end
deg=pi/180;
delta=d*deg;
t=linspace(t1,t2,round((t2-t1)/delta));
