function Torusknoten

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

R=7; r=4; rho=1; a=2; b=3;
clc
close('all');
xStr='(R+r*cos(b*t))*cos(a*t)';
yStr='(R+r*cos(b*t))*sin(a*t)';
zStr='(R+r*cos(b*t))*r*sin(b*t)';
Phi=Rohr([sym(xStr);sym(yStr);sym(zStr)],'t','rho','u','v');
xRohrInl=inline(vectorize(subs(Phi(1),{'R' 'r' 'rho' 'a' 'b'},{R r rho a b})),'u','v');
yRohrInl=inline(vectorize(subs(Phi(2),{'R' 'r' 'rho' 'a' 'b'},{R r rho a b})),'u','v');
zRohrInl=inline(vectorize(subs(Phi(3),{'R' 'r' 'rho' 'a' 'b'},{R r rho a b})),'u','v');
[U,V]=meshgrid([0:1/72:2]*pi,[0:1/72:2]*pi);
surfl(xRohrInl(U,V),yRohrInl(U,V),zRohrInl(U,V));
shading('interp')
view(0,90);
colormap(repmat(linspace(0.5,0.8,2048)',1,3));
axis('image')
axis('off')
axSurf=axis;
print('-deps','BspTriang1');

[U,V]=meshgrid([0:1/69:2]*pi,[0:1/5:2]*pi);
tri=delaunay(U,V);
trisurf(tri,xRohrInl(U,V),yRohrInl(U,V),zRohrInl(U,V));
shading('faceted')
view(0,90);
colormap([1 1 1]);
axis('image')
axis('off')
axis(axSurf);
print('-deps','BspTriang2');

function [T,N,B]=TNB(c,t);
dc=simple(diff(c,t));
d2c=simple(diff(dc,t));
T=unitVec(dc);
B=unitVec(cross(dc,d2c));
N=cross(B,T);

function Phi=Rohr(m,t,r,u,v);
[T,N,B]=TNB(m,t);
Phi=subs(m,t,u)+r*(cos(sym(v))*subs(N,t,u)+sin(sym(v))*subs(B,t,u));

function v0=unitVec(v);
v0=simple(v/sqrt(dot(v,v)));