HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Implementing write sphere of values to 3D array

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
arraywritevaluessphereimplementing

Problem

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 = 0 && Ypos + h >= 0 && Zpos + d >= 0 ) {
                    if ( ( w * w ) + ( h * h ) + ( d * d ) < 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 ) < Radius Radius ) { of the
function to fit my needs since I pulled it from an example code.

Solution

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 ) <= Radius * Radius )


(i.e. <= instead of just <)

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?

Code Snippets

if ( ( w * w ) + ( h * h ) + ( d * d ) <= Radius * Radius )

Context

StackExchange Code Review Q#56413, answer score: 4

Revisions (0)

No revisions yet.