## Math & Collisions library

davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

### Math & Collisions library

NOTE: This thread has been moved to here!

Hello ladies and gents! Here is MLib:

What?
MLib is a math and collisions library.

Check out the GitHub to see more information.

History:
Most recent updates are in red
v. 2.0.0 -Made mlib.shape and made numberous bug fixes.

v. 1.9.4 -Made mlib.math.prime faster and removed ability to test multiple numbers at once. Thanks Robin!
v. 1.9.3 -Fixed polygon.area and polygon.centroid
v. 1.9.2 -Updated to 0.9.0.
v. 1.9.1 -Made mlib.line.closestPoint able to take either two points on the slope or the slope and intercept.
v. 1.9.0 -Added mlib.lineSegmentIntersects (no affiliation with previous one (changed to mlib.line.segment.intersect)) and mlib.line.closestPoint
v. 1.8.3 -Changed naming mechanism to be more organized.
v. 1.8.2 -"Fixed" mlib.lineSegmentsIntersect AGAIN!!!!
v. 1.8.1 -Removed a print statement.
v. 1.8.0 -mlib.pointInPolygon added
v. 1.7.5 -mlib.lineSegmentsIntersect vertical lines fixed again. This time for real. I promise... or hope, at least...
v. 1.7.4 -mlib.lineSegmentsIntersect vertical parallels fixed
v. 1.7.3 -mlib.lineSegmentsIntersect parallels fixed
v. 1.7.2 -mlib.lineSegmentsIntersect now handles vertical lines
v. 1.7.1 -mlib.lineSegmentsIntersect now returns the two places in between where the line segments begin to intersect.
v. 1.7.0 -Added mlib.circlesIntersect, mlib.pointOnLineSegment, mlib.linesIntersect, and mlib.lineSegmentsIntersect
v. 1.6.1 -Employed usage of summations for mlib.getPolygonArea and mlib.getPolygonCentroid and removed area as an argument for mlib.getPolygonCentroid.
v. 1.6.0 -Added several functions.
v. 1.5.0 -Made lots of changes to syntax to make it easier to use (hopefully). I also put out specs.
v. 1.4.1 -Localized mlib. Thanks, Yonaba!
v. 1.4.0 -Added mlib.getPolygonCentroid (gets the midpoint of a non-self-intersecting polygons)
v. 1.3.2 -Made mlib.getPrime take tables as arguments, so you can check all the values of a table.
v. 1.3.1 -Changed name method to mlib.getPolygonArea
v. 1.3.0 -Added mlib.get_polygon_area and removed mlib.get_convex_area and mlib.get_triangle_area since they are repetitive.
v. 1.2.2 -Made functions return faster, functions that previously returned tables now return multiple arguments.
v. 1.2.1 -Localized functions, made tables acceptable as arguments, refined function speed, mlib.get_mode now returns number most repeated as well as how many times.
v. 1.2.0 -Added mlib.get_angle
v. 1.1.0 -Added mlib.get_convex_area
v. 1.0.4 -Fixed get_mode to handle bimodials.
v. 1.0.3 -Prime Checker optimized (hopefully final update on this.)
v. 1.0.2 -Prime checker now works! (At least to 1000. I haven't tested any further)
v. 1.0.1 -'Fixed' the prime checker
v. 1.0.0 -Initial release
Attachments
mlib.lua
2.0.0
Last edited by davisdude on Tue Dec 23, 2014 6:13 pm, edited 64 times in total.
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
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

### Re: mlib: Math Library

Why would the prime function return nil if it's true? That's just unpractical.

I can't look at your code right now, but if you can't find a better solution then you can always do

Code: Select all


local old = mlib.is_prime
function mlib.is_prime(a)
if old(a) == nil then
return true
end
return false
end

davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

### Re: mlib: Math Library

Why would the prime function return nil if it's true? That's just unpractical.

I can't look at your code right now, but if you can't find a better solution then you can always do

Code: Select all


local old = mlib.is_prime
function mlib.is_prime(a)
if old(a) == nil then
return true
end
return false
end
Yeah, I tried that before. Somehow, it didn't work. Sorry it took so long to reply. I sort of forgot to reply.
Last edited by davisdude on Mon Nov 11, 2013 2:15 am, edited 1 time in total.
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

I know it's a bit quick on the draw, but while I was waiting I went ahead and did mlib.get_convex_area( ... ) because I can't stand doing nothing.
I'm also currently working on mlib.get_concave_area( ... ), but I'm still working on a formula. If you know please don't tell me. I want to try getting it on my own.
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
ArchAngel075
Party member
Posts: 317
Joined: Mon Jun 24, 2013 5:16 am

### Re: mlib: Math Library

Amazing library! Ive thoroughly abused the quickness of get_distance in about 95% of my projects.

Though i have a request :

In a project i have a player. The player is made using love.physics.
I needed this player to ALLWAYS be facing the direction of the mouse. Sadly looking around for a get Angle Between Two Known Points didnt turn out well.
I did find the dotProduct method, but on implementing it , i had strange bugs and no idea why i failed at it.
Later i thought up my on method thanks to the sine-rule of triangles.

Anyhow on to the ACTUALK request, could you perhaps provide a function that takes 2 points ([x],[y],optional [z]) and provides the angle between those two points? Im sure this would be helpful to others!
---

If you'd like my version of a get_angle_between I'd happily provide it, it is 100% working with all quadrants. Though it has no z unless i know how to get a z from a triangle with all variables known (vertices A,B,C | Angles a,b,c)
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

### Re: mlib: Math Library

ArchAngel075 wrote: Anyhow on to the ACTUALK request, could you perhaps provide a function that takes 2 points ([x],[y],optional [z]) and provides the angle between those two points? Im sure this would be helpful to others!

Code: Select all

math.atan2 ( -dy, dx )
As for 3d, you would normally get two angles: azimuth and bearing. Bearing is obtained via X and Y coordinates only, as the name suggests. Azimuth is obtained via same function, using Z as dy, and the magnitude of XY vector as dx.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

### Re: mlib: Math Library

I'm glad you like my library!
I will work on that function. It shouldn't take too long, I'll post it when I'm available.
On a side note, your bugs might have been caused by using degrees instead of radians.
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

Updated! Check out the readme on GitHub (link in original post) on how to use!
By the way, I'm now accepting formula for concave area at this point. I tried, and it's HARD!
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
ArchAngel075
Party member
Posts: 317
Joined: Mon Jun 24, 2013 5:16 am

### Re: mlib: Math Library

thanks for the angle function!

Btw, looking at your functions ive noticed you use the default screen width/height... Yet some projects may change the width/height.

Perhaps its best to write it to use the love.graphics.getWidth/Height by default? This not technically break anything and only requires some swapping out.

Just a suggestion, if you have a good reason for not using the getWidth/Height functions then alls good
Ref
Party member
Posts: 693
Joined: Wed May 02, 2012 11:05 pm

### Re: mlib: Math Library

davisdude wrote:I know it's a bit quick on the draw, but while I was waiting I went ahead and did mlib.get_convex_area( ... ) because I can't stand doing nothing.
I'm also currently working on mlib.get_concave_area( ... ), but I'm still working on a formula. If you know please don't tell me. I want to try getting it on my own.
Should be able to do in 8 lines of code (or less).

### Who is online

Users browsing this forum: No registered users and 21 guests