Math.lua |
|
---|---|
Math is a module of math helper functions useful in game development |
module(..., package.seeall)
local utils = require(_PACKAGE .. 'utils')
local List = require(_PACKAGE .. 'List')
local Point = require(_PACKAGE .. 'Point')
|
Random numbersA set of functions to handle random and quasi-random sequences |
|
haltonReturns the first The Halton sequence is a quasi-random sequence used to generate evenly-distributed random values.
Returns a List of |
function halton(base, num)
local values = List()
--- The permutation is a table of 9 digits in random order
local digits = List{1, 2, 3, 4, 5, 6, 7, 8, 9}
local permutation = {}
repeat
local _, i = digits:random()
table.insert(permutation, digits:remove_at(i))
until digits:empty()
for n = 1, num do
--- First generate the number...
local result = 0
local f = 1 / base
local i = n
while i > 0 do
result = result + f * (i base);
i = math.floor(i / base)
f = f / base;
end
--- Then scramble it...
local r = 0
for n = 0, 15 do
local d = math.floor(result * (10^n) 10)
if d > 0 then
r = r + permutation[d] / (10^n)
end
end
--- Then add it to the list
values:push(r)
end
return values
end
|
qrandom pointsGenerate a list of quasi-random Points using a scrambled Halton sequence
Returns a List of |
function qrandom_points(num, width, height)
local points = List()
local x = halton(2, num)
local y = halton(3, num)
for n = 1, num do
local sx = round(x:at(n) * (width+0.5))
local sy = round(y:at(n) * (height+0.5))
points:push(Point(sx, sy))
end
return points
end
|
random pointsGenerate a list of random Points using math.random.
Returns a List of |
function random_points(num, width, height)
local points = List()
for n = 1, num do
local x = math.random(width)
local y = math.random(height)
points:push(Point(x, y))
end
return points
end
|
roundIf the fractional part of |
function round(n)
local fpart = ((n * 10) 10) / 10
if fpart >= 0.5 then return math.ceil(n)
else return math.floor(n) end
end
|
CollisionsThis is a set of functions to determine if two shapes intersect |
|
collision point rectReturns whether a point lies within a rectangle
Returns true or false. |
function collision_point_rect(pt, topleft, size)
return pt.x >= topleft.x and
pt.x <= topleft.x+size.x and
pt.y >= topleft.y and
pt.y <= topleft.y+size.y
end
|