function Kegelmantel

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

deg=pi/180;
h=6;
r=5;
syms('t','u','v','real');
clc;
close('all')
c=[t;0;h-(h/r)*t];
Phi=RK(c,t,sym([0;0;1]),u,v);
xRKInl=inline(vectorize(Phi(1)),'u','v');
yRKInl=inline(vectorize(Phi(2)),'u','v');
zRKInl=inline(vectorize(Phi(3)),'u','v');
[U,V]=meshgrid([0 r],linspace(0,2*pi,64));
X=xRKInl(U,V);
Y=yRKInl(U,V);
Z=zRKInl(U,V);
surf(X,Y,Z,V);
c=0.5+0.375*(sin(pi*[0:size(V,1)-1]/(size(V,1)-1))).^2;
colormap(repmat(c',1,3));
hold('on')
uRef=round(0.35*size(U,1));
plot3(X(uRef,:),Y(uRef,:),Z(uRef,:),'k','linewidth',2.0)
S=([X(uRef,end);Y(uRef,end);Z(uRef,end)]+[0;0;h])/2;
plot3(S(1),S(2),S(3),'.k','Markersize',25,'erase','none');
phi=[0:360]*deg;
plot3(0.5*r*cos(phi),0.5*r*sin(phi),repmat(0.5*h,size(phi)),'k','linewidth',2.0)
Schrift(S,'\itS_{ }','HorizontalAlignment','right','VerticalAlignment','middle','erase','none')
Schrift([cos(3*pi/4) -sin(3*pi/4) 0;sin(3*pi/4) cos(3*pi/4) 0;0 0 1]*S,'\itU_S','HorizontalAlignment','center','VerticalAlignment','top','erase','none')
Schrift([0;0;0.5*h],'\ith','Rotation',90,'HorizontalAlignment','center','VerticalAlignment','top','erase','none')
Schrift([0.5*S(1:2);0.75*h],'{\itm}/2','HorizontalAlignment','right','VerticalAlignment','bottom','erase','none')
Schrift([1.5*S(1:2);0.25*h],'_{ }{\itm}/2','HorizontalAlignment','right','VerticalAlignment','bottom','erase','none')
Schrift([S(1:2);0],'{\itr}','HorizontalAlignment','center','VerticalAlignment','bottom','erase','none')
Schrift([0.5*S(1:2);0.5*h],'{\itr_S}','HorizontalAlignment','center','VerticalAlignment','bottom','erase','none')
plot3([S(1) X(uRef,end);0 0],[S(2) Y(uRef,end);0 0],[S(3) 0;S(3) 0],'k','erase','none');
Schrift([X(uRef,end);Y(uRef,end);Z(uRef,end)],{'Profilkurve' '=Mantellinie'},...
    'HorizontalAlignment','right','VerticalAlignment','bottom','erase','none')
Schrift([0;0;h],'Achse^{  }','Rotation',90,'HorizontalAlignment','left','VerticalAlignment','bottom','erase','none')
plot3(X,Y,Z,'k')
plot3([0;0],[0;0],[min(Z(:));max(Z(:))]+1*(max(Z(:))-min(Z(:)))*[-1;1],'k','erase','none','linewidth',2.0)
Spitze([0;0;h], [0;0;1],0.5,30*deg);
Spitze([0;0;0],-[0;0;1],0.5,30*deg);
axis('equal')
axis([min(X(:)) max(X(:)) min(Y(:)) max(Y(:)) min(Z(:)) 0.7*max(Z(:))])
axis('off')
shading('interp')
view(-46,30)
print('-deps','Kegelmantel','-opengl')

function Phi=RK(c,t,q,u,v);
Phi=(cos(v)*sym(eye(3))+(1-cos(v))*q*q'-sin(v)*[0 q(3) -q(2);-q(3) 0 q(1);q(2) -q(1) 0])*subs(c,t,u);

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

function Spitze(P,v,L,phi);
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','erase','none');
    end
  end
