Given a center point (x0, y0, z0)
and a radius (g_radius
)
I want to use Python to generate points in a sphere in a cubic world (= Minecraft).
I’m trying to use this algorithm (I found it here on so) but it’s not precise and I have to increase the number of samples to a ridiculous huge number to get almost all the points, but it’s still not 100% accurate:
num_pts = 10000000 indices = arange(0, num_pts, dtype=float) + 0.5 phi = arccos(1 - 2 * indices / num_pts) theta = pi * (1 + 5 ** 0.5) * indices t_x, t_y, t_z = cos(theta) * sin(phi), sin(theta) * sin(phi), cos(phi) tmp = [(g_x0 + int(x * g_radius), g_y0 + int(y * g_radius) + g_radius, g_z0 + int(z * g_radius)) for (x, y, z) in zip(t_x, t_y, t_z)] final_coord_result = list(set(tmp))
Here’s what you get with a small radius (5
):
How would you do this?
Advertisement
Answer
This sounds like a job for raster_geometry:
import raster_geometry from matplotlib import pyplot as plt # define degree of rasterization radius = 7.5 # create full sphere sphere_coords = raster_geometry.sphere(int(2*radius), radius) # make it hollow sphere_coords = raster_geometry.unfill(sphere_coords) # plot it ax = plt.figure().add_subplot(111, projection='3d') ax.scatter(*sphere_coords.nonzero()) plt.show()