Evenly distributing n points on a sphere

132,352

Solution 1

In this example code node[k] is just the kth node. You are generating an array N points and node[k] is the kth (from 0 to N-1). If that is all that is confusing you, hopefully you can use that now.

(in other words, k is an array of size N that is defined before the code fragment starts, and which contains a list of the points).

Alternatively, building on the other answer here (and using Python):

> cat ll.py
from math import asin
nx = 4; ny = 5
for x in range(nx):
    lon = 360 * ((x+0.5) / nx)
    for y in range(ny):                                                         
        midpt = (y+0.5) / ny                                                    
        lat = 180 * asin(2*((y+0.5)/ny-0.5))                                    
        print lon,lat                                                           
> python2.7 ll.py                                                      
45.0 -166.91313924                                                              
45.0 -74.0730322921                                                             
45.0 0.0                                                                        
45.0 74.0730322921                                                              
45.0 166.91313924                                                               
135.0 -166.91313924                                                             
135.0 -74.0730322921                                                            
135.0 0.0                                                                       
135.0 74.0730322921                                                             
135.0 166.91313924                                                              
225.0 -166.91313924                                                             
225.0 -74.0730322921                                                            
225.0 0.0                                                                       
225.0 74.0730322921                                                             
225.0 166.91313924
315.0 -166.91313924
315.0 -74.0730322921
315.0 0.0
315.0 74.0730322921
315.0 166.91313924

If you plot that, you'll see that the vertical spacing is larger near the poles so that each point is situated in about the same total area of space (near the poles there's less space "horizontally", so it gives more "vertically").

This isn't the same as all points having about the same distance to their neighbours (which is what I think your links are talking about), but it may be sufficient for what you want and improves on simply making a uniform lat/lon grid.

Solution 2

The Fibonacci sphere algorithm is great for this. It is fast and gives results that at a glance will easily fool the human eye. You can see an example done with processing which will show the result over time as points are added. Here's another great interactive example made by @gman. And here's a simple implementation in python.

import math


def fibonacci_sphere(samples=1000):

    points = []
    phi = math.pi * (3. - math.sqrt(5.))  # golden angle in radians

    for i in range(samples):
        y = 1 - (i / float(samples - 1)) * 2  # y goes from 1 to -1
        radius = math.sqrt(1 - y * y)  # radius at y

        theta = phi * i  # golden angle increment

        x = math.cos(theta) * radius
        z = math.sin(theta) * radius

        points.append((x, y, z))

    return points

1000 samples gives you this:

enter image description here

Solution 3

The golden spiral method

You said you couldn’t get the golden spiral method to work and that’s a shame because it’s really, really good. I would like to give you a complete understanding of it so that maybe you can understand how to keep this away from being “bunched up.”

So here’s a fast, non-random way to create a lattice that is approximately correct; as discussed above, no lattice will be perfect, but this may be good enough. It is compared to other methods e.g. at BendWavy.org but it just has a nice and pretty look as well as a guarantee about even spacing in the limit.

Primer: sunflower spirals on the unit disk

To understand this algorithm, I first invite you to look at the 2D sunflower spiral algorithm. This is based on the fact that the most irrational number is the golden ratio (1 + sqrt(5))/2 and if one emits points by the approach “stand at the center, turn a golden ratio of whole turns, then emit another point in that direction,” one naturally constructs a spiral which, as you get to higher and higher numbers of points, nevertheless refuses to have well-defined ‘bars’ that the points line up on.(Note 1.)

The algorithm for even spacing on a disk is,

from numpy import pi, cos, sin, sqrt, arange
import matplotlib.pyplot as pp

num_pts = 100
indices = arange(0, num_pts, dtype=float) + 0.5

r = sqrt(indices/num_pts)
theta = pi * (1 + 5**0.5) * indices

pp.scatter(r*cos(theta), r*sin(theta))
pp.show()

and it produces results that look like (n=100 and n=1000):

