@Caleb P
Actually I like you approach (I don’t think it’s uglier @thomas6’s approach
), and it seems could be faster, here my changes:
[lua]
local math_sqrt = math.sqrt
local math_random = math.random
local math_pi2 = math.pi * 2
local math_cos = math.cos
local math_sin = math.sin
local radius = 500
local radius2 = radius * radius
local count = 2000000
local x, y, r, theta
local time = system.getTimer ()
for i = 1, count do
r = math_sqrt (math_random()) * radius
theta = math_random () * math_pi2
x = r * math_cos (theta)
y = r * math_sin (theta)
– too many circles
– display.newCircle (x + display.contentCenterX, y + display.contentCenterY, 1)
end
local time2 = system.getTimer()
for i = 1, count do
repeat
x = math_random (-radius, radius)
y = math_random (-radius, radius)
until x * x + y * y <= radius2 – << this is the optimization
– too many circles
– display.newCircle (x + display.contentCenterX, y + display.contentCenterY, 1)
end
local time3 = system.getTimer()
print ((time2 - time) / (time3 - time2), time3 - time2, time2 - time)
[/lua]
On my tests, your approach is faster about 1.3 times than @thomas6’s approach.
Thanks for thread.