## Small Useful Functions

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
OttoRobba
Party member
Posts: 104
Joined: Mon Jan 06, 2014 5:02 am
Location: Sao Paulo, Brazil

### Re: Small extra functions

Cool functions

If I may suggest, another useful function is a table.shuffle - great for random item lists and whatnot. I'm using the one below, it works only with properly indexed tables but it should be easily adaptable.

Code: Select all

function table.shuffle(array)
math.randomseed(os.time())
local arrayCount = #array
for i = arrayCount, 2, -1 do
local j = math.random(1, i)
array[i], array[j] = array[j], array[i]
end
return array
end
For use with LÖVE, love.math.random would probably be better.

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: Small extra functions

Looks like a good implementation of Fisher–Yates shuffle. Don't call math.randomseed, though. It should generally only be called at the start of the program, otherwise it becomes less random.

Roland_Yonaba
Inner party member
Posts: 1562
Joined: Tue Jun 21, 2011 6:08 pm
Contact:

### Re: Small extra functions

Robin wrote:Looks like a good implementation of Fisher–Yates shuffle.
Definitely.
Robin wrote:Don't call math.randomseed, though. It should generally only be called at the start of the program, otherwise it becomes less random.
I agree with that, too.

HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Somewhere in Brazil
Contact:

### Re: Small extra functions

Another table-related small function that I made a long time ago. Pretty useful, I think. Gets the biggest item of a table, being it another table (amount of items), a string (amount of chars) or a number (biggest value). For the biggest table, it only gets array sizes, but not string items (like something["anotherthing"]). For that, I'd highly suggest replacing every "#table" thing on the code by "table.size" and implement this function (may be in the main post).

Code: Select all

function table.biggest(t, dtype) --Copied from Mari0 +Portal, but totally mine :)
local dtype = dtype or "number"
local biggest = nil
for i, v in pairs(t) do
if not biggest then
if type(v) == dtype then
biggest = i
end
else
if type(v) == dtype then
if dtype == "table" then
if #v > #t[biggest] then
biggest = i
end
elseif dtype == "number" then
if v > t[biggest] then
biggest = i
end
elseif dtype == "string" then
if string.len(v) > string.len(t[biggest]) then
biggest = i
end
end
end
end
end
return biggest
end


EDIT: I was trying to get fading colors, but the way LÖVE provides it in Snippets' category is a bit (a lot) confusing. So, I made my own, simple fading color function:

Arguments: the timer value, the maximum timer value, the first color's table, the second color's table
Returns the new colors

Code: Select all

function fade(currenttime, maxtime, c1, c2)
local tp = currenttime/maxtime
local ret = {} --return color

for i = 1, #c1 do
ret[i] = c1[i]+(c2[i]-c1[i])*tp
ret[i] = math.max(ret[i], 0)
ret[i] = math.min(ret[i], 255)
end

return unpack(ret)
end


I also made a small function to get the average value of numbers:

Code: Select all

function math.med( ... )
local args = { ... }
local ret = 0
for i = 1, #args do
ret = ret + args[i]
end
return ret/#args
end

HugoBDesigner - my Blog

HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Somewhere in Brazil
Contact:

### Re: Small extra functions

Another small drawing function. This time, to draw ellipses:

Code: Select all

function love.graphics.ellipse(mode, x, y, w, h, segments)
local pointstable = {}
local segments = segments or 32
for i = 1, segments do
local px = x + w/2
local py = y + h/2
local a = math.pi*2/segments*i
px = px + math.cos(a)*w/2
py = py + math.sin(a)*h/2
table.insert(pointstable, px)
table.insert(pointstable, py)
end
love.graphics.polygon(mode, pointstable)
end

HugoBDesigner - my Blog

Ref
Party member
Posts: 687
Joined: Wed May 02, 2012 11:05 pm

### Re: Small extra functions

Slightly different ellipse function - permits rotation.

Code: Select all

