Math & Collisions library

Showcase your libraries, tools and other projects that help your fellow love users.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: mlib: Math Library

Post by davisdude »

I have no idea why I didn't do that earlier... :P
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: mlib: Math Library

Post 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
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: mlib: Math Library

Post 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 )
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: mlib: Math Library

Post 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
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: mlib: Math Library

Post 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
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Hexenhammer
Party member
Posts: 175
Joined: Sun Feb 17, 2013 8:19 am

Re: mlib: Math Library

Post 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.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: mlib: Math Library

Post 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!
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: mlib: Math Library

Post 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.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: mlib: Math Library

Post 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
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: mlib: Math Library

Post 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...)
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 2 guests