function KegelVolumen

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

global ax
ax=[inf -inf inf -inf inf -inf];
deg=pi/180;
clc;
close('all')
c=inline('3+sin(3*t).*cos(5*t)./(1+sin(exp(t)).^2)','t');
t=linspace(0,2*pi,24);
[x,y]=pol2cart(t,c(t),length(t));
xx=x([2:end 1]);
yy=y([2:end 1]);
M=[[(x.^2+x.*xx+xx.^2)*(yy-y)';(y.^2+y.*yy+yy.^2)*(x-xx)']/(6*polyarea(x,y));0];
S=[M(1:2);3];
hold('on')
Keg(M,S,c,length(t))
hRef=0.55*S(3);
tRef=-30*deg;
q=1-hRef/S(3);
plot3(M(1)+q*x,M(2)+q*y,repmat(hRef,size(x)),'k')
M=M+[7;0;0];
S=S+[7;0;0];
[xRef,yRef]=pol2cart(tRef,0.8*c(tRef));
plot3([M(1)+xRef M(1);S(1) S(1)],[M(2)+yRef M(2);S(2) S(2)],[0 M(3);S(3) S(3)],'k')
plot3(M(1)+[0 xRef],M(2)+[0 yRef],[0 0],'k')
plot3(M(1)+[0 q*xRef],M(2)+[0 q*yRef],[hRef hRef],'k')
for h=[0 hRef]
  Scheibe(M+[0;0;h],(1-h/S(3)),c,length(t))
end
Spitze(M,[0;0;-1],0.5,30*deg,'erase','none')
Spitze([M(1:2);hRef],[0;0;1],0.5,30*deg,'erase','none')
qq=-0.3;
plot3([M(1) S(1)]+qq,[M(2) S(2)],[M(3) S(3)],'k');
Spitze(M+[qq;0;0],[0;0;-1],0.5,30*deg,'erase','none')
Spitze(S+[qq;0;0],[0;0;1],0.5,30*deg,'erase','none')
Schrift([M(1:2);hRef/2],'\itz','Rotation',90,'HorizontalAlignment','center','VerticalAlignment','top','erase','none')
Schrift([S(1)+qq;S(2);S(3)/2],'\ith','Rotation',90,'HorizontalAlignment','center','VerticalAlignment','bottom','erase','none')
Schrift([M(1:2)+[xRef;yRef]/2;0],'\its','HorizontalAlignment','center','VerticalAlignment','top','erase','none')
Schrift([M(1:2)+q*[xRef;yRef]/2;hRef],'\it_{ }s_z','HorizontalAlignment','center','VerticalAlignment','top','erase','none')
Schrift([M(1:2)-c(-tRef)*[cos(tRef);sin(tRef)];0],'\itG','FontSize',15,'HorizontalAlignment','center','VerticalAlignment','middle','erase','none')
Schrift([M(1:2)-q*c(-tRef)*[cos(tRef);sin(tRef)];hRef],'\itG_z','FontSize',15,'HorizontalAlignment','right','VerticalAlignment','bottom','erase','none')
hold('off')
axis('equal')
axis([ax(1:5) 2.6])
axis('off')
view(0,10)
print('-deps','KegelVolumen','-zbuffer')

function Keg(M,Sp,c,n)
v=Sp-M;
t=linspace(0,2*pi,n);
s=[0 1];
[T,S]=meshgrid(t,s);
X=M(1)+S*v(1)+(1-S).*c(T).*cos(T);
Y=M(2)+S*v(2)+(1-S).*c(T).*sin(T);
Z=M(3)+S*v(3);
surf(X,Y,Z,T)
c=0.5+0.375*(sin(pi*[0:length(t)-1]/(length(t)-1))).^2;
colormap(repmat(c',1,3));
fill3(X(1,:),Y(1,:),Z(1,:),mean(c)*[1 1 1])
axUpdate(X,Y,Z);

function Scheibe(M,r,c,n)
t=linspace(0,2*pi,n);
X=M(1)+r*c(t).*cos(t);
Y=M(2)+r*c(t).*sin(t);
Z=repmat(M(3),size(X));
fill3(X(1,:),Y(1,:),Z(1,:),0.875*[1 1 1],'edgecolor',0.875*[1 1 1])
axUpdate(X,Y,Z);

function axUpdate(X,Y,Z)
global ax
if min(X(:))<ax(1)
  ax(1)=min(X(:));
end
if max(X(:))>ax(2)
  ax(2)=max(X(:));
end
if min(Y(:))<ax(3)
  ax(3)=min(Y(:));
end
if max(Y(:))>ax(4)
  ax(4)=max(Y(:));
end
if min(Z(:))<ax(5)
  ax(5)=min(Z(:));
end
if max(Z(:))>ax(6)
  ax(6)=max(Z(:));
end

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

function Spitze(P,v,L,phi,varargin);
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',varargin{:});
    end
  end
