Skip to content

Instantly share code, notes, and snippets.

@behreajj
Created July 26, 2020 13:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save behreajj/3f078fda638dd231aab3038d0be8b8cf to your computer and use it in GitHub Desktop.
Save behreajj/3f078fda638dd231aab3038d0be8b8cf to your computer and use it in GitHub Desktop.
Fibonacci Sphere Convex Hull
from bpy import data as D, context as C
import bmesh
import math
def fibonacci_points(count=32, radius=0.5):
"""
Distributes points on a sphere according to
the golden ratio, (1.0 + sqrt(5.0)) / 2.0.
"""
# Validate inputs.
v_count = max(3, count)
v_rad = max(0.000001, radius)
# Approximately 1.618033988749895 .
golden_ratio = (1.0 + 5.0 ** 0.5) * 0.5
tau_gr = math.tau * golden_ratio
to_step = 2.0 / v_count
i_range = range(0, v_count)
vs = []
for i in i_range:
azimuth = tau_gr * i
inclination = math.asin(1.0 - i * to_step)
rho_cos_phi = v_rad * math.cos(inclination)
# Convert spherical to Cartesian coordinates.
x = rho_cos_phi * math.cos(azimuth)
y = rho_cos_phi * math.sin(azimuth)
z = v_rad * -math.sin(inclination)
v = (x, y, z)
vs.append(v)
return vs
bm = bmesh.new()
count = 1024
fib_points = fibonacci_points(count)
for point in fib_points:
bm.verts.new(point)
bmesh.ops.convex_hull(bm, input=bm.verts)
mesh_data = D.meshes.new("Sphere")
bm.to_mesh(mesh_data)
bm.free()
mesh_obj = D.objects.new(mesh_data.name, mesh_data)
C.collection.objects.link(mesh_obj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment