function ParallDrach

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

clc;
close('all')
global ax
deg=pi/180;
phi=[0:360]*deg;

figure('numbertitle','off','name','Vorgabe');
ax=[inf -inf inf -inf];
A=[0;0]; c=[50;0]; b=[10;23]; B=A+c; C=A+b;
D=Parallelogramm(A,B,C); v=D-A;     A(1)=B(1)+1.2*c(1);  B=A+c; C=A+b;
D=Parallelogramm(B,C,A); v=[v D-A]; A(1)=B(1)+0.3*c(1);  B=A+c; C=A+b;
D=Parallelogramm(C,A,B); v=[v D-A]; A(1)=B(1)+0.3*c(1);  B=A+c; C=A+b;
D=Drachenviereck(A,B,C); v=[v D-A]; A(1)=B(1)+0.3*c(1);  B=A+c; C=A+b;
D=Drachenviereck(B,C,A); v=[v D-A]; A(1)=B(1)+D(1)-A(1); B=A+c; C=A+b;
D=Drachenviereck(C,A,B);
clf;
A=[0;0]; c=[50;0]; b=[10;23]; B=A+c; C=A+b;
hold('on');
Polygon([A B C A],'xk','MarkerSize',15)
ax=[min(v(1,:)) max(v(1,:)) min(v(2,:)) max(v(2,:))];
ax=[ax(1:2)+0.1*[-1 1]*(ax(2)-ax(1)) ax(3:4)+0.1*[-1 1]*(ax(4)-ax(3))];
plot(ax([1 2 2 1 1]),ax([3 3 4 4 3]),'k','linewidth',0.25)

hold('off')
axis('equal')
axis(ax)
axis('off')
print('-deps','ParallDrach-Vgb');

figure('numbertitle','off','name','Parallelogramm - Koordinaten der Eckpunkte');
ax=[inf -inf inf -inf];
A=[9;7];
B=[2;6];
C=[3;1];
hold('on');
D=D123('ParallelogrammK',A,B,C);
a=B-A; 
Spitze(B,a,1,30*deg); 
Schrift(A+0.5*a,'{\it\bfa} = {\itB}-{\itA}','VerticalAlignment','bottom','HorizontalAlignment','center','Rotation',atan(a(2)/a(1))/deg);
Polygon([C D(:,1)],'k');
Schrift(C+0.5*a,'{\it\bfa}','VerticalAlignment','bottom','HorizontalAlignment','center','Rotation',atan(a(2)/a(1))/deg);
Spitze(D(:,1),a,1,30*deg); 
b=C-B; 
Spitze(C,b,1,30*deg); 
Schrift(B+0.5*b,'{\it\bfb} = {\itC}-{\itB}','VerticalAlignment','bottom','HorizontalAlignment','center','Rotation',atan(b(2)/b(1))/deg);
Polygon([A D(:,2)],'k');
Schrift(A+0.5*b,'{\it\bfb}','VerticalAlignment','bottom','HorizontalAlignment','center','Rotation',atan(b(2)/b(1))/deg);
Spitze(D(:,2),b,1,30*deg); 
c=A-C; 
Spitze(A,c,1,30*deg); 
Schrift(C+0.5*c,'{\it\bfc} = {\itA}-{\itC}','VerticalAlignment','bottom','HorizontalAlignment','center','Rotation',atan(c(2)/c(1))/deg);
Polygon([B D(:,3)],'k');
Schrift(B+0.5*c,'{\it\bfc}','VerticalAlignment','bottom','HorizontalAlignment','center','Rotation',atan(c(2)/c(1))/deg);
Spitze(D(:,3),c,1,30*deg); 
Schrift(D(:,1),'_{   }{\itD}_1={\itC}+{\it\bfa}','VerticalAlignment','top','HorizontalAlignment','center')
Schrift(D(:,2),'{\itD}_2={\itA}+{\it\bfb}_{ }','VerticalAlignment','top','HorizontalAlignment','center')
Schrift(D(:,3),'_{ }{\itD}_3={\itB}+{\it\bfc}','VerticalAlignment','bottom','HorizontalAlignment','center')
hold('off')
axis('equal')
axis(ax)
axis('off')
print('-depsc','ParallDrach-ParallelogrammK');

figure('numbertitle','off','name','Parallelogramm - Schnittpunkte der Diagonalen');
ax=[inf -inf inf -inf];
A=[9;7];
B=[2;6];
C=[3;1];
hold('on');
D=D123('ParallelogrammK',A,B,C);
Polygon([A D(:,1)],'k');
Polygon([B D(:,2)],'k');
Polygon([C D(:,3)],'k');
Schrift(D(:,1),'{\itD}_1','VerticalAlignment','top','HorizontalAlignment','center')
Schrift(D(:,2),'{\itD}_2','VerticalAlignment','middle')
Schrift(D(:,3),'_{ }{\itD}_3','VerticalAlignment','bottom','HorizontalAlignment','center')
S=(D+[A B C])/2;
Schrift(S(:,1),'_{ }{\itS}_1','VerticalAlignment','top','HorizontalAlignment','left')
Schrift(S(:,2),'_{ }{\itS}_2','VerticalAlignment','top','HorizontalAlignment','center')
Schrift(S(:,3),'{\itS}_{3   }','VerticalAlignment','bottom','HorizontalAlignment','center')
hold('off')
axis('equal')
axis(ax)
axis('off')
print('-depsc','ParallDrach-ParallelogrammS');

