How do I generate n cubes to define a sphere?

3 views (last 30 days)
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

Accepted Answer

John
John on 29 Nov 2011
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

More Answers (3)

Walter Roberson
Walter Roberson on 30 Nov 2011
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 on 30 Nov 2011
Hi Walter..I think is what I am looking for. Is there a clever way to plot the cubes?

iiulian marius
iiulian marius on 23 Nov 2020
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(:)];

Categories

Find more on Random Number Generation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!