function ESVW

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

close('all');
clc
deg=pi/180;
OrthAxo(25*deg,35*deg,40*deg,'ESVW-Tri',0);
OrthAxo(asin(1/8),acos(1/4*sqrt(7))-asin(1/3*sqrt(2)),asin(1/4*sqrt(7))+asin(1/3*sqrt(2)),'ESVW-Di1',1);
OrthAxo(asin(1/4*sqrt(7)),asin(1/4*sqrt(7))+asin(1/3*sqrt(2)),45*deg,'ESVW-Di2',1);
OrthAxo(30*deg,45*deg,45*deg,'ESVW-Iso',1);

function OrthAxo(alpha,gamma1,gamma2,Name,dispW)
global ax
figure('numbertitle','off','name',Name);
ax=[inf -inf inf -inf];
axo.alpha=alpha;
axo.p1=20;
axo.p2=29;
axo.p3=15;
axo.xy=Riss(gamma1,15,17,20);
axo.yz=Riss(gamma2,20,20,15);
hold('on')
axo=initAchsen(axo)
Schrift(axo.xy.O,'\itO'' ','HorizontalAlignment','right','VerticalAlignment','bottom')
Schrift(axo.yz.O,'\itO''''  ','HorizontalAlignment','center','VerticalAlignment','baseline')
Schrift([0;0],'\itO^{ }','HorizontalAlignment','right','VerticalAlignment','bottom')
drawPolygon(@plot,[0.9*axo.x(1)*axo.p1 0.6*axo.y(1)*axo.p2;0 0],':k')
r=0.85;
K1=Kr([0;0],r*[axo.x(1)*axo.p1;0],-r*axo.p1*axo.x(1)*axo.x,'-ok')
[Y1,Y2,Y]=plotGA(axo,[0;0.5*axo.p2;0]);
r=8;
K2=Kr(axo.xy.O,axo.xy.O+[-r;0],axo.xy.O+r*axo.xy.x,'-dk')
r=4.5;
K3=Kr(axo.yz.O,axo.yz.O+r*[-axo.x(2);axo.x(1)],axo.yz.O+r*axo.yz.y,'-vk')
if dispW
  set(legend([K1 K2 K3],{['\alpha_{ }=' dmsstr(alpha)] ['\gamma_1=' dmsstr(gamma1)] ['\gamma_2=' dmsstr(gamma2)]},2),'FontName','TimesNewRoman','fontsize',16.3);
else
  set(legend([K1 K2 K3],{'\alpha_{ }' '\gamma_1' '\gamma_2'},2),'FontName','TimesNewRoman','fontsize',27);
end
Schrift(Y1,'\itY''','HorizontalAlignment','right','VerticalAlignment','top')
Schrift(Y2,'\itY''''','HorizontalAlignment','left','VerticalAlignment','bottom')
Schrift(Y,'\itY','HorizontalAlignment','right','VerticalAlignment','top')
hold('off')
axis('off')
axis('equal')
axis(ax)
print('-deps',Name)

function [G,A,S]=GAS(axo,P) 
G=ProjRiss(axo.xy.O,axo.xy.x,axo.xy.y,P(1:2,:));
A=ProjRiss(axo.yz.O,axo.yz.y,axo.yz.z,P(2:3,:));
n=[-axo.x(2);axo.x(1)];
S=zeros(2,size(P,2));
for k=1:size(P,2)
  S(:,k)=G(:,k)-(((G(:,k)-A(:,k))'*n)/(axo.z'*n))*axo.z;
end

function [G,A,S]=plotGA(axo,P)
[G,A,S]=GAS(axo,P);
for k=1:size(P,2)
  drawPolygon(@plot,[G(:,k) S(:,k) A(:,k)],'--k')
end
drawPolygon(@plot,G,'k','linewidth',1.0)
drawPolygon(@plot,A,'k','linewidth',1.0)
drawPolygon(@plot,S,'k','linewidth',2.0)

function Q=ProjRiss(o,e1,e2,P)
v1=repmat(P(1,:),2,1).*repmat(e1,1,size(P,2));
v2=repmat(P(2,:),2,1).*repmat(e2,1,size(P,2));
O=repmat(o,1,size(P,2));
Q=O+v1+v2;

function R=Riss(gamma,dO,p1,p2)
R.gamma=gamma;
R.dO=dO; 
R.p1=p1; 
R.p2=p2;

function Axo=initAchsen(axo)
deg=pi/180;
Axo=axo;
Axo.x=[-cos(axo.alpha);-sin(axo.alpha)];
Axo.z=[0;1];
L=max([axo.p1;axo.p2;axo.p3])/10;
[Axo.xy.O,Axo.xy.x,Axo.xy.y]=plotAR(pi/2,pi+axo.xy.gamma,axo.xy,L,'x''','y''');
[Axo.yz.O,Axo.yz.y,Axo.yz.z]=plotAR(pi+axo.alpha,1.5*pi+axo.alpha+axo.yz.gamma,axo.yz,L,'y''''','z''''');
[G,A,S]=GAS(Axo,[0;1;0]);
Axo.y=S/norm(S);
O=[0;0];
P1=Axo.x*axo.p1;
P2=Axo.y*axo.p2;
P3=Axo.z*axo.p3;
drawPolygon(@plot,[O P1],'k');
drawPolygon(@plot,[O P2],'k');
drawPolygon(@plot,[O P3],'k');
Spitze(P1,P1,L,30*deg);
Spitze(P2,P2,L,30*deg);
Spitze(P3,P3,L,30*deg);
Schrift(P1,'\itx ','HorizontalAlignment','right','VerticalAlignment','middle')
Schrift(P2,'\ity','HorizontalAlignment','left','VerticalAlignment','middle')
Schrift(P3,'\itz','HorizontalAlignment','center','VerticalAlignment','bottom')

