Skip to content

Commit

Permalink
Add math.SnapTo (#1888)
Browse files Browse the repository at this point in the history
* Add math.SnapTo

* Formatting consistency & small typo fix
  • Loading branch information
chev2 authored Jun 1, 2022
1 parent 61d4982 commit ee9fe73
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions garrysmod/lua/includes/extensions/math.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ math.Min = math.min
Name: EaseInOut(fProgress, fEaseIn, fEaseOut)
Desc: Provided by garry from the facewound source and converted
to Lua by me :p
Usage: math.EaseInOut(0.1, 0.5, 0.5) - all parameters shoule be between 0 and 1
Usage: math.EaseInOut(0.1, 0.5, 0.5) - all parameters should be between 0 and 1
-----------------------------------------------------------]]
function math.EaseInOut( fProgress, fEaseIn, fEaseOut )

if ( fEaseIn == nil ) then fEaseIn = 0 end
if ( fEaseOut == nil ) then fEaseOut = 1 end

if ( fProgress == 0 || fProgress == 1 ) then return fProgress end
if ( fProgress == 0 or fProgress == 1 ) then return fProgress end

local fSumEase = fEaseIn + fEaseOut

Expand All @@ -86,9 +86,9 @@ function math.EaseInOut( fProgress, fEaseIn, fEaseOut )

local fProgressCalc = 1 / ( 2 - fEaseIn - fEaseOut )

if( fProgress < fEaseIn ) then
if ( fProgress < fEaseIn ) then
return ( ( fProgressCalc / fEaseIn ) * fProgress * fProgress )
elseif( fProgress < 1 - fEaseOut ) then
elseif ( fProgress < 1 - fEaseOut ) then
return ( fProgressCalc * ( 2 * fProgress - fEaseIn ) )
else
fProgress = 1 - fProgress
Expand All @@ -103,7 +103,7 @@ function math.calcBSplineN( i, k, t, tinc )

if ( k == 1 ) then

if ( ( KNOT( i, tinc ) <= t ) && ( t < KNOT( i + 1, tinc ) ) ) then
if ( ( KNOT( i, tinc ) <= t ) and ( t < KNOT( i + 1, tinc ) ) ) then

return 1

Expand Down Expand Up @@ -199,7 +199,6 @@ function math.NormalizeAngle( a )
return ( a + 180 ) % 360 - 180
end


function math.AngleDifference( a, b )

local diff = math.NormalizeAngle( a - b )
Expand Down Expand Up @@ -227,3 +226,8 @@ end
function math.Remap( value, inMin, inMax, outMin, outMax )
return outMin + ( ( ( value - inMin ) / ( inMax - inMin ) ) * ( outMax - outMin ) )
end

-- Snaps the provided number to the nearest multiple
function math.SnapTo( num, multiple )
return math.floor( num / multiple + 0.5 ) * multiple
end

0 comments on commit ee9fe73

Please sign in to comment.