enter image description here

Spacing the points radially

The key strange thing is the formula r = sqrt(indices / num_pts); how did I come to that one? (Note 2.)

Well, I am using the square root here because I want these to have even-area spacing around the disk. That is the same as saying that in the limit of large N I want a little region R ∈ (r, r + dr), Θ ∈ (θ, θ + dθ) to contain a number of points proportional to its area, which is r dr dθ. Now if we pretend that we are talking about a random variable here, this has a straightforward interpretation as saying that the joint probability density for (R, Θ) is just c r for some constant c. Normalization on the unit disk would then force c = 1/π.

Now let me introduce a trick. It comes from probability theory where it’s known as sampling the inverse CDF: suppose you wanted to generate a random variable with a probability density f(z) and you have a random variable U ~ Uniform(0, 1), just like comes out of random() in most programming languages. How do you do this?

  1. First, turn your density into a cumulative distribution function or CDF, which we will call F(z). A CDF, remember, increases monotonically from 0 to 1 with derivative f(z).
  2. Then calculate the CDF’s inverse function F-1(z).
  3. You will find that Z = F-1(U) is distributed according to the target density. (Note 3).

Now the golden-ratio spiral trick spaces the points out in a nicely even pattern for θ so let’s integrate that out; for the unit disk we are left with F(r) = r2. So the inverse function is F-1(u) = u1/2, and therefore we would generate random points on the disk in polar coordinates with r = sqrt(random()); theta = 2 * pi * random().

Now instead of randomly sampling this inverse function we’re uniformly sampling it, and the nice thing about uniform sampling is that our results about how points are spread out in the limit of large N will behave as if we had randomly sampled it. This combination is the trick. Instead of random() we use (arange(0, num_pts, dtype=float) + 0.5)/num_pts, so that, say, if we want to sample 10 points they are r = 0.05, 0.15, 0.25, ... 0.95. We uniformly sample r to get equal-area spacing, and we use the sunflower increment to avoid awful “bars” of points in the output.

Now doing the sunflower on a sphere

The changes that we need to make to dot the sphere with points merely involve switching out the polar coordinates for spherical coordinates. The radial coordinate of course doesn't enter into this because we're on a unit sphere. To keep things a little more consistent here, even though I was trained as a physicist I'll use mathematicians' coordinates where 0 ≤ φ ≤ π is latitude coming down from the pole and 0 ≤ θ ≤ 2π is longitude. So the difference from above is that we are basically replacing the variable r with φ.

Our area element, which was r dr dθ, now becomes the not-much-more-complicated sin(φ) dφ dθ. So our joint density for uniform spacing is sin(φ)/4π. Integrating out θ, we find f(φ) = sin(φ)/2, thus F(φ) = (1 − cos(φ))/2. Inverting this we can see that a uniform random variable would look like acos(1 - 2 u), but we sample uniformly instead of randomly, so we instead use φk = acos(1 − 2 (k + 0.5)/N). And the rest of the algorithm is just projecting this onto the x, y, and z coordinates:

from numpy import pi, cos, sin, arccos, arange
import mpl_toolkits.mplot3d
import matplotlib.pyplot as pp

num_pts = 1000
indices = arange(0, num_pts, dtype=float) + 0.5

phi = arccos(1 - 2*indices/num_pts)
theta = pi * (1 + 5**0.5) * indices

x, y, z = cos(theta) * sin(phi), sin(theta) * sin(phi), cos(phi);

pp.figure().add_subplot(111, projection='3d').scatter(x, y, z);
pp.show()

Again for n=100 and n=1000 the results look like: enter image description here enter image description here

Further research

I wanted to give a shout out to Martin Roberts’s blog. Note that above I created an offset of my indices by adding 0.5 to each index. This was just visually appealing to me, but it turns out that the choice of offset matters a lot and is not constant over the interval and can mean getting as much as 8% better accuracy in packing if chosen correctly. There should also be a way to get his R2 sequence to cover a sphere and it would be interesting to see if this also produced a nice even covering, perhaps as-is but perhaps needing to be, say, taken from only a half of the unit square cut diagonally or so and stretched around to get a circle.

