Generating points on a sphere

I found this code online, but I can’t seem to plug it into Lua.

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);

I need to generate a list of points on a sphere to iterate through, primarily to fire rays in their direction. Anyone know how?

1 Like

I looked up this code. You got it from here: python - Evenly distributing n points on a sphere - Stack Overflow

You conveniently left out this:

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

That’s where the magic happens.

In that same post I saw another bit of code:

import math, random

def fibonacci_sphere(samples=1,randomize=True):
    rnd = 1.
    if randomize:
        rnd = random.random() * samples

    points = []
    offset = 2./samples
    increment = math.pi * (3. - math.sqrt(5.));

    for i in range(samples):
        y = ((i * offset) - 1) + (offset / 2);
        r = math.sqrt(1 - pow(y,2))

        phi = ((i + rnd) % samples) * increment

        x = math.cos(phi) * r
        z = math.sin(phi) * r

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

    return points

I roughly converted this for you:

local function fibonacci_sphere(samples)
   local rnd = 1.0

        rnd = math.random() * samples

     local  points = {}
     local  offset = 2/samples
     local  increment = math.pi * (3. - math.sqrt(5));

    for i=1, samples do
        y = ((i * offset) - 1) + (offset / 2);
        r = math.sqrt(1 - math.pow(y,2))

        phi = ((i + rnd) % samples) * increment

        x = math.cos(phi) * r
        z = math.sin(phi) * r

      ---  points.append([x,y,z])
local Part= Instance.new("Part")
Part.Parent = workspace
Part.Size = Vector3.new(.5, .5, .5)
Part.CFrame = CFrame.new( Vector3.new(x, y, z)*10)

print(x, z)
end

    return points




end


fibonacci_sphere(400)

It generates this:

7 Likes

Here’s the code you posted that’s translated to Lua (roughly).

local num_pts = 1000
local indices =  -- All floats from 0  to num_pts inclusively (for loop perhaps where i = float)

local phi = math.acos(1 - 2*indices/num_pts)
local theta = math.pi * (1 + 5*0.5) * indices

local x, y, z = math.cos(theta) * math.sin(phi), math.sin(theta) * math.sin(phi), math.cos(phi)

I found what I think is a more elegant solution. Let me know if it’s any help to you :slight_smile:

local GoldenRatio = 1 + math.sqrt(5)/4
local AngleIncrement = math.pi*2*GoldenRatio
local Multiplier = 30
local Points = 20000

for i = 0, Points do
	local Distance = i/Points
	local Angle = AngleIncrement*i
	local Incline = math.acos(1 - 2*Distance)
	local Azimuth = AngleIncrement*i

	local x = math.sin(Incline) * math.cos(Azimuth) * Multiplier
	local y = math.sin(Incline) * math.sin(Azimuth) * Multiplier
	local z = math.cos(Incline) * Multiplier

	local Point = script.Part:Clone()
	Point.Position = Vector3.new(x,y,z)
	Point.Parent = workspace.Model
end

Equdistant points sphere

1 Like