Making a spherical cap using equations of sphere

Hello everyone,
I am trying to make a spherical cap using points or nodes and mesh grid . I dont want to use built in functions. I am having some problems. Here is my code
% plotting script
% select : which plot you want
set(groot,'defaultLegendInterpreter','latex');
set(0, 'DefaultFigureRenderer','painters');
set(0, 'DefaultFigureRenderer', 'OpenGL');
set(groot,'defaultAxesTickLabelInterpreter','latex');
set(groot,'defaulttextInterpreter','latex');
%%
% Plot the sphere or spherical patch
p = linspace(-1/2,1/2,10);
radius_1 = p(size(p,2))/2;
radius_2 = radius_1 /2;
[X,Y,Z] = meshgrid(p,p,p);
theta= atan(Y./X);
active = (X.^2+Y.^2 +Z.^2 <= radius_1);
active_2 = (X.^2+Y.^2 +Z.^2 <= (radius_1 -radius_2)) ;
figure()
plot3(X(active),Y(active),Z(active),'o','MarkerFaceColor','red');
hold on
plot3(X(active_2),Y(active_2),Z(active_2),'o','MarkerFaceColor','cyan');
I cannot draw the spherical cap. I have the thetha but how do i use it to cut the sphere to make spherical cap and i want to know which nodes are inside the cap and which are outside the cap using mesh grid.
Does anyone knows how to do it?

1 Comment

X.^2+Y.^2 +Z.^2 is the square of the distance to origin, then you compare with radius and diffderence of radius, it does noot make any interpretable sense to me.

Sign in to comment.

 Accepted Answer

phi=linspace(pi/2,pi/6,30); % the cap end is determined by pi/6 change accordinglt
theta=linspace(0,2*pi,120);
r=3;
[PHI,THETA]=meshgrid(phi,theta);
X=r*cos(THETA).*cos(PHI);
Y=r*sin(THETA).*cos(PHI);
Z=r*sin(PHI);
surf(X,Y,Z)
axis equal;

8 Comments

Hi,
thank you for you answer. But i dont want to use surf. I want to first make a box mesh and than make a spherical cap inside it, so I can see which points are inside the cap and which are outside.
But I have no idea what you mean by "spherical cap" since you code wrong equation at first. I only guess you want a "spherical shell".
yes, it is similar to cap in geometry
do you know how it can be done,
I have made the box and the sphere, now I just want to cut that sphere so only spherical shell/ cap look alike is plotted inside the box
I guess your cap is the set
{ X^2 + Y^2 + Z^2 <= R^2 and Z >= something }
-R < something < R
@hamzah khan, instead of using the built-in function as shown by @Bruno Luong, I think you borrowed the concept like computing the Area of Segment:
So, I guess you are trying to remove a solid Cube from a solid Sphere, leaving you with SIX (6) Spherical Caps? Judging from geometry, I think you will get the Cap with a square Base? If my interpretation is wrong, please sketch a simple diagram so that we can understand your product requirement.
hii,
thanks . this is my code now
clear all; close all;
p = linspace(-1/2,1/2,50);
[X,Y,Z] = meshgrid(p,p,p); % box mesh
radius_1 = p(size(p,2))/2;
radius_2 = radius_1/2;
active = (X.^2+Y.^2 +Z.^2 <= radius_1 & Z >= radius_2); % plot sphere
figure()
plot3(X(active),Y(active),Z(active),'o','MarkerFaceColor','red');
hold on
and it is making the spherical cap, thanks
one last question, if i want to fit in the angle thetha, how can i use it?
theta is the angle between y and x if we look at the sphere is 2D
Sorry I don't understand what you ask for.
And in your last code you are still compare distance squared with radius without squared.

Sign in to comment.

More Answers (1)

alternate answer:
clear all; close all;
p = linspace(-1/2,1/2,100);
[X,Y,Z] = meshgrid(p,p,p); % box mesh
R = p(size(p,2))/2;
r = R/1.5;
alpha = deg2rad(45);
f = figure('visible','on');
theta = atan2(sqrt(X.^2+Y.^2),Z);
active = (X.^2 + Y.^2 + Z.^2 <= R^2) & (X.^2 + Y.^2 +Z.^2 >= r^2);
plot3(X(active),Y(active),Z(active),'o','MarkerFaceColor','blue');
active = (X.^2 + Y.^2 + Z.^2 <= R^2) & (X.^2 + Y.^2 +Z.^2 >= r^2) & (abs(theta) <= alpha);
hold on
plot3(X(active),Y(active),Z(active),'o','MarkerFaceColor','red');
saveas(f,'3d_patch','fig')

Products

Release

R2020a

Asked:

on 24 Mar 2022

Answered:

on 29 Mar 2022

Community Treasure Hunt

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

Start Hunting!