Tube-Plot with x-y-Coordinates and radius

25 views (last 30 days)
Maximilian
Maximilian on 17 Aug 2014
Answered: Roger Stafford on 17 Aug 2014
Hi!
I'm looking for a function, which can plot me a tube around a line.
I will draw a various line with some points i picked with my mouse in a gui and after that, the line should be a tube. Anyone an idea how to do that?

Answers (1)

Roger Stafford
Roger Stafford on 17 Aug 2014
Let P1 and P2 be 1 x 3 row vectors of the x, y, z coordinates of two points along your line and let r be the desired radius.
u = P2-P1;
t = r*null(u)';
v = t(1,:); w = t(2,:);
m = 32; n = 85;
[S,T] = meshgrid(linspace(0,1,m),linspace(0,2*pi,n));
S = S(:); T = T(:);
P = repmat(P1,m*n,1) + S*u + cos(T)*v + sin(T)*w;
X = reshape(P(:,1),n,m);
Y = reshape(P(:,2),n,m);
Z = reshape(P(:,3),n,m);
surf(X,Y,Z)
This should plot the surface of a cylindrical surface of radius r about the line segment P1P2. Hopefully you can adapt the code to your needs.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!