make slices and project to 2d plot

Hello. I have a sphere that I want to make slices of. I want to be able to slice the sphere from the z axis at any point and obtain a 2d circle plots on the x, y axis. The slice at z = 0 would be the biggest circle and z = 4 would be smallest circle. How can I do this? Thank you.

2 Comments

Can't you calculate radius of a circle and just use plot3?
Hello. I can, but I would like to pick a random coordinate and plot. The issue is finding the radius at that coordinate.

Sign in to comment.

Answers (1)

DGM
DGM on 2 Apr 2021
Edited: DGM on 3 Apr 2021
The general idea would be to use contour()
contour(X,Y,Z,V) will draw the contour at each level specified by the vector V.
If you want to only specify one level, specify it as [1 1]*mylevel (i.e. V is a minimum of 2 elements)
Bear in mind, contour just throws the plot at Z=0. If you don't need your surf plot centered, you can always offset it. You can probably offset the zlabels to match. Off the top of my head, idk how you'd offset the contour plot.
[X,Y,Z]=sphere;
r=5;
X2=X*r;
Y2=Y*r;
Z2=Z*r;
% say i want to offset it
surfoffset=5;
clf
surf(X2,Y2,Z2+surfoffset); hold on
% fudge the z axis labels
osz=@(x) num2str(str2num(x)-surfoffset);
set(gca,'zticklabels',cellfun(osz,get(gca,'zticklabels'),'uniformoutput',false))
% pick the z levels you want to plot
% note that these are WRT the non-offset object
contour(X2,Y2,Z2,[0 1 2 3 4])
would give this:
I'm sure there are more elegant ways of dealing with an offset contour.

Asked:

on 1 Apr 2021

Commented:

on 8 Apr 2021

Community Treasure Hunt

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

Start Hunting!