garrysmod
garrysmod copied to clipboard
Added: Linear space interpolation
You should put this in the math library and rename it to LinearSet or something more descriptive of its single dimensional nature. Also, add some bound checks to prevent infinite loops. Lastly, you should match the codebase: add spaces around the parentheses and put the loop body and return on separate lines. No reason to make the variable names undescriptive either, just use the ones in your docs.
@Kefta
Well it is in the math library ;) Done rename it to LinearSet
It doesn't have math.
in front of it so right now it's a global function.
@Kefta Done, thank you!
You should put this in the math library and rename it to LinearSet or something more descriptive of its single dimensional nature.
It's called linspace
in matlab
https://www.mathworks.com/help/matlab/ref/linspace.html
@notcake Yes, that is correct. Take a look at the commit : https://github.com/Facepunch/garrysmod/pull/1563/commits/1696b902ea77005a2b7f34898baa8156aef4e779 . I guess you need me to default the samples count parameter. This seems reasonable. Thanks.
Taughts @Kefta
Why are we adding matlab functions to gmod's math library? It's cool and all, but next to nothing is going to use it. Keep the libraries slim, they're meant to provide commonly needed things, not every thing.
So, is it gonna be added? This is used for interpolation mainly.
I agree with @thegrb93, this is a quite narrow use case function, we already have a linear interpolation function (https://wiki.facepunch.com/gmod/Global.Lerp, vector and angle versions also exist), you can generate a table of points easily enough with it.
function math.LinearSet( nStart, nEnd, nAmount )
local t = {}
for i=0, nAmount - 1 do
table.insert( t, Lerp( i / (nAmount - 1), nStart, nEnd ) )
end
return t
end
PrintTable( math.LinearSet(0, 10, 5) ) --> {0, 2.5, 5, 7.5, 10 }
PrintTable( math.LinearSet(-5,5,7) ) --> { -5.0000, -3.3333, -1.6667, 0, 1.6667, 3.3333, 5.0000 }
Your implementation doesn't even match what matlab function does.
Neat.. Will try ;)