Implementing write sphere of values to 3D array The code below is sort of complicated to explain...
It writes a "sphere" of data to a 3D array from the given 3D-Position outwards in a spherical shape based on the given radius and value to write.

A graphical representation of this would be:



double UbytePrism :: PrismSphereSet( uint64 Xpos , uint64 Ypos , uint64 Zpos , sint64 Radius , ubyte Byte ) {
    for( sint64 w = -Radius; w &lt;= Radius; w ++ ) {
        for( sint64 h = -Radius; h &lt;= Radius; h ++ ) {
            for( sint64 d = -Radius; d &lt;= Radius; d ++ ) {
                if ( Xpos + w &lt; ByteBuffWidth &amp;&amp; Ypos + h &lt; ByteBuffHeight &amp;&amp; Zpos + d &lt; ByteBuffDepth &amp;&amp; Xpos + w &gt;= 0 &amp;&amp; Ypos + h &gt;= 0 &amp;&amp; Zpos + d &gt;= 0 ) {
                    if ( ( w * w ) + ( h * h ) + ( d * d ) &lt; Radius * Radius ) {
                        BytePrism[ ( ( ByteBuffWidth * ByteBuffHeight ) * ( Zpos + d ) ) + ( ByteBuffWidth * ( Ypos + h ) ) + ( Xpos + w ) ] = Byte;
                    };
                };
            };
        };
    };

    return 0.0;
};


The general usage of this function would be:

// On a 7x7x7 grid like the image:
// PrismSphereSet( x , y , z , radius , value );
PrismSphereSet( 3 , 3 , 3 , 4 , 5 );


As you can see there is an issue where it's not drawing an exact circular shape.
If possible I'd like to fix and or modify this to work elliptical-spheres rather
than exact spheres. However, I am not sure how to modify the if-statement if ( ( w * w ) + ( h * h ) + ( d * d ) &lt; Radius * Radius ) { of the
function to fit my needs since I pulled it from an example code.
I find it suspicious that your "Dimension 0" is completely empty. I'd at least expect to see a small "+" of 5's there. Whatever shows up in this last dimension should also show up (assuming spherical symmetry), on the top, the bottom and the sides of the sphere. This will round-out your square.

Perhaps you just need to try

if ( ( w * w ) + ( h * h ) + ( d * d ) &lt;= Radius * Radius )


(i.e. &lt;= instead of just &lt;)

But I think you also need to ask yourself - should a sphere of radius 0 have a single 5 at the origin? Should a sphere of radius 1? What exactly does your radius mean?