Translating a hemisphere in 3D

I have created a hemisphere with the following code and then rotated it by 90 degrees. I now want to translate the shape to a new position vector. Thanks
[x1,y1,z1]=sphere;
x1=x1(11:end,:);
y1=y1(11:end,:);
z1=z1(11:end,:);
r=10;
sf=surf(r.*x1,r.*y1,r.*z1);
rotate(sf,[1,0,0],90);
axis equal;

 Accepted Answer

Try this:
[x1,y1,z1]=sphere;
x1=x1(11:end,:);
y1=y1(11:end,:);
z1=z1(11:end,:);
r=10;
figure
sf=surf(r.*x1,r.*y1,r.*z1);
rotate(sf,[1,0,0],90);
hold on
XD = get(sf, 'XData');
YD = get(sf, 'YData');
ZD = get(sf, 'ZData');
surf(XD-10, YD+15, ZD+20)
hold off
axis equal

6 Comments

It works thanks! only problem now is it generates two shapes one in the desired location and one in the original location, do you know how I could delete the original shape?
As always, my pleasure!
The easiest way is to remove the hold calls, since without hold on, the new (translated) hemisphere will over-write the first one. (I kept the first one in, using hold, to demonstrate that the second one is actually translated relative to the first.)
Another option is to plot the translated shape in a new axes or figure.
I would not clear the original ‘sf’ handle, since you may want those data later in your code, and clearing it may also eliminate the translated hemisphere.
I tried this but it didn't seem to work because I also need to plot a cylinder on the same figure, any suggestions?
figure(1)
%cylinder
[x,y,z]=cylinder([0,10,10,0],100);
z([1,2],:)=0;
z([3,4],:)=100;
hm=surf(x,y,z);
axis equal;
direction=[1,0,0];
rotate(hm,direction,90)
hold on
%1st hemisphere
[x1,y1,z1]=sphere;
x1=x1(11:end,:);
y1=y1(11:end,:);
z1=z1(11:end,:);
r=10;
sf=surf(r.*x1,r.*y1,r.*z1);
rotate(sf,[1,0,0],90);
XD = get(sf, 'XData');
YD = get(sf, 'YData');
ZD = get(sf, 'ZData');
surf(XD, YD-80, ZD+20)
axis equal
The cylinder capped by a hemisphere seems to have worked.
You can’t eliminate the first hemisphere (the blue one), but you can make it invisible by adding a set call to do that just after you assign ‘sf’:
sf=surf(r.*x1,r.*y1,r.*z1);
set(sf, 'EdgeColor','none', 'FaceAlpha',0)
This doesn’t affect the hemisphere on the cylinder end. The rest of your code is unchanged.
Awesome thank you
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!