How can I create a 3D cylinder (inner and outer radius) and disk (solid tube) with value inside =1 and outside = 0?

10 views (last 30 days)
I am trying to create a cylinder with an inner radius and outer radius, so it looks like a hollow tube. If a point lies inside this tube, it will have a value of 1 and otherwise it is zero. I start with a designated field of view that the cylinder will lie in, then I create a zeros matrix of the field. I then place the cylinder in the centre of this FOV (I want the axis of the cylinder aligned with the x-axis) and create my matrix.
The following code does create a cylinder, however the points are still very square, and I want the ends to look more circular. Is there a way I can go about doing this? I also have a similar code for a disk, but it is the same issue of it seeming square and not circular on the ends.
function cylinder=Cylindershape(FOV,matrixsize,r1,r2,h)
% all units are in cm
% FOV = Field of view - input is a vector [x y z] where x, y, and z create % the FOV
% matrixsize - a vector i.e. [256 256 256]
% r1 is the inner radius of the cylinder
% r2 is the outer radius of the cylinder
% h is the height of the cylinder
centre=[FOV(1)/2 FOV(2)/2 FOV(3)/2];
cylinder=zeros(FOV(1:3));
for index_x=1:matrixsize(1)
for index_y=1:matrixsize(2)
for index_z=1:matrixsize(3)
R=sqrt((index_x-centre(1))^2+(index_z-centre(3))^2);
if (r1<=R)&&(R<=r2)&&(h/2>=abs(index_y-centre(2)))
cylinder(index_x,index_y,index_z)=1;
end
end
end
end
[x,y,z]=ind2sub(size(cylinder),find(cylinder));
plot3(x,y,z,'.k');
axis([0 FOV(1) 0 FOV(2) 0 FOV(3)])
xlabel('X-Direction')
ylabel('Y-Direction')
zlabel('Z-Direction')
I would appreciate any help! Thanks

Answers (0)

Community Treasure Hunt

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

Start Hunting!