Notes

  1. Those “bars” are formed by rational approximations to a number, and the best rational approximations to a number come from its continued fraction expression, z + 1/(n_1 + 1/(n_2 + 1/(n_3 + ...))) where z is an integer and n_1, n_2, n_3, ... is either a finite or infinite sequence of positive integers:

    def continued_fraction(r):
        while r != 0:
            n = floor(r)
            yield n
            r = 1/(r - n)
    

    Since the fraction part 1/(...) is always between zero and one, a large integer in the continued fraction allows for a particularly good rational approximation: “one divided by something between 100 and 101” is better than “one divided by something between 1 and 2.” The most irrational number is therefore the one which is 1 + 1/(1 + 1/(1 + ...)) and has no particularly good rational approximations; one can solve φ = 1 + 1/φ by multiplying through by φ to get the formula for the golden ratio.

  2. For folks who are not so familiar with NumPy -- all of the functions are “vectorized,” so that sqrt(array) is the same as what other languages might write map(sqrt, array). So this is a component-by-component sqrt application. The same also holds for division by a scalar or addition with scalars -- those apply to all components in parallel.

  3. The proof is simple once you know that this is the result. If you ask what's the probability that z < Z < z + dz, this is the same as asking what's the probability that z < F-1(U) < z + dz, apply F to all three expressions noting that it is a monotonically increasing function, hence F(z) < U < F(z + dz), expand the right hand side out to find F(z) + f(z) dz, and since U is uniform this probability is just f(z) dz as promised.

Solution 4

This is known as packing points on a sphere, and there is no (known) general, perfect solution. However, there are plenty of imperfect solutions. The three most popular seem to be:

  1. Create a simulation. Treat each point as an electron constrained to a sphere, then run a simulation for a certain number of steps. The electrons' repulsion will naturally tend the system to a more stable state, where the points are about as far away from each other as they can get.
  2. Hypercube rejection. This fancy-sounding method is actually really simple: you uniformly choose points (much more than n of them) inside of the cube surrounding the sphere, then reject the points outside of the sphere. Treat the remaining points as vectors, and normalize them. These are your "samples" - choose n of them using some method (randomly, greedy, etc).
  3. Spiral approximations. You trace a spiral around a sphere, and evenly-distribute the points around the spiral. Because of the mathematics involved, these are more complicated to understand than the simulation, but much faster (and probably involving less code). The most popular seems to be by Saff, et al.

A lot more information about this problem can be found here

Solution 5

What you are looking for is called a spherical covering. The spherical covering problem is very hard and solutions are unknown except for small numbers of points. One thing that is known for sure is that given n points on a sphere, there always exist two points of distance d = (4-csc^2(\pi n/6(n-2)))^(1/2) or closer.

If you want a probabilistic method for generating points uniformly distributed on a sphere, it's easy: generate points in space uniformly by Gaussian distribution (it's built into Java, not hard to find the code for other languages). So in 3-dimensional space, you need something like

Random r = new Random();
double[] p = { r.nextGaussian(), r.nextGaussian(), r.nextGaussian() };

Then project the point onto the sphere by normalizing its distance from the origin

double norm = Math.sqrt( (p[0])^2 + (p[1])^2 + (p[2])^2 ); 
double[] sphereRandomPoint = { p[0]/norm, p[1]/norm, p[2]/norm };

The Gaussian distribution in n dimensions is spherically symmetric so the projection onto the sphere is uniform.

