function ABVD

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

clc
deg=pi/180;
close('all');
SchrAxo(25*deg,10*deg,[1 1 1],'ABVD-schr1',[]);
SchrAxo(25*deg,10*deg,[2 1 1],'ABVD-schr2',1);
SchrAxo(25*deg,10*deg,[1 2 1],'ABVD-schr3',1);
SchrAxo(25*deg,10*deg,[1 1 2],'ABVD-schr4',[]);
SchrAxo(25*deg,10*deg,[1 2 3],'ABVD-schr5',[]);
SchrAxo(0*deg, 0*deg,[1 1 1],'ABVD-Aufr',[]);
SchrAxo(45*deg, 0*deg,[1 1 1],'ABVD-Kav',1);
alphaV=[30:30:240];
ax=[inf -inf inf -inf];
close('all');
for k=1:length(alphaV)
  SchrAxo(alphaV(k)*deg,(90-alphaV(k))*deg,[1 1 1],['ABVD-Vog' num2str(k)],1);
end

function SchrAxo(alpha,beta,v,Name,spez)
global ax
ax=[inf -inf inf -inf];
deg=pi/180;
figure('numbertitle','off','name',Name);
axo.alpha=alpha;
axo.beta=beta;
axo.v=v;
axo.p1=70*v(1);
axo.p2=100*v(2);
axo.p3=25*v(3);
hold('on')
axo=initAchsen(axo);
a=20; b=75; c=45; d=33;
P=[0  a  a  a-c  a-c
   0  0  b  b    b-d
   0  0  0  0    0];
P=[10+P(1,:)-min(P(1,:))
   10+P(2,:)-min(P(2,:))
      P(3,:)-min(P(3,:))];
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)];
plotGS(axo,P(:,2:4),1);
plotGS(axo,[       First(:,1)     P(:,2)],1);
plotGS(axo,[P(:,3) First(:,3)     P(:,4)],1);
plotGS(axo,First,1);
vis=ismember(1,spez);
plotGS(axo,P(:,1:2),vis);
plotGS(axo,P(:,4:end),vis);
plotGS(axo,[P(:,4) First(:,[3 2]) P(:,5)],vis);
plotGS(axo,[P(:,1) First(:,1)     P(:,2)],vis);
hold('off')
axis('off')
axis('equal')
hdl=Schrift([ax(1);ax(4)],{['{\it v_x}:{\it v_y}:{\it v_z}=' num2str(axo.v(1)) ':' num2str(axo.v(2)) ':' num2str(axo.v(3))] ' '},...
  'HorizontalAlignment','left','VerticalAlignment','bottom','FontSize',30);
extent=get(hdl,'extent');
ax(4)=ax(4)+extent(4);
axis(ax)
print('-depsc',Name)

function Axo=initAchsen(axo)
deg=pi/180;
Axo=axo;
Axo.x=[-cos(axo.alpha);-sin(axo.alpha)];
Axo.y=[cos(axo.beta);-sin(axo.beta)];
Axo.z=[0;1];
Axo.L=max([axo.p1;axo.p2;axo.p3])/10;
O=[0;0];
P1=Axo.x*axo.p1;
P2=Axo.y*axo.p2;
P3=Axo.z*axo.p3;
d=10*axo.v(1);
xTck=0:d:d*round((Axo.p1-1.5*Axo.L)/d);
drawPolygon(@plot,repmat(xTck,2,1).*repmat(Axo.x,1,length(xTck)),'o','color',0.5*[1 1 1],'Markersize',15);
d=10*axo.v(2);
yTck=d:d:d*round((Axo.p2-1.5*Axo.L)/d);
drawPolygon(@plot,repmat(yTck,2,1).*repmat(Axo.y,1,length(yTck)),'o','color',0.5*[1 1 1],'Markersize',15);
d=10*axo.v(3);
zTck=d:d:d*round((Axo.p3-1.5*Axo.L)/d);
drawPolygon(@plot,repmat(zTck,2,1).*repmat(Axo.z,1,length(zTck)),'o','color',0.5*[1 1 1],'Markersize',15);
drawPolygon(@plot,[O P1],'color',0.5*[1 1 1]);
drawPolygon(@plot,[O P2],'color',0.5*[1 1 1]);
drawPolygon(@plot,[O P3],'color',0.5*[1 1 1]);
Spitze(P1,P1,Axo.L,30*deg);
Spitze(P2,P2,Axo.L,30*deg);
Spitze(P3,P3,Axo.L,30*deg);
if Axo.x(2)>0
  Schrift(P1,'\itx','HorizontalAlignment','left','VerticalAlignment','baseline','color',0.5*[1 1 1])
elseif abs(Axo.x(2))<sqrt(eps) & Axo.x(1)>0
  Schrift(P1,'\itx ','HorizontalAlignment','left','VerticalAlignment','middle','color',0.5*[1 1 1])
else
  Schrift(P1,' \itx ','HorizontalAlignment','right','VerticalAlignment','middle','color',0.5*[1 1 1]);
end
if Axo.y(2)>0
  Schrift(P2,'\ity','HorizontalAlignment','right','VerticalAlignment','baseline','color',0.5*[1 1 1]);
else
  Schrift(P2,' \ity','HorizontalAlignment','left','VerticalAlignment','middle','color',0.5*[1 1 1]);
end
if abs(Axo.y(1))<sqrt(eps)
  Schrift(P3,'\itz ','HorizontalAlignment','right','VerticalAlignment','middle','color',0.5*[1 1 1]);
else
  Schrift(P3,'\itz','HorizontalAlignment','center','VerticalAlignment','bottom','color',0.5*[1 1 1]);
end

function [G,Gx,Gy,S]=GS(axo,P) 
Gx=repmat(axo.x,1,size(P,2)).*repmat(P(1,:),2,1)*axo.v(1);
Gy=repmat(axo.y,1,size(P,2)).*repmat(P(2,:),2,1)*axo.v(2);
G=Gx+Gy;
S=G+repmat(axo.z,1,size(P,2)).*repmat(P(3,:),2,1)*axo.v(3);

function [G,Gx,Gy,S]=plotGS(axo,P,vis)
[G,Gx,Gy,S]=GS(axo,P);
drawPolygon(@plot,G,'k','linewidth',0.5)
for k=1:size(P,2)
  drawPolygon(@plot,[G(:,k) S(:,k)],'--','color',0.5*[1 1 1],'linewidth',0.25)
  drawPolygon(@plot,[Gx(:,k) G(:,k) Gy(:,k)],'--','color',0.5*[1 1 1],'linewidth',0.25)
end
if vis
  drawPolygon(@plot,S,'k','linewidth',2.0)
else
  drawPolygon(@plot,S,'--k','linewidth',2.0)
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 drawPolygon(draw,P,varargin);
global ax
feval(draw,P(1,:),P(2,:),varargin{:})
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

function hdl=Schrift(P,txt,varargin)
hdl=text(P(1),P(2),txt,'FontName','TimesNewRoman','FontSize',30,varargin{:});

function Spitze(P,v,L,phi);
if nargin<=2
  L=1;
end
if nargin<=3
  phi=7.5*pi/180;
end
ta=tan(phi/2);
v=L*v./repmat(sqrt(dot(v,v)),size(P,1),1);
n=[-v(2,:);v(1,:)];
Q=P-v+ta*n;
R=P-v-ta*n;
for k=1:size(P,2);
  drawPolygon(@fill,[P Q R],0.5*[1 1 1],'EdgeColor','none')
end