function ellipse( mode, x, y, rx, ry, an, ox, oy )
-- x&y are upper left corner
-- for centering  ox = rx/2 & oy = ry/2
assert( type(mode) == 'string', 'Ops! Forgot to select mode!' )
ry = ry or rx
ox = ox or 0
oy = oy or 0
an = an or 0
love.graphics.push()
love.graphics.translate( x, y )
love.graphics.rotate( an )
local segments = ( rx + ry ) / 10
local vertices = {}
for i = 0, 2 * math.pi * ( 1 - 1 / segments ), 2 * math.pi / segments do
vertices[#vertices+1] = rx * (1 + math.cos(i) ) / 2 - ox
vertices[#vertices+1] = ry * (1 + math.sin(i) ) / 2 - oy
end
love.graphics.polygon( mode, vertices )
love.graphics.pop()
end


Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

### Re: Small extra functions

Does anyone have a formula that outputs the sum of all previous numbers of the the given one ?
For exemple if I provide 1 the function would give me 1
2 would give me 3 because 1+2=3
3 = 6, 1+2+3 = 6
4 = 10, 1+2+3+4 = 10
5 = 16, 1+2+3+4+5 = 15
6 = 21,1+2+3+4+5+6 = 21

And I'd like a formula and not actual a function because I don't want to calculate this with a for loop as googol numbers would take quite a lot of time.

EDIT: Here it is:

Code: Select all

function sum(n)
return (n*(n+1))/2
end
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping

HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Somewhere in Brazil
Contact:

### Re: Small extra functions

Ranguna259 wrote:Does anyone have a formula that outputs the sum of all previous numbers of the the given one ?
For exemple if I provide 1 the function would give me 1
2 would give me 3 because 1+2=3
3 = 6, 1+2+3 = 6
4 = 10, 1+2+3+4 = 10
5 = 16, 1+2+3+4+5 = 15
6 = 21,1+2+3+4+5+6 = 21

And I'd like a formula and not actual a function because I don't want to calculate this with a for loop as googol numbers would take quite a lot of time.

EDIT: Here it is:

Code: Select all

function sum(n)
return (n*(n+1))/2
end
I'd probably suggest this:

Code: Select all

function sum(n)
local ret = 0
local n = n
while n >= 1 do
ret = ret + n
n = n - 1
end
return ret
end

But yours is a LOT better
HugoBDesigner - my Blog

substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

### Re: Small extra functions

HugoBDesigner wrote: ...
I also made a small function to get the average value of numbers:

Code: Select all

function math.med( ... )
local args = { ... }
local ret = 0
for i = 1, #args do
ret = ret + args[i]
end
return ret/#args
end

The average is called mean (or arithmetic mean) not median.

The median picks the midpoints of a (sorted) list of numbers:

Code: Select all

function math.med(...)
local a = {...}
table.sort(a, function (x, y) return x < y end)
if #a % 2 ~= 0 then
return a[math.ceil(#a/2)]
else
return a[#a/2]/2 + a[#a/2+1]/2
end
end

Here are a couple of statistics functions that I converted from a python file I used to simplify my stats calculations.

Code: Select all

-- calculate the mean (average) of a list
function math.mean(...)
local args = {...}
local r = 0
for i = 1, #args do
r = r + args[i]
end
return r/#args
end

-- calculate the standard deviation of a list
function math.sd(...)
local a = {...}
local m = math.mean(...)
local sd = 0
local l = #a
for i=1, l do
d = a[i]-m
sd = sd + d*d
end
sd = sd/l
return math.sqrt(sd)
end

-- calculate the standard units of a value
function math.su(val, mean, sd)
return (val-mean)/sd
end

-- return the list of standard units of a list
function math.sulist(...)
local a = {...}
local m = math.mean(...)
local sd = math.sd(...)
local su = {}
for i=1, #a do
su[#su+1] = math.su(a[i], m, sd)
end
return su
end

-- multiply two lists
function math.mulList(l1, l2)
if #l1 ~= #l2 then return nil end
local rs = {}
for i=1, #l1 do
rs[#rs+1]=l1[i]*l2[i]
end
return rs
end

-- calculate the correlation factor of two lists
function math.calcR(l1, l2)
if #l1 ~= #l2 then return nil end
local su1 = math.sulist(l1)
local su2 = math.sulist(l2)
return math.mean(math.mulList(su1, su2))
end

And for anyone who knows Python, here's the original .py file: Standard Deviation Calculator.py

Edits:
Fixed a couple of bugs in the code (such as passing a table instead of an unpacked one.)
math.med sorts the table before finding the midpoint.
Last edited by substitute541 on Thu Apr 24, 2014 6:08 am, edited 5 times in total.
Currently designing themes for WordPress.

Sometimes lurks around the forum.

OttoRobba
Party member
Posts: 104
Joined: Mon Jan 06, 2014 5:02 am
Location: Sao Paulo, Brazil

### Re: Small extra functions

Robin wrote:Looks like a good implementation of Fisher–Yates shuffle. Don't call math.randomseed, though. It should generally only be called at the start of the program, otherwise it becomes less random.
Ah yes, I don't know why I added it as part of the function here (specially since in my current code I generate a single random seed in the main.lua).

### Who is online

Users browsing this forum: No registered users and 7 guests