Skip to content

Commit

Permalink
added mathx.inverse_lerp, mathx.remap_range, mathx.remap_range_clamped
Browse files Browse the repository at this point in the history
  • Loading branch information
1bardesign committed Aug 10, 2023
1 parent 097c19b commit b019767
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions mathx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ function mathx.bilerp(a, b, c, d, u, v)
)
end

--get the lerp factor on a range, inverse_lerp(6, 0, 10) == 0.6
function mathx.inverse_lerp(v, min, max)
return (v - min) / (max - min)
end

--remap a value from one range to another
function mathx.remap_range(v, in_min, in_max, out_min, out_max)
return mathx.lerp(out_min, out_max, mathx.inverse_lerp(v, in_min, in_max))
end

--remap a value from one range to another, staying within that range
function mathx.remap_range_clamped(v, in_min, in_max, out_min, out_max)
return mathx.lerp(out_min, out_max, mathx.clamp01(mathx.inverse_lerp(v, in_min, in_max)))
end

--easing curves
--(generally only "safe" for 0-1 range, see mathx.clamp01)

Expand Down

0 comments on commit b019767

Please sign in to comment.