function [O1,e1,e2]=plotAR(w,w1,R,L,s1,s2);
deg=pi/180;
s=sign(w1);
w1=mod(w1,2*pi);
O=[0;0];
O1=-[cos(w);sin(w)]*R.dO;
w2=mod(w1+0.5*s*pi,2*pi);
e1=[cos(w1);sin(w1)];
e2=[cos(w2);sin(w2)];
P1=O1+e1*R.p1;
P2=O1+e2*R.p2;
drawPolygon(@plot,[O O1],':k','linewidth',2.0)
v=O1-0;
v=v/norm(v);
v=[-v(2);v(1)];
drawPolygon(@plot,[O1+1.2*v*v'*(P1-O1) O1+v*v'*(P2-O1)],':k')
drawPolygon(@plot,[P2 O1 P1],'k')
Spitze(P1,P1-O1,L,30*deg);
Spitze(P2,P2-O1,L,30*deg);
if w1>1.5*pi | w1<0.5*pi
  Schrift(P1,['\it' s1],'HorizontalAlignment','left','VerticalAlignment','middle')
else
  Schrift(P1,['\it' s1],'HorizontalAlignment','right','VerticalAlignment','middle')
end
if w2<pi
  Schrift(P2,['\it' s2],'HorizontalAlignment','center','VerticalAlignment','bottom')
else
  Schrift(P2,['\it' s2],'HorizontalAlignment','center','VerticalAlignment','top')
end

function h=Kr(M,A,B,varargin);
deg=pi/180
v=A-M;
w=B-M;
if abs(norm(v)-norm(w))>sqrt(eps)
  error('Fehler in function Kr: A und B nicht gleich weit von M entfernt');
end
r=norm(v);
v0=v/r;
w0=w/r;
t=degGrid(0,acos(v0'*w0),100/r)
K=repmat(M,1,length(t))+repmat(cos(t),2,1).*repmat(v,1,length(t))+repmat(sin(t),2,1).*repmat([-v(2);v(1)],1,length(t));
h=drawPolygon(@plot,K,varargin{:},'erase','xor')

function h=drawPolygon(draw,P,varargin);
global ax
h=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 Schrift(P,txt,varargin)
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);
  fill([P(1,k) Q(1,k) R(1,k)],[P(2,k) Q(2,k) R(2,k)],'k')
end

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

function t=dmsstr(alpha)
deg=pi/180;
d=alpha/deg;
D=fix(d);
m=60*(d-D);
M=fix(m);
S=round(60*(m-M));
if S==60
  S=0;
  M=M+1;
end
if M==60;
  M=0;
  D=D+1;
end
t=sprintf('%2d^\\circ%02d''%02d''''',D,M,S);