Skip to content

Instantly share code, notes, and snippets.

@Seanmatthews
Created May 29, 2018 02:54
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Seanmatthews/a51ac697db1a4f58a6bca7996d75f68c to your computer and use it in GitHub Desktop.
Save Seanmatthews/a51ac697db1a4f58a6bca7996d75f68c to your computer and use it in GitHub Desktop.
Create equidistant points on the surface of a sphere using Fibonacci sphere algorithm
#!/usr/bin/env python3
import argparse
import mpl_toolkits.mplot3d.axes3d as ax3d
import matplotlib.pyplot as plt
import numpy as np
def fibonacci_sphere(num_points: int):
ga = (3 - np.sqrt(5)) * np.pi # golden angle
# Create a list of golden angle increments along tha range of number of points
theta = ga * np.arange(num_points)
# Z is a split into a range of -1 to 1 in order to create a unit circle
z = np.linspace(1/num_points-1, 1-1/num_points, num_points)
# a list of the radii at each height step of the unit circle
radius = np.sqrt(1 - z * z)
# Determine where xy fall on the sphere, given the azimuthal and polar angles
y = radius * np.sin(theta)
x = radius * np.cos(theta)
# Display points in a scatter plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z)
plt.show()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description = 'fibonacci sphere')
parser.add_argument('numpts', type=int, help='number of points/2 to distribute across spherical cap')
args = parser.parse_args()
fibonacci_sphere(int(args.numpts))
@GRASBOCK
Copy link

import mpl_toolkits.mplot3d.axes3d as ax3d is not required.
Works well though. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment