How to generate a 2D sphere matrix and not a disc

Hello,
I have a question for all, how can i create a matrix image 512*512 of a half sphere ? It is not a disk but a half sphere that can be said that it is 2.5D and not 2D.
An image example generated automatically : (I want to have an image like this but generated with matlab code :-) )
When we show this image by 'surf' function we get this result :
Does anyone have an idea on how to generate this half sphere with a matlab itterative code ? thank you

2 comentarios

Jan
Jan el 10 de Oct. de 2018
Editada: Jan el 10 de Oct. de 2018
Which of the two images do you want to create? What about using sphere? 2.5D? "When we show this image by 'surf'" - does this mean, that you do have the needed code already?
telkab01
telkab01 el 10 de Oct. de 2018
Editada: telkab01 el 10 de Oct. de 2018
Hello, i want to create the first image : a 2D matrix that represents a half sphere (and not a disc). The difference between disc and sphere that when we show sphere image with surf function we obtain a height variation unlike a flat disc.
Using Sphere i get a 3D sphere but i want to have a 2D half sphere (matrix N by M only).
I want to draw my sphere with loops for, on determining the radius and all the details (angle etc ..) have you any ideas ?

Iniciar sesión para comentar.

 Respuesta aceptada

Rik
Rik el 10 de Oct. de 2018
% r^2 = x^2+y^2+z^2
% so if you know r, and generate a field of x and y, you can calculate z
r=200;
[x,y]=ndgrid(-255:256);
% r^2 = x^2+y^2+z^2
% x^2+y^2-r^2=-(z^2)
% z^2=-x^2-y^2+r^2
z=sqrt(-(x.^2)-(y.^2)+r^2);
z=real(z);
imshow(z,[])

Más respuestas (1)

Bruno Luong
Bruno Luong el 10 de Oct. de 2018
Editada: Bruno Luong el 10 de Oct. de 2018
phi=linspace(0,2*pi,512);
theta=linspace(0,pi/2,512).';
x=cos(phi).*cos(theta);
y=sin(phi).*cos(theta);
z=ones(size(phi)).*sin(theta);
surf(x,y,z,'edgecolor','none')
axis equal

3 comentarios

telkab01
telkab01 el 10 de Oct. de 2018
Thnak you for your response, but i want to compute a 2D matrix of half sphere. Thank you again
Bruno Luong
Bruno Luong el 10 de Oct. de 2018
Editada: Bruno Luong el 10 de Oct. de 2018
Sorry to be pedantic, but the first solution you accept actually is not a half sphere but a half-sphere surrounded by a flat rectangle with a hole in the middle, the hole is take place by the half-sphere (use SURF to see this evidence).
You could replace the rectangle part by NaN, but then if you plot the surface using SURF you'll get the "hairs" in the border.
But if it meets your need than I'm OK.
Rik
Rik el 10 de Oct. de 2018
You're being pedantic, or I'm caught red-handed in a trick, either way you bring up an important distinction between our solutions, which I'dd encourage telkab to explore.

Iniciar sesión para comentar.

Preguntada:

el 10 de Oct. de 2018

Comentada:

Rik
el 10 de Oct. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by