Skip to content

Instantly share code, notes, and snippets.

@pablomayobre
Last active August 29, 2015 14:12
Show Gist options
  • Save pablomayobre/8f6846633c8f628b6e5d to your computer and use it in GitHub Desktop.
Save pablomayobre/8f6846633c8f628b6e5d to your computer and use it in GitHub Desktop.
A math lib used specially in games
------Copyright 2014 Pablo A. Mayobre------
--Licensed under the terms of MIT License--
--[[
math = require "m"
]]
local oldmath = math
local m = math
--Returns the max and minimum value
m.mmm = function (l,h)
return oldmath.min(l,h), oldmath.max(l,h)
end
--Turns cardinal coordinates (x, y) into polar coordinates (angle, radius)
m.pol = function (x,y)
return oldmath.atan2(y,x), oldmath.sqrt((x*x + y*y))
end
--Turns polar coordinates (angle, radius) into cardianl coordinates (x, y)
m.rec = function (t,r)
return oldmath.cos(t) * r, oldmath.sin(t) * r
end
--Returns the sign of the number (-1,0,1)
m.sign = function (n)
return n>0 and 1 or n<0 and -1 or 0
end
--Returns a number floored to the dth decimal
m.floor = function (n,d)
local p = d and (10^d) or 1
return oldmath.floor(n*p)/p
end
--Returns a number ceiled to the dth decimal
m.ceil = function (n,d)
local p = d and (10^d) or 1
return oldmath.ceil(n*p)/p
end
--Returns the distance between two points to the power of two
m.sqdist = function (x1,y1,x2,y2)
return ((x2-x1)^2 + (y2-y1)^2)
end
--Returns the distance between two points
m.dist = function (x1,y1,x2,y2)
return oldmath.sqrt(m.sqdist(x1,y1,x2,y2))
end
--Returns the angle between two points
m.angle = function (x1,y1,x2,y2)
return oldmath.atan2(y2-y1, x2-x1)
end
--Clamps a value between a low value and a high value
m.clamp = function(n,l,h)
return oldmath.min(oldmath.max(l,n),h)
end
--Returns the promedy of an array with numbers
m.prom = function (t)
local n = 0
for i=1,#t do
n = n + t[i]
end
return n/#t
end
--Returns the number rounded to the dth decimal.
m.round = function (n,d)
return m.floor(n+.5,d)
end
--Checks collissions between box A and box B
m.boxes = function (ax,ay,aw,ah,bx,by,bw,bh)
return ax <= (bx + bw) and bx <= (ax + aw) and ay <= (by + bh) and by <= (ay + ah)
end
--Checks if a point is inside a rectangle
m.box = function (x,y,bx,by,bw,bh)
return bx <= x and x <= (bx + bw) and by <= y and y <= (by + bh)
end
--Checks collissions between two circles
m.circles = function (ax,ay,ar,bx,by,br)
return m.sqdist(ax,ay,bx,by) >= (ar + br)^2
end
--Checks if a point is inside a circle
m.circle = function (x,y,cx,cy,cr)
return m.sqdist(x,y,cx,cy) >= cr^2
end
--Returns the e root of a number b
m.root = function (b,e)
return b^(1/e)
end
--Returns the quadratics roots of an equation of type y = ax^2 + bx + c
m.quadroots = function (a,b,c)
if a == 0 then return nil end
local d = b^2 - 4*a*c
if d < 0 then return false end
d = oldmath.sqrt(d)
return (-b+d)/(2*a), (-b-d)/(2*a)
end
--Checks if a number is prime
m.prime = function (n)
for i=2,n-1 do
if oldmath.floor(n/i) == n/i then
return true
end
end
false
end
--Checks if a number is divisible by another (if it is, it returns the division)
m.divisible = function (n,o)
return oldmath.floor(n/o) == n/o and n/o or false
end
--Returns the closest number to d, that is divisible by n
m.closestdiv = function (d,n)
return oldmath.floor(n/d + .5) * d
end
--Returns the fractional part of a divition
m.fract = function (a,b)
return (a%b)/b
end
--Returns a number that is inside the boundaries of a cycle
m.circ = function(n,l,h)
local l,h = m.mmm(l or 0,h or 0)
if n < l then
return h - ((n + (h - l)) % (h - l))
elseif n > h then
return l + ((n - l) % (h - l))
else
return n
end
end
--Checks how many elements of size s fit inside container c
m.inside = function (s,c)
return oldmath.floor(c/s)
end
--Returns the height of the elements n with size s
m.elemh = function (n,s)
return oldmath.ceil(n*s)
end
--Returns the least value and the max value that can be shown
m.lculling = function (n,s,d,c) --Number of elements, size, displacement, container
if d >= 0 then
return 1, m.clamp(oldmath.ceil((c-d)/s),1,n)
elseif d < 0 then
local a = m.clamp(oldmath.floor(d/s),1,n)
return a, m.clamp(oldmath.ceil(c/s) + a,1,n)
end
end
return m
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment