garrysmod icon indicating copy to clipboard operation
garrysmod copied to clipboard

Added: Linear space interpolation

Open dvdvideo1234 opened this issue 5 years ago • 8 comments

dvdvideo1234 avatar Mar 05 '19 08:03 dvdvideo1234

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 avatar Mar 05 '19 08:03 Kefta

@Kefta

Well it is in the math library ;) Done rename it to LinearSet

dvdvideo1234 avatar Mar 05 '19 09:03 dvdvideo1234

It doesn't have math. in front of it so right now it's a global function.

Kefta avatar Mar 05 '19 10:03 Kefta

@Kefta Done, thank you!

dvdvideo1234 avatar Mar 05 '19 10:03 dvdvideo1234

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 avatar Apr 12 '19 13:04 notcake

@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

dvdvideo1234 avatar Apr 16 '19 06:04 dvdvideo1234

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.

thegrb93 avatar Apr 16 '19 07:04 thegrb93

So, is it gonna be added? This is used for interpolation mainly.

dvdvideo1234 avatar Apr 17 '19 08:04 dvdvideo1234

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.

robotboy655 avatar Jan 17 '23 17:01 robotboy655

Neat.. Will try ;)

dvdvideo1234 avatar Jan 17 '23 20:01 dvdvideo1234