Last active
April 8, 2017 06:27
-
-
Save AnastasiaDunbar/b5f889e2c6124a448d745a1884c2631a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function BoundingBox(x1,y1,w1,h1, x2,y2,w2,h2) | |
return x1 < x2+w2 and | |
x2 < x1+w1 and | |
y1 < y2+h2 and | |
y2 < y1+h1 | |
end | |
function math.round(n,deci)deci=10^(deci or 0)return math.floor(n*deci+.5)/deci end | |
function math.round(x)return math.floor(x+.5)end | |
function math.sign(n) return n>0 and 1 or n<0 and -1 or 0 end | |
function lerp(a,b,t) return(1-t)*a+t*b end | |
function clamp(val,min,max)return math.max(min,math.min(val,max))end | |
function split(s,delimiter) --Splt string, please escape delimiter | |
result={}; | |
for match in (s..delimiter):gmatch("(.-)"..delimiter) do | |
table.insert(result,match); | |
end | |
return result; | |
end | |
function chars(s) --Split string into a table of characters | |
local t={} | |
for i=1,#s do | |
table.insert(t,string.sub(s,i,i)) | |
end | |
return t | |
end | |
local function shuffleTable(t) | |
local iterations=#t | |
for i=iterations,2,-1 do | |
local j=math.random(i) | |
t[i],t[j]=t[j],t[i] | |
end | |
return t | |
end | |
function stringify(object,indent,depth) | |
depth=depth or 1 | |
local newline=#indent>0 and "\n" or "" | |
if(type(object)=="table")then | |
local tbl={}; | |
local isArray=true; | |
local i=1 | |
for key,value in pairs(object)do | |
if(key~=i)then --If the key doesn't follow order | |
isArray=false | |
break | |
end | |
i=i+1 | |
end | |
for key,value in pairs(object)do | |
table.insert(tbl,string.rep(indent,depth)..(isArray and "" or '["'..key..'"]=')..stringify(value,indent,depth+1)) | |
end | |
return "{"..newline..table.concat(tbl,","..newline)..newline..string.rep(indent,depth-1).."}" | |
else | |
if(type(object)=="string")then | |
return'"'..string.gsub(string.gsub(object,"\n","\\n"),"\t","\\t")..'"' | |
else | |
return tostring(object) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment