Skip to content

Instantly share code, notes, and snippets.

@marmakoide
Last active May 12, 2018 05:56
Show Gist options
  • Save marmakoide/1dbd3d459d41e5caddf8a0400eb0c165 to your computer and use it in GitHub Desktop.
Save marmakoide/1dbd3d459d41e5caddf8a0400eb0c165 to your computer and use it in GitHub Desktop.
Returns N (approximatively) evenly spread points over the unit sphere

Golden dot spiral on the sphere

This code sample demonstrates how to generates a set of points that covers a sphere almost evenly, for any number of points. The trick is to use a spiral and the golden angle.

42 points 256 points 999 points
dot spiral, 42 points dot spiral, 256 points dot spiral, 999 points

Such points sets are useful for anything that requires sampling a sphere.

import numpy
def get_spiral_sphere(n):
golden_angle = numpy.pi * (3 - numpy.sqrt(5))
theta = golden_angle * numpy.arange(n)
z = numpy.linspace(1. - 1. / n, 1. / n - 1., n)
radius = numpy.sqrt(1. - z ** 2)
return numpy.stack([radius * numpy.cos(theta), radius * numpy.sin(theta), z], axis = 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment