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;

 Akzeptierte Antwort

Star Strider
Star Strider am 12 Apr. 2018

1 Stimme

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 Kommentare

Jonathan Bird
Jonathan Bird am 12 Apr. 2018
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?
Star Strider
Star Strider am 12 Apr. 2018
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.
Jonathan Bird
Jonathan Bird am 12 Apr. 2018
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.
Jonathan Bird
Jonathan Bird am 12 Apr. 2018
Awesome thank you
Star Strider
Star Strider am 12 Apr. 2018
As always, my pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by