Created
May 29, 2018 02:54
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import mpl_toolkits.mplot3d.axes3d as ax3d
is not required.Works well though. Thank you!