Of course, there's no guarantee that the distance between any two points in a collection of uniformly generated points will be bounded below, so you can use rejection to enforce any such conditions that you might have: probably it's best to generate the whole collection and then reject the whole collection if necessary. (Or use "early rejection" to reject the whole collection you've generated so far; just don't keep some points and drop others.) You can use the formula for d given above, minus some slack, to determine the min distance between points below which you will reject a set of points. You'll have to calculate n choose 2 distances, and the probability of rejection will depend on the slack; it's hard to say how, so run a simulation to get a feel for the relevant statistics.

Share:
132,352

Related videos on Youtube

Befall
Author by

Befall

Updated on July 29, 2022

Comments

  • Befall
    Befall almost 2 years

    I need an algorithm that can give me positions around a sphere for N points (less than 20, probably) that vaguely spreads them out. There's no need for "perfection", but I just need it so none of them are bunched together.

    • This question provided good code, but I couldn't find a way to make this uniform, as this seemed 100% randomized.
    • This blog post recommended had two ways allowing input of number of points on the sphere, but the Saff and Kuijlaars algorithm is exactly in psuedocode I could transcribe, and the code example I found contained "node[k]", which I couldn't see explained and ruined that possibility. The second blog example was the Golden Section Spiral, which gave me strange, bunched up results, with no clear way to define a constant radius.
    • This algorithm from this question seems like it could possibly work, but I can't piece together what's on that page into psuedocode or anything.

    A few other question threads I came across spoke of randomized uniform distribution, which adds a level of complexity I'm not concerned about. I apologize that this is such a silly question, but I wanted to show that I've truly looked hard and still come up short.

    So, what I'm looking for is simple pseudocode to evenly distribute N points around a unit sphere, that either returns in spherical or Cartesian coordinates. Even better if it can even distribute with a bit of randomization (think planets around a star, decently spread out, but with room for leeway).

    • ninjagecko
      ninjagecko about 12 years
      What do you mean "with a bit of randomization"? Do you mean perturbations in some sense?
    • BlueRaja - Danny Pflughoeft
      BlueRaja - Danny Pflughoeft about 12 years
      OP is confused. What he's looking for is to put n-points on a sphere, so that the minimum distance between any two points is as large as possible. This will give the points the appearance of being "evenly distributed" over the entire sphere. This is completely unrelated to creating a uniform random distribution on a sphere, which is what many of those links are about, and what many of the answers below are talking about.
    • John Alexiou
      John Alexiou over 9 years
      20 isn't a lot of points to place on a sphere if you don't want them to look just random.
    • trusktr
      trusktr about 6 years
      Here's a way to do it (it has code examples): pdfs.semanticscholar.org/97a6/… (looks like it uses repulsion force calculations)
    • dmckee --- ex-moderator kitten
      dmckee --- ex-moderator kitten over 5 years
      Of course for values on N in {4, 6, 8, 12, 20} there exist exact solutions in which the distance from each point to (each of) it's nearest neighbors is a constant for all points and all nearest neighbors.
  • andrew cooke
    andrew cooke about 12 years
    that would look a lot better if you worked in sin(lat) rather than lat. as it is, you will get a lot of bunching near the poles.
  • Rusty Rob
    Rusty Rob about 12 years
    nice, it's good to see a mathematical solution. I was thinking of using a helix and arc length separation. I'm still not certain on how to get the optimal solution which is an interesting problem.
  • Rusty Rob
    Rusty Rob about 12 years
    to improve my answer you should change closest_index = i to closest_index = randchoice(i,j)
  • andrew cooke
    andrew cooke about 12 years
    did you see that i edited my answer to include an explanation of node[k] at the top? i think that may be all you need...
  • andrew cooke
    andrew cooke about 12 years
    isn't this equivalent to the option he discarded as being "100% randomized"? my understanding is that he wants them to be more evenly spaced than a uniform random distribution.
  • Befall
    Befall about 12 years
    Wonderful, thanks for the explanation. I'll try it out later, as I haven't time currently, but thank you so much for helping me out. I'll let you know how it ends up working for my purposes. ^^
  • Befall
    Befall about 12 years
    I will be looking into the spiral tactic that andrew cooke posted below, however, could you please clarify the difference between what I want and what "uniform random distribution" is? Is that just 100% randomized placement of points on a sphere so that they are uniformly placed? Thanks for the help. :)
  • BlueRaja - Danny Pflughoeft
    BlueRaja - Danny Pflughoeft about 12 years
    @Befall: "uniform random distribution" refers to the probability-distribution being uniform - it means, when choosing a random point on the sphere, every point has an equal likelihood of being chosen. It has nothing to do with the final spatial-distribution of the points, and thus has nothing to do with your question.
  • Befall
    Befall about 12 years
    Ahhh, okay, thanks very much. Searching for my question lead to a ton of answers for both, and I couldn't really grasp which was pointless to me.
  • ninjagecko
    ninjagecko about 12 years
    @BlueRaja-DannyPflughoeft: Hmm, fair enough. I guess I didn't read the question as carefully as I should have. I leave this here anyway in case others find it useful. Thanks for pointing this out.
  • Befall
    Befall about 12 years
    Using the Spiral method fits my needs perfectly, thanks so much for the help and clarification. :)
  • hcarver
    hcarver over 11 years
    It'd be helpful if you wrote some text explaining what this is meant to do, so the OP doesn't have to take it on faith that it will just work.
  • Andrew Staroscik
    Andrew Staroscik over 9 years
    a variable n is called when defining phi: phi = ((i + rnd) % n) * increment. Does n = samples?
  • Fnord
    Fnord over 9 years
    @AndrewStaroscik yes! When i first wrote the code i used "n" as a variable and changed the name later but didn't do due diligence. Thanks for catching that!
  • The Guy with The Hat
    The Guy with The Hat over 8 years
    A good idea, but it only works for 4, 6, 8, 12, 20, 24, or 30 points.
  • naphier
    naphier about 8 years
    This is great, but how would you use this and input the desired distance between the points? For example, say I want to use this to pack sphere with radius of 2 around a sphere. What variable would I change to get a separation of 2 between the points? Thanks!
  • chessofnerd
    chessofnerd about 8 years
    If you want to cheat, you can use the center of faces and verticies. They will not be equi-spaced but a decent approximation. This is nice because it it deterministic.
  • AturSams
    AturSams over 7 years
    @naphier You can do a binary search if I understand your question.
  • AturSams
    AturSams over 7 years
    To be clear, every point has zero probability of being chosen. The ratio of the probabilities that the point will belong to any two areas on the surface of the sphere, is equal to the ratio of the surfaces.
  • AturSams
    AturSams over 7 years
    The difference is of some importance because on a computer, when using floats of doubles, there is a finite number of points on the sphere, depending on your tolerance, and they don't (I think) have an equal chance of being picked.
  • gman
    gman over 7 years
  • Xarbrough
    Xarbrough about 7 years
    Great! How do I scale the entire sphere when inputting a radius other than 1?
  • Fnord
    Fnord about 7 years
    @Xarbrough the code gives you points around a unit sphere, so just multiply each point by whatever scalar you want for radius.
  • doublefelix
    doublefelix about 7 years
    If you are wondering whether to use randomize=True, I did some tests and found that while the minimum nearest neighbor distance (NND) was about the same whether randomize was on or not, the stdev of NNDs divided by their mean was significantly lower (better) when randomize = False. In addition, the "evenness" of the distribution gets better with a larger sample size when randomize = False.
  • Krupip
    Krupip over 6 years
    I'm not sure why this is so far down, this is by far the best fast method to do this.
  • CR Drost
    CR Drost over 6 years
    @snb thank you for the kind words! it is so far down in part because it is much, much younger than all the rest of the answers here. I am surprised that it is even doing as well as it has been.
  • Amir
    Amir about 6 years
    @Fnord I wonder, how can one also make y totally random? Currently y is indifferent to the random seed
  • judepereira
    judepereira about 6 years
    If you're using this in context with a real world, where your camera is placed at 0,0,0, multiply the resulting output (x, y, z) with the radius of your sphere.
  • gota
    gota about 6 years
    3. doesn't work for dimensions > 3... 2. works for all dimensions but the rejection rate becomes impossible for dimensions > 20... Does anyone know of an implementation of 1. in e.g. python?
  • Felix D.
    Felix D. almost 6 years
    The last link is now dead
  • Felix D.
    Felix D. almost 6 years
    One question that remains for me is: How many points n do i need to distribute for a given maximum distance between any two points?
  • CR Drost
    CR Drost almost 6 years
    @FelixD. That sounds like a question that could get very complicated very fast especially if you start using, say, great-circle distances rather than Euclidean distances. But maybe I can answer a simple question, if one converts the points on the sphere to their Voronoi diagram, one can describe each Voronoi cell as having approximately area 4π/N and one can convert this to a characteristic distance by pretending it's a circle rather than a rhombus, πr² = 4π/N. Then r=2/√(N).
  • Felix D.
    Felix D. almost 6 years
    Okay thanks, that sounds like a good estimate for sufficiently large N!
  • Lanny
    Lanny over 5 years
    Another good writeup on evenly distributing points on a sphere is available at extremelearning.com.au/evenly-distributing-points-on-a-spher‌​e
  • Tintinabulator Zea
    Tintinabulator Zea over 5 years
    how can you perform this on an uneven cylinder type shape? if you have the vertices of the geometry
  • dmckee --- ex-moderator kitten
    dmckee --- ex-moderator kitten over 5 years
    Using the sampling theorem with actually uniform instead of randomly-uniform input is one of those things that makes me say "Well, why the #$%& didn't I think of that?". Nice.
  • CR Drost
    CR Drost over 5 years
    @dmckee thanks for saying so! Especially coming from someone I respect as much as you, that's a high compliment!
  • pikachuchameleon
    pikachuchameleon about 5 years
    @Fnord: Can we do this for higher dimensions?
  • dmckee --- ex-moderator kitten
    dmckee --- ex-moderator kitten about 5 years
    Upvoted for the minimum maximum distance expressions. Useful for putting limits on the number of points you want to use. A reference to an authoritative source for that would be nice, though.
  • Ferdinando Randisi
    Ferdinando Randisi over 4 years
    Really cool!!! What tool did you use to generate that render?
  • Demitri
    Demitri about 4 years
    The function above internally calculates phi, then converts to and returns Cartesian coordinates. What is the definition of theta if I wanted to return spherical coordinates without going back and forth?
  • Ismael Harun
    Ismael Harun almost 4 years
    Your latitude conversion to degrees seem incorrect. Shouldn't you divide by pi also?
  • Ismael Harun
    Ismael Harun almost 4 years
    Also your latitude calculation ranges from 0 to 2 exclusively, that can't be right. Perhaps you meant to subtract 1 from that.
  • Ruslan
    Ruslan almost 4 years
    In Spacing the points radially section you say "sphere" twice. Did you instead mean "disk" there?
  • Jonathan H
    Jonathan H over 3 years
    Looks like the reference you cite divides by the golden ratio to obtain theta, but you seem to multiply instead. Is that a mistake?
  • CR Drost
    CR Drost over 3 years
    Great question! I believe my answer is closer to the “reason it works” while Martin’s squeaks out an extra bit of precision. So the golden ratio by definition satisfies φ² = φ + 1, which rearranges to φ – 1 = 1/φ, multiplying by 2 π, that leading digit 1 just gets nuked by the trig functions. So in floating point, just subtracting the one would fill that 53rd bit with a 0 where a 1 would be more correct.
  • orion elenzil
    orion elenzil almost 3 years
    rad. implemented as a raymarched demo on shadertoy.
  • imkzh
    imkzh almost 2 years
    Can I have a list of triangle faces accompany with these vertices as well?