General math (Français)

Cette page répertorie les fonctions mathématiques qui sont utiles dans la programmation de jeux. Elles sont généralement courtes, simples et optimales.

-- Calculer la moyenne d'un nombre arbitraire d'angles (en radians).
function math.averageAngles(...)
	local x,y = 0,0
	for i=1,select('#',...) do local a= select(i,...) x, y = x+math.cos(a), y+math.sin(a) end
	return math.atan2(y, x)
end


-- Retourne la distance entre deux points
function math.dist(x1,y1, x2,y2) return ((x2-x1)^2+(y2-y1)^2)^0.5 end

-- Retourne la distance entre deux points 3D:
function math.dist(x1,y1,z1, x2,y2,z2) return ((x2-x1)^2+(y2-y1)^2+(z2-z1)^2)^0.5 end


-- Renvoie l'angle entre deux vecteurs supposant la même origine.
function math.angle(x1,y1, x2,y2) return math.atan2(y2-y1, x2-x1) end


-- Rrenvoie le multiple le plus proche de 'size' (10 par défaut).
function math.multiple(n, size) size = size or 10 return math.round(n/size)*size end


-- Fixe un nombre dans une certaine fourchette.
function math.clamp(low, n, high) return math.min(math.max(low, n), high) end


-- Interpolation linéaire entre deux nombres.
function lerp(a,b,t) return (1-t)*a + t*b end
function lerp2(a,b,t) return a+(b-a)*t end

-- Interpolation en cosinus entre deux nombres.
function cerp(a,b,t) local f=(1-math.cos(t*math.pi))*.5 return a*(1-f)+b*f end


-- Normaliser deux nombres.
function math.normalize(x,y) local l=(x*x+y*y)^.5 if l==0 then return 0,0,0 else return x/l,y/l,l end end


-- Retourne le nombre 'n' au décimal 'deci' le plus proche (par défaut, les nombres entiers).
function math.round(n, deci) deci = 10^(deci or 0) return math.floor(n*deci+.5)/deci end


-- Renvoie aléatoirement soit -1 soit 1.
function math.rsign() return love.math.random(2) == 2 and 1 or -1 end


-- Renvoie 1 si le nombre est positif, -1 s'il est négatif, ou 0.
function math.sign(n) return n>0 and 1 or n<0 and -1 or 0 end


-- Donne un nombre décimal aléatoire précis en fonction d'un minimum et d'un maximum.
function math.prandom(min, max) return love.math.random() * (max - min) + min end


-- Vérifie si deux segments de droite se croisent. Les segments de ligne sont donnés sous la forme ({x,y},{x,y}, {x,y},{x,y}).
function checkIntersect(l1p1, l1p2, l2p1, l2p2)
	local function checkDir(pt1, pt2, pt3) return math.sign(((pt2.x-pt1.x)*(pt3.y-pt1.y)) - ((pt3.x-pt1.x)*(pt2.y-pt1.y))) end
	return (checkDir(l1p1,l1p2,l2p1) ~= checkDir(l1p1,l1p2,l2p2)) and (checkDir(l2p1,l2p2,l1p1) ~= checkDir(l2p1,l2p2,l1p2))
end

-- Vérifie si deux lignes se croisent (ou des segments de ligne si seg est vrai)
-- Les lignes sont données sous forme de quatre nombres (deux coordonnées)
function findIntersect(l1p1x, l1p1y, l1p2x, l1p2y, l2p1x, l2p1y, l2p2x, l2p2y, seg1, seg2)
	local a1,b1,a2,b2 = l1p2y-l1p1y, l1p1x-l1p2x, l2p2y-l2p1y, l2p1x-l2p2x
	local c1,c2 = a1*l1p1x+b1*l1p1y, a2*l2p1x+b2*l2p1y
	local det,x,y = a1*b2 - a2*b1
	if det==0 then return false, "The lines are parallel." end
	x,y = (b2*c1-b1*c2)/det, (a1*c2-a2*c1)/det
	if seg1 or seg2 then
		local min,max = math.min, math.max
		if seg1 and not (min(l1p1x,l1p2x) <= x and x <= max(l1p1x,l1p2x) and min(l1p1y,l1p2y) <= y and y <= max(l1p1y,l1p2y)) or
		   seg2 and not (min(l2p1x,l2p2x) <= x and x <= max(l2p1x,l2p2x) and min(l2p1y,l2p2y) <= y and y <= max(l2p1y,l2p2y)) then
			return false, "The lines don't intersect."
		end
	end
	return x,y
end


Other Languages