function DreieckHMSW

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

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

A=[0;0];
c=[50;0];
b=[10;23];

B=A+c;
C=A+b;
Polygon([A B C A],'k','linewidth',1.5);
hold('on');
Polygon([Lotfuss(A,B,C) C],'k','linewidth',0.25);
Polygon([Lotfuss(B,C,A) A],'k','linewidth',0.25);
Polygon([Lotfuss(C,A,B) B],'k','linewidth',0.25);

A(2)=A(2)-1.3*b(2);
B=A+c;
C=A+b;
Polygon([A B C A],'k','linewidth',1.5);
Polygon([M(A,B,C) (A+B)/2],'k','linewidth',0.25);
Polygon([M(B,C,A) (B+C)/2],'k','linewidth',0.25);
Polygon([M(C,A,B) (C+A)/2],'k','linewidth',0.25);
Kreisbogen(B,SchnittGerGer(M(A,B,C),(A+B)/2,M(B,C,A),(B+C)/2),B,180*deg,'k','linewidth',0.25);

A(2)=A(2)-1.9*b(2);
B=A+c;
C=A+b;
Polygon([A B C A],'k','linewidth',1.5);
Polygon([A (B+C)/2],'k','linewidth',0.25);
Polygon([B (C+A)/2],'k','linewidth',0.25);
Polygon([C (A+B)/2],'k','linewidth',0.25);
S=(A+B+C)/3;
Schrift((A+S)/2,'2','VerticalAlignment','bottom','HorizontalAlignment','center','Rotation',Rot(A,S));
Schrift((B+S)/2,'2','VerticalAlignment','bottom','HorizontalAlignment','center','Rotation',Rot(B,S));
Schrift((C+S)/2,'2','VerticalAlignment','bottom','HorizontalAlignment','left','Rotation',Rot(C,S));
Schrift(A+1.25*(S-A),'1','VerticalAlignment','bottom','HorizontalAlignment','center','Rotation',Rot(A,S));
Schrift(B+1.25*(S-B),'1','VerticalAlignment','bottom','HorizontalAlignment','center','Rotation',Rot(B,S));
Schrift(C+1.25*(S-C),'1','VerticalAlignment','bottom','HorizontalAlignment','left','Rotation',Rot(C,S));

A(2)=A(2)-1.3*b(2);
B=A+c;
C=A+b;
Polygon([A B C A],'k','linewidth',1.5);
Polygon([W(A,B,C) A],'k','linewidth',0.25);
Polygon([W(B,C,A) B],'k','linewidth',0.25);
Polygon([W(C,A,B) C],'k','linewidth',0.25);
InkreisM=SchnittGerGer(W(A,B,C),A,W(B,C,A),B);
Polygon(repmat(InkreisM,size(phi))+norm(InkreisM-Lotfuss(A,B,InkreisM))*[cos(phi);sin(phi)],'k','linewidth',0.25)


hold('off')
axis('equal')
axis(ax+[-1 1 -1 1])
axis('off')
print('-deps','DreieckHMSW');

function H=Lotfuss(A,B,C);
syms('t','real');
v=B-A;
H=A+double(solve(v'*(A+t*v-C),t))*(B-A);

function H=M(A,B,C);
syms('t','real');
v=B-A;
t1=double(solve(v'*(B+t*(C-B)-(A+B)/2),t));
if (t1>=0) & (t1<=1)
  H=B+t1*(C-B);
else
  H=A+double(solve(v'*(A+t*(C-A)-(A+B)/2),t))*(C-A);
end

function H=W(A,B,C)
syms('t','real');
alpha1=cart2pol(B(1)-A(1),B(2)-A(2));
alpha2=cart2pol(C(1)-A(1),C(2)-A(2));
alpha=(alpha1+alpha2)/2;
n=[-sin(alpha);cos(alpha)];
H=B+double(solve(n'*(B+t*(C-B)-A),t))*(C-B);

function S=SchnittGerGer(A,B,C,D)
syms('t','real');
n=[C(2)-D(2);D(1)-C(1)];
S=A+double(solve(n'*(A+t*(B-A)-C),t))*(B-A);

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 Kreisbogen(A,M,B,offset,varargin)
deg=pi/180;
[t1,r1]=cart2pol(A(1)-M(1),A(2)-M(2));
[t2,r2]=cart2pol(B(1)-M(1),B(2)-M(2));
if abs(r1-r2)>sqrt(eps)
  error('Kreisbogen: A und B liegen nicht auf einem Kreis mit Mittelpunkt M')
end
r=(r1+r2)/2;
t1=t1-offset;
t2=t2+offset;
if t2<t1
  t2=t2+2*pi;
end
t=linspace(t1,t2,round((t2-t1)/deg));
Polygon(repmat(M,size(t))+r*[cos(t);sin(t)],varargin{:});

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

function r=Rot(a,b)
deg=pi/180;
r=atan2(b(2)-a(2),b(1)-a(1))/deg;
if r<0
  r=r+180;
end
if r>90
  r=r-180;
end
