From 3950a3b2fa78b7a928c0437d98105d79c13a922f Mon Sep 17 00:00:00 2001 From: iamcheeseman Date: Sun, 8 Mar 2026 18:32:14 -0400 Subject: add util functions; add resources --- src/utils.lua | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'src/utils.lua') diff --git a/src/utils.lua b/src/utils.lua index c3e4bea..b2186cc 100644 --- a/src/utils.lua +++ b/src/utils.lua @@ -37,3 +37,58 @@ function dist(x1, y1, x2, y2) local dy = y1 - y2 return (dx*dx + dy*dy)^0.5 end + +mathx = {} + +mathx.tau = math.pi * 2 + +function mathx.sign(x) + if x == 0 then + return 0 + end + return x < 0 and -1 or 1 +end + +function round(x) + return math.floor(x + 0.5) +end + +function clamp(a, min, max) + return math.min(max, math.max(a, min)) +end + +function snap(x, step) + return math.floor(x / step) * step +end + +function frac(x) + return x - math.floor(x) +end + +local function angle_diff(a, b) + local diff = (b - a) % (math.pi * 2) + return (2 * diff) % (math.pi * 2) - diff +end + +function lerp_angle(a, b, t) + return a + angle_diff(a, b) * (1 - 0.5^t) +end + +function dot(x, y, xx, yy) + return x*xx + y*yy +end + +function vec_sqlen(x, y) + return x*x + y*y +end + +function vec_len(x, y) + return math.sqrt(x*x + y*y) +end + +function rotate_vec(x, y, r) + local angle = math.atan2(y, x) + r + local len = mathx.vec_len(x, y) + return math.cos(angle) * len, math.sin(angle) * len +end + -- cgit v1.3-2-g0d8e