• UtilX -- Extending GLua -- Need community input
    371 replies, posted
Stay on topic. This thread should not die, it has way too much potential. [code] function utilx.CalcCirclePos( start_x, start_y, angle, radius ) angle = math.NormalizeAngle( angle ) local x = start_x + math.Round( math.cos( angle ) * radius ) local y = start_y + math.Round( math.sin( angle ) * radius ) return x, y end [/code] You can use this function in your [i]GM:HudPaint( )[/i] hooks to get the X and Y pos of whatever you wanna draw. [b]TIP:[/b] To make a clock, simply pass [i]CurTime( )[/i] as the "angle" parameter. [b]Example:[/b] [code] GM:HUDPaint( ) local x, y = utilx.CalcCirclePos( ScrW( ) / 2, ScrH( ) / 2, CurTime( ), 128 ) draw.RoundedBox( 8, x, y, 16, 16, Color( 255, 50, 50, 100 ) ) end [/code] [b]Output:[/b] Draws a small red circle that rotates clock-wise 128 units away friom the center of the screen.
Is OP still doing this or should I put this together?
I already did it (with documentation), will add the last few entries :)
[lua]function _R.Entity:NearestOBB(invector) local outvector = Vector() local obbmin, obbmax = self:OBBMins(), self:OBBMaxs() for i = 1,3 do outvector[i] = math.Clamp(invector[i],obbmin[i], obbmax[i]) end return outvector end[/lua] Gets the closest point to invector on the entity's OBB. invector must be in local coordinates. untested but my previous implementation of this algorithm used to work nicely
[QUOTE=The-Stone;18077164]I already did it (with documentation), will add the last few entries :)[/QUOTE] Thanks for doing this. I've been really busy lately with other projects, I appreciate it :D
[lua]function utilx.CombineFunctions( func_a, func_b ) local func = function() func_a() func_b() end return func end[/lua] I haven't been following up on this thread so I don't know if this has been made before. Combines 2 functions. I haven't found much of a use for it, but it works.
i know i subbmitted something that was useless and didnt work before, but that was probably the worst thing i've ever coded. here's a percent of error function: [php] function utilx.PercentError( estimate, actual ) local diff = estimate - actual local div = diff / actual local abs = math.abs(div) local error = abs * 100 return error end [/php] this just returns how far off you are with estimates. this could be used in a guessing minigame... And a single line approach, i guess... [php] function utilx.PercentError( estimate, actual ) return math.abs(estimate - actual / actual) * 100 end [/php]
actual/actual? anyway, does utilx need basic math functions?
[QUOTE=TomyLobo;18295111]actual/actual? anyway, does utilx need basic math functions?[/QUOTE] I guess it needs everything that users are able to bring up :)
[QUOTE=TomyLobo;18295111]actual/actual? anyway, does utilx need basic math functions?[/QUOTE] Ah... Calculus Sum and Product functions would be nice. I've had to use them a bit for generalizations.
[QUOTE=TomyLobo;18295111]actual/actual? anyway, does utilx need basic math functions?[/QUOTE] Anything that isn't in gLua by default that's useful, Guess so.
[lua] function utilx.GenerateFuckedUpNumber( n1, n2, n3, n4, n5 ) --Generates a fucked up number that has absolutely nothing to do with the arguments given but it still changes the outcome somehow!!!!! local fuckedup = n1 - n2 / n3 * n2 + n1 - math.fmod( n4, n1 ) * n1 * n3 - n2 + 29849346.2 - math.pi * math.Rand( n1 / 1000, n4 * 1000 ) / 8 * math.fmod( n2 + n3, n5 ) + CurTime() * 99999 / math.calcBSplineN( n1, n4, n5, n2 ) + 8 * n1 * 8 + math.TimeFraction( n5, n2, n4 ) fuckedup = fuckedup + math.ceil( fuckedup - n3 + n4 ) / n5 fuckedup = math.fmod( math.pow( fuckedup, 4 ), 99999 ) return fuckedup + 99999 end[/lua] [editline]10:25PM[/editline] Will give a different outcome every single time you call it, I promise! Oh, can also be used for benchmarking. Calling this function every frame will sure stress your PC! :v:
So is basically a completely random number? Could be useful, thank you!
[QUOTE=Dlaor;18302795][lua] function utilx.GenerateFuckedUpNumber( n1, n2, n3, n4, n5 ) --Generates a fucked up number that has absolutely nothing to do with the arguments given but it still changes the outcome somehow!!!!! local fuckedup = n1 - n2 / n3 * n2 + n1 - math.fmod( n4, n1 ) * n1 * n3 - n2 + 29849346.2 - math.pi * math.Rand( n1 / 1000, n4 * 1000 ) / 8 * math.fmod( n2 + n3, n5 ) + CurTime() * 99999 / math.calcBSplineN( n1, n4, n5, n2 ) + 8 * n1 * 8 + math.TimeFraction( n5, n2, n4 ) fuckedup = fuckedup + math.ceil( fuckedup - n3 + n4 ) / n5 fuckedup = math.fmod( math.pow( fuckedup, 4 ), 99999 ) return fuckedup + 99999 end[/lua] [editline]10:25PM[/editline] Will give a different outcome every single time you call it, I promise![/QUOTE] Why would you do that when you can just use math.random?
[php] function utilx.PercentError( estimate, actual ) return math.abs( (estimate - actual) / actual) * 100 end [/php] i think that fixes it. it's supposed to be the absolute value of estimate minus actual over actual, times 100. [editline]04:16PM[/editline] really big: [php] function utilx.mathHUUUGE(power) return math.huge()^math.huge()^math.huge()^power end [/php] just thought i'd add this, not sure if it works.
[QUOTE=Salads;18305924]Why would you do that when you can just use math.random?[/QUOTE] I agree. It's still based on the same seed so it'll just return a uselessly large (in size) number, but no more random. [B]edit : [/B] Well actually it really does what it says. it returns a fucked up number. [QUOTE=ZenX2;18306139][php]really big: [php] function utilx.mathHUUUGE(power) return math.huge()^math.huge()^math.huge()^power end [/php] just thought i'd add this, not sure if it works.[/QUOTE] First it's not actually a function, you simply call it as math.huge. Also it's not exactly a value, it represents infinity and is something that is bigger then anything you throw at it, except itself. [lua]print(math.huge = math.huge^math.huge^math.huge^5) --Prints true[/lua] :science: [url=http://wiki.garrysmod.com/?search=math.huge&go=Go][I]math.huge[/I] ([B]Wiki link [img]http://wiki.garrysmod.com/favicon.ico[/img][/b])[/url]
1 / 0 == math.huge, fyi.
wow, that's a shocker :O
[QUOTE=Kogitsune;18309032]1 / 0 == math.huge, fyi.[/QUOTE] Oh that would make sense. And it's right too. :smile:
I just realized math.Clamp can be written like this: [lua]function math.Clamp( val, min, max ) return math.max( min, math.min( val, max ) ) end[/lua] Not sure if it's faster though.
[QUOTE=Overv;18699643]I just realized math.Clamp can be written like this: [lua]function math.Clamp( val, min, max ) return math.max( min, math.min( val, max ) ) end[/lua] Not sure if it's faster though.[/QUOTE] Hmm, that's pretty cool. And ZenX2, stop posting equations from your high-school algebra book. That math.huge thing was fail.
[QUOTE=Overv;18699643]I just realized math.Clamp can be written like this: [lua]function math.Clamp( val, min, max ) return math.max( min, math.min( val, max ) ) end[/lua] Not sure if it's faster though.[/QUOTE] It's probably the same thing? Also if you really want to rewrite it : [lua]function math.Clamp( val, min, max ) return val <= max and (val >= min and val or min) or max end[/lua]
Bumpty bump, this is a very useful thread full of many handy snippets. Would be a shame to lose them all.
[QUOTE=notRzilla;25570570]Bumpty bump, this is a very useful thread full of many handy snippets. Would be a shame to lose them all.[/QUOTE] so the thread would disappear if you didn't bump it? also gbps, add the merged version to the op
What I meant to say is that this project needs to be revived.
[QUOTE=CapsAdmin;25570879]so the thread would disappear if you didn't bump it?[/QUOTE] Inactive threads are removed after a while actually.
Oh hi my thread
This is great and should be kept forever.
[QUOTE=notRzilla;25570570]Bumpty bump, this is a very useful thread full of many handy snippets. Would be a shame to lose them all.[/QUOTE] Could move a lot of these to the wiki, if they haven't already made it there. [url]http://wiki.garrysmod.com/?title=Common_Code_Snippets[/url]
Always hated how i had to use a loop to find someone from a string. Probably already been contributed and cleaner then this but what the hey. And im just coding this out of my head so im not sure if this is right. [code] function utilx.findplayer(str) FoundPlayers={} for k,v in pairs(player.GetAll()) do if string.find(v:Nick(),str) or string.find(v:SteamID(),str) then table.Insert(FoundPlayers,v) end end if #FoundPlayers>1 or #FoundPlayers==0 then return nil else return FoundPlayers[1] end FoundPlayers={} end [/code]
Sorry, you need to Log In to post a reply to this thread.