MATLAB Answers


Draw partial spheroid include a spheroid

Asked by KOU DU
on 29 Jul 2019
Latest activity Commented on by Kim
on 17 Oct 2019
I want to draw 1/8 spheroid include a small spheroid and output the geometry for mesh. But my current coding always have discontinue in the cutting plan.
Can anyone help provide a idea of cutting the spheroid in 1/8 not for showing but get the data.


Sign in to comment.

3 Answers

Answer by Bruno Luong
on 21 Aug 2019
Edited by Bruno Luong
on 21 Aug 2019
 Accepted Answer

The code bellow us this FEX to generate mesh points.
% radius of the inner/outer spherical parts
r1 = 1;
r2 = 2;
n = 20; % discretization parameters of spherical parts
W = allVL1(3, n); % FEX file
% Connectivity
XY = W*[0 1 0;
0 0 1].';
% Points in S2
Tri3 = eye(3);
W = W/n;
XYZ = W*Tri3';
XYZ = XYZ ./ sqrt(sum(XYZ.^2,2));
% inner/outer sphericals
XYZ1 = XYZ*r1;
XYZ2 = XYZ*r2;
% TRUE for points on the boundary
ibdr = W==0;
close all
patcharg = {'FaceColor', 'g', 'FaceAlpha', 0.5};
patch('Faces', F, 'Vertices', XYZ1, patcharg{:});
patch('Faces', F, 'Vertices', XYZ2, patcharg{:});
for k=1:3
XYZk = [XYZ1(ibdr(:,k),:); flipud(XYZ2(ibdr(:,k),:))];
% You are free to mesh XYZk, I leave it as polygonal shape (quarter of a rings)
patch('XData', XYZk(:,1), 'YData', XYZk(:,2), 'ZData', XYZk(:,3), patcharg{:});
axis equal


Well, I'll be kind for once and ended up doing the whole thing for you
% radius of the outer spherical
r1 = 1;
% parameter of inner part
a=0.2; b=0.2; c=0.2; p=0.7;
n = 20; % discretization parameters of spherical parts
W = allVL1(3, n); % FEX file
% Connectivity
XY = W*[0 1 0;
0 0 1].';
F = delaunay(XY);
% Points in S2
XYZ = W/n;
% inner/outer sphericals
XYZ1 = XYZ .* (r1./ sqrt(sum(XYZ.^2,2)));
q = 2*p;
qnorm = sum((XYZ ./ [a,b,c]).^q,2).^(1/q); % add abs if components are negative
XYZ2 = XYZ ./ qnorm;
% TRUE for points on the boundary
ibdr = W==0;
% Group vertices
XYZ = [XYZ1; XYZ2];
m = size(XYZ1,1);
Fout = 0 + F;
Fin = m + fliplr(F);
Fwall = cell(1,3);
for k=1:3
bk = find(ibdr(:,k));
Fk = [[bk(1:end-1) bk(2:end) bk(1:end-1)]+[0 0 m];
[bk(2:end) bk(1:end-1) bk(2:end)]+[m m 0]];
% Re-orienting triangular faces so that they are consistently oriented
T = reshape(XYZ(Fk,:),[],3,3);
N = cross(T(:,2,:)-T(:,1,:),T(:,3,:)-T(:,1,:),3);
reverse = N(:,:,k) > 0;
Fk(reverse,:) = fliplr(Fk(reverse,:));
Fwall{k} = Fk;
Faces = cat(1,Fin,Fout,Fwall{:});
close all
patcharg = {'FaceColor', 'g', 'FaceAlpha', 0.7};
patch('Faces', Faces, 'Vertices', XYZ, patcharg{:});
axis equal
stlwrite('watermeloon.stl', Faces, XYZ)
Thank you!
on 17 Oct 2019
Since I have a similar problem, I tried to compile this programm but as in my case, I always get the error "Input argument must be a triangulation object."
Could anybody point out, what mistake I've been making?

Sign in to comment.

Answer by darova
on 19 Aug 2019

Use patch() to generate planes
R = 10;
r = 3;
t = linspace(0,pi/2,20);
x = [r*cos(t) fliplr(R*cos(t))];
y = [r*sin(t) fliplr(R*sin(t))];
hold on
hold off


on 22 Aug 2019
Don't know how introduce 'p' parameter in that form
Thanks darova. Sorry I don't see the question of shapes. The inner shape is not only a ellispoid.
And as you said. I try to introduce p but not success. Whatever, thank you very much.

Sign in to comment.

Answer by Abhisek Pradhan on 7 Aug 2019

Following code may be used as an alternative to draw a sphere. Theta and Phi can be varied to get the desired result.
hSurface = surf(X,Y,Z);
set(hSurface,'FaceColor',[0 0 1], 'FaceAlpha',0.5,'FaceLighting','gouraud','EdgeColor','none');
Refer meshgrid and surf for more information.

  1 Comment

Thanks, Pradhan. But I know how to draw a whole sphere or other geometry. The problem I meet now is the discontinue in the cutting plan.

Sign in to comment.