Page 2 of 5

Re: mlib: Math Library

Posted: Sun Sep 29, 2013 9:20 pm
by davisdude
I have no idea why I didn't do that earlier... :P

Re: mlib: Math Library

Posted: Mon Sep 30, 2013 2:33 pm
by Ref
Not too bright here but doesn't

Code: Select all

function median(tab)
	table.sort(tab)
	if #tab%2 == 0 then
		return (tab[math.floor(#tab/2)]+tab[math.floor(#tab/2+1)])/2
		else
		return tab[#tab/2+.5]
		end
	end
get you to the same place as

Code: Select all

function mlib.get_median( ... )
	name = { ... }
	for i = 1, #name do
		if #name > 2 then
			least = math.min( unpack( name ) )
			largest = math.max( unpack( name ) )
			for ii = 1, #name do
				if name[ii] == least then 
					table.remove( name, ii )
					break
				end
			end
			for ii = 1, #name do
				if name[ii] == largest then
					table.remove( name, ii )
					break
				end
			end
		elseif #name == 2 then
			median = mlib.get_mean( name[1], name[2] )
			return median
		elseif #name == 1 then
			median = name[1]
			return median
		end
	end
end
Just wondering.
Best
Edit: If you want to retain the original order of the table you could add:

Code: Select all

local tab = {unpack(tab)}
as the first line of my function

Re: mlib: Math Library

Posted: Tue Oct 01, 2013 10:28 pm
by davisdude
Updated GitHub! Functions are now localized, refined, and get_mode returns how many times the number repeats as well.
You can also use straight tables now instead of having to do

Code: Select all

mlib.get_mode( unpack( tab ) )
you can do

Code: Select all

mlib.get_mode( tab )

Re: mlib: Math Library

Posted: Wed Oct 02, 2013 5:48 pm
by Ref
davisdude wrote:Updated GitHub! Functions are now localized( tab )[/code]
Might want to avoid unnecessary variable creation by replacing:

Code: Select all

function mlib.get_midpoint( x1, y1, x2, y2 )
	local name = { ( x1 + x2 ) / 2, ( y1 + y2 ) / 2 }
	return name
end
with:

Code: Select all

function mlib.get_midpoint( x1, y1, x2, y2 )
	return  { ( x1 + x2 ) / 2, ( y1 + y2 ) / 2 }
end
No biggy, just a suggestion.
Best

Re: mlib: Math Library

Posted: Wed Oct 02, 2013 11:34 pm
by davisdude
I read somewhere that it was faster if you assigned variables then called them as opposed to just calling them. I don't know if it's true or not, or if it even really impacts it that much, but... :P

Re: mlib: Math Library

Posted: Thu Oct 03, 2013 10:27 pm
by Hexenhammer
davisdude wrote:I read somewhere that it was faster if you assigned variables then called them as opposed to just calling them. I don't know if it's true or not, or if it even really impacts it that much, but... :P
Assigning the table to a variable is completely pointless here and only makes your code more verbose. If it has any effect on performance at all it will make things slower.

Re: mlib: Math Library

Posted: Thu Oct 03, 2013 11:06 pm
by davisdude
Hexenhammer wrote:
davisdude wrote:I read somewhere that it was faster if you assigned variables then called them as opposed to just calling them. I don't know if it's true or not, or if it even really impacts it that much, but... :P

Assigning the table to a variable is completely pointless here and only makes your code more verbose. If it has any effect on performance at all it will make things slower.
I guess that does make more sense, but I just thought it would make the code neater anyway... :)
Fixed!

Re: mlib: Math Library

Posted: Mon Nov 11, 2013 2:12 am
by davisdude
Sorry for double posting!

Updated! Added getPolygonArea and removed get_convex_area and get_triangle_area since they are repetitive. Check out the GitHub for the update!

By the way, just in case any of you are curious, I used the Shoelace Formula.

Re: mlib: Math Library

Posted: Tue Nov 12, 2013 1:55 am
by davisdude
Sorry for triple posting! (Please don't get mad! :cry: )

v. 1.4.1 is now released! You can now get the centroid of a non-intersecting polygon! mlib is also a localized variable now!
I also have a simple little demo if you want to check it out! I may expand upon it/make more if you guys like it! Just an idea... :P

Re: mlib: Math Library

Posted: Fri Nov 15, 2013 12:21 am
by davisdude
The shame! 4 consecutive posts... :(
Somebody stop the madness! :shock:
Anyway, 1.5.0 is out and I'm working on more functions at the moment.
As always, please make sure to vote and comment if you want any particular function/don't like how the arguments work/have any issue.
Enjoy! :)
(I'm working on a better demo right now, so ignore that bit for now...)