figure('numbertitle','off','name','Drachenviereck - Koordinaten der Eckpunkte');
ax=[inf -inf inf -inf];
A=[9;7];
B=[2;6];
C=[3;1];
hold('on');
D=D123('DrachenviereckK',A,B,C);
D=D(:,[3 1 2]);
Polygon([B D(:,1)],'k');
Polygon([C D(:,2)],'k');
Polygon([A D(:,3)],'k');
S=(D+[B C A])/2;
Polygon([B S(:,1)],'k','linewidth',1.5);
Spitze(A,c,0.5,30*deg); 
Spitze(B,a,0.5,30*deg); 
Spitze(C,b,0.5,30*deg); 
v=S(:,1)-B;
Spitze(S(:,1),v,0.5,30*deg); 
Schrift(0.6*S(:,1)+0.4*B,'{\it Orth_{\bfc}}({\it\bfb})','VerticalAlignment','bottom','HorizontalAlignment','center',...
  'Rotation',atan(v(2)/v(1))/deg);
a=B-A; 
Schrift(A+0.5*a,'{\it\bfa} = {\itB}-{\itA}','VerticalAlignment','bottom','HorizontalAlignment','center','Rotation',atan(a(2)/a(1))/deg);
b=C-B; 
Schrift(B+0.6*b,'{\it\bfb} = {\itC}-{\itB}_{ }','VerticalAlignment','top','HorizontalAlignment','center','Rotation',atan(b(2)/b(1))/deg);
c=A-C; 
Schrift(C+0.6*c,'{\it\bfc} = {\itA}-{\itC}','VerticalAlignment','top','HorizontalAlignment','center','Rotation',atan(c(2)/c(1))/deg);
Schrift(D(:,1),'{\itD}_1','VerticalAlignment','top','HorizontalAlignment','center')
Schrift(D(:,2),'{\itD}_2','VerticalAlignment','bottom','HorizontalAlignment','center')
Schrift(D(:,3),'{\itD}_3','VerticalAlignment','middle','HorizontalAlignment','right')
Schrift(S(:,1),'_{ }{\itS}_1','VerticalAlignment','top','HorizontalAlignment','center')
Schrift(S(:,2),'{\itS}_2','VerticalAlignment','bottom','HorizontalAlignment','left')
Schrift(S(:,3),'{\itS}_3','VerticalAlignment','top','HorizontalAlignment','right')
hold('off')
axis('equal')
axis(ax)
axis('off')
print('-depsc','ParallDrach-DrachenK');

function D=D123(Typ,A,B,C)
global ax
deg=pi/180;
D=zeros(2,3);
D(:,1)=feval(Typ,A,B,C); 
D(:,2)=feval(Typ,B,C,A); 
D(:,3)=feval(Typ,C,A,B); 
P=[A B C D];
ax=[floor(min(P(1,:))) ceil(max(P(1,:))) floor(min(P(2,:))) ceil(max(P(2,:)))]+[-1 1 -1 1];
[X,Y]=meshgrid(floor(ax(1)):ceil(ax(2)),floor(ax(3)):ceil(ax(4)));
plot(X,Y,X',Y','color',0.75*[1 1 1],'linewidth',1.0);
Polygon([A B C A],'k','linewidth',1.5)
switch Typ
  case 'ParallelogrammK'
    Polygon([B D(:,1) C],'--k','linewidth',1.5);
    Polygon([C D(:,2) A],'--k','linewidth',1.5);
    Polygon([A D(:,3) B],'--k','linewidth',1.5);
  case 'DrachenviereckK'
    Polygon([A D(:,1) B],'--k','linewidth',1.5);
    Polygon([B D(:,2) C],'--k','linewidth',1.5);
    Polygon([C D(:,3) A],'--k','linewidth',1.5);
end
h=0.25;
plot(ax(1:2),[0 0],'k',[0 0],ax(3:4),'k','linewidth',1.0)
Spitze([ax(2);0],[1;0],h,45*pi/180)
Schrift([ax(2);0],'\itx','HorizontalAlignment','left','VerticalAlignment','middle');
Spitze([0;ax(4)],[0;1],h,45*pi/180)
Schrift([0;ax(4)+h/2],'\ity','HorizontalAlignment','center','VerticalAlignment','bottom');
Schrift(A,sprintf('_{ }%s(%g;%g)','{\itA}',A))
Schrift(B,sprintf('%s(%g;%g)_{ }','{\itB}',B),'HorizontalAlignment','right')
Schrift(C,sprintf('%s(%g;%g)','{\itC}',C),'HorizontalAlignment','center','VerticalAlignment','top')

function D=ParallelogrammK(A,B,C)
D=B+C-A;

function D=Parallelogramm(A,B,C)
D=ParallelogrammK(A,B,C);
Polygon([A B D C A],'k','linewidth',1.5);
Polygon([B C],'k','linewidth',0.25);
Polygon([A B C],'xk','MarkerSize',10);

function D=DrachenviereckK(A,B,C)
c=B-A;
d=[-c(2);c(1)]/norm(c);
D=C-2*d*d'*(C-A);

function D=Drachenviereck(A,B,C)
D=DrachenviereckK(A,B,C);
Polygon([A D B C A],'k','linewidth',1.5);
Polygon([A B],'k','linewidth',0.25);
Polygon([C D],'k','linewidth',0.25);
Polygon([A B C],'xk','MarkerSize',10);

function Polygon(P,varargin);
global ax
plot(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',18,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