Reputation: 3715
I have values for the following variables::
x0,y0,z0 = coords of the first selection (intended to be center of sphere)
x1,y1,z1 = coords of the second selection (intended to be an outmost point for the sphere)
ishollow = boolean value indicating if the sphere should be hollow
The result must draw, the best it can, a perfect sphere. Here is an example:
I was unsure of where to start, can you point me in the direction to identify the mathimatical functions to code such a process? Thank you for your assistance in advance.
Upvotes: 0
Views: 2281
Reputation: 78324
The first quantity you have to compute is the radius of the sphere. This is simple, using your notation radius = sqrt((x1-x0)^2+(y1-yo)^2+(z1-z0)^2)
.
Next, to 'colour in' the entire sphere, you could simply loop through every voxel in your 100 x 100 x 100
space and test whether or not it is within the sphere, colouring it accordingly. There are lots of ways to make this operation more efficient, for example you could first figure out the axis-aligned bounding box for the sphere and only iterate over the points in that box. You could do all your computations in one octant of the space, then use reflection to colour in the other octants.
Drawing a hollow sphere will be rather more challenging, the radius of your sphere will, in general, be a real number, not an integer, so there won't be a nicely arranged one voxel thick shell of voxels around the centre. Instead, the naive-est algorithm (test that a voxel is exactly at the specified distance from the sphere's centre) might result in no voxels being included in the shell. You'll have to allow some tolerance in your tests for whether or not a voxel is in the shell, inside it or outside it. I expect that you'll find it quite tricky to define the tolerance such that you never have the shell two voxels thick at any point while simultaneously being zero voxels thick at other points.
If you're finding the conceptualisation of all this a little mind-bending, work it all out for a circle in 2D before tackling the 3rd dimension.
Upvotes: 1
Reputation: 29244
Distance between points = Sphere Radius.
(Center Position) & (Sphere Radius) is all you need. You can check if a point is within the sphere if its distance to the center is less than the radius.
Upvotes: 1