How do I generate n cubes to define a sphere?

조회 수: 8 (최근 30일)
Chad
Chad 2011년 11월 29일
답변: iiulian marius 2020년 11월 23일
I would like to generate cubes in a sphere. For example I would like to start simple where I can generate a central cube with 26 surrounding cubes that fit in a sphere defined by the size of the cubes. I am not concerned about filling the sphere completely. This would be my first attempt where I have to find the center of the surrounding cubes with respect to the center cube. Once I get my program working where I perform a calculation on the surrounding 26 cubes I would like to extend it to many cubes where I can record the center of each cube. So my program will subtend n cubes surrounding a central cube.
I would like to plot the 27 cubes and record the central (x,y,z) of each cube. I have tried using some examples but they are far move involved than I need. I appreciate the help.
Chad

채택된 답변

John
John 2011년 11월 29일
To create the cubes and visualize them I would start with something like the following:
It has proven useful in the past but requires the use of patches, if you are not familiar with them I suggest looking in the help

추가 답변 (3개)

Walter Roberson
Walter Roberson 2011년 11월 30일
The largest cube that will fit in a sphere with radius R will have diagonal 2*R and thus would have outer edges of length 2*R/sqrt(3) . If we assign the center of the sphere to be coordinates (0,0,0) and orient the cube to be perpendicular to the coordinate axes, then the outer cube would extend from -R/sqrt(3) to +R/sqrt(3) in each of the three coordinates.
Divide that (-R/sqrt(3) to +R/sqrt(3)) equally in to 3 planes in each direction to get the total of 27 cubes. That gives bounding planes at -R/sqrt(3), -R/3/sqrt(3), +R/3/sqrt(3) and +R/sqrt(3) and corresponding edge midpoints of -R/2/sqrt(3), 0, and +R/2/sqrt(3), and the complete list of centers then becomes
cc = [-R/2/sqrt(3), 0, +R/2/sqrt(3)];
[Xc, Yc, Zc] = ndgrid(cc, cc, cc);
centers = [Xc(:), Yc(:), Zc(:)];
and for vertices,
ev = [-R/sqrt(3), -R/3/sqrt(3), +R/3/sqrt(3), +R/sqrt(3)];
[Xv, Yv, Zv] = ndgrid(ev, ev, ev);
vertices = [Xv(:), Yv(:), Zv(:)];

Chad
Chad 2011년 11월 30일
Hi Walter..I think is what I am looking for. Is there a clever way to plot the cubes?

iiulian marius
iiulian marius 2020년 11월 23일
ev = [-R/sqrt(3), -R/3/sqrt(3), +R/3/sqrt(3), +R/sqrt(3)];
[Xv, Yv, Zv] = ndgrid(ev, ev, ev);
vertices = [Xv(:), Yv(:), Zv(:)];
ev = [-R/sqrt(3), -R/3/sqrt(3), +R/3/sqrt(3), +R/sqrt(3)];
[Xv, Yv, Zv] = ndgrid(ev, ev, ev);
vertices = [Xv(:), Yv(:), Zv(:)];

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by