MathUtil
		
		
		
		
		
		
		Jump to navigation
		Jump to search
		
		
		
		
		
		
		
	
In Mini Micro, mathUtil is an import module in the /sys/lib directory.  It provides various additional math-related functions and constants.
Like all the modules in /sys/lib, the best documentation for mathUtil is the source code (/sys/lib/mathUtil.ms) itself.  But this page summarizes the content in more concise form.
Constant
| Name | Value / Purpose | 
|---|---|
| mathUtil.e | Euler's number, roughly 2.718; base of natural logarithms | 
Methods
The following methods are normally accessed via the mathUtil prefix, e.g. mathUtil.radToDeg(pi).
| Method | Returns | 
|---|---|
| ln(x) | natural logarithm of x | 
| radToDeg(radians) | converts radians into degrees | 
| degToRad(degrees) | converts degrees into radians | 
| moveTowards(num, targetNum, maxChange=1) | returns a value closer to targetNum from num, differing from num by no more than maxChange) | 
| moveTowardsXY(mover, target, maxDist=1) | like moveTowards, but mutates map mover to approach target, each with x and y properties (e.g., a Sprite)
 | 
| distance(p1, p2) | find the distance between two points; each may be a map containing x and y', or an [x, y] list | 
| lerp(a, b, t) | returns a value some portion t of the way from a to b | 
| lerp2d(p1, p2, t) | same as lerp, but on 2d points (maps or lists)
 | 
| proportionAlongLine(endA, endB, p) | returns how far p is along the line from endA to endB | 
| nearestPointOnLine(endA, endB, p) | returns closest point to p on the line from endA to endB as an [x, y] list | 
| nearestPointOnLineSegment(endA, endB, p) | same as nearestPointOnLine, but limited to not go beyond the range from endA to endB
 | 
| distanceToLine(endA, endB, p) | returns distance from p to the line from endA to endB | 
| distanceToLineSegment(endA, endB, p) | same as distanceToLine, but limited to not go beyond the range from endA to endB
 | 
| lineIntersectProportion(p1, p2, p3, p4) | look for an intersection between line p1-p2 and line p3-p4. Return null if none found. | 
| lineSegmentsIntersect(p1, p2, p3, p4) | return whether the line segment p1-p2 intersects segment p3-p4. | 
| lineLineIntersection(p1, p2, p3, p4) | return the point at which line p1-p2 intersects line p3-p4. If there is no intersection, this function returns null. | 
| reflect(vector, normal) | reflects a vector across a given normal. This does the classic "angle of incidence equals angle of reflection" thing, where the angle is measured relative to the given normal vector. MiniMicro Only! | 
| bounceVelocity(ball, lineSeg, friction=0.1) | Calculates the new velocity of something like a ball, after colliding with a (possibly moving) line segment. MiniMicro Only! | 
| bounceOffSegment(ball, segment, friction=0.1, fracBefore=null) | Bounce a ball-like object -- which is attempting to move from ball.x-ball.vx, ball.y-ball.vy to ball.x, ball.y -- off of a line segment. MiniMicro Only! | 
| bounceOffStaticPoly(ball, polyPts, friction=0.1) | Bounce a ball-like object -- which is attempting to move from ball.x-ball.vx, ball.y-ball.vy to ball.x, ball.y -- off of a stationary polygon with points defined by polyPts. Returns true if bounced, false if not. MiniMicro Only! | 
| bounceOffPoly(ball, polyPts, prevPolyPts=null, friction=0.1) | Bounce a ball-like object -- which is attempting to move from ball.x-ball.vx, ball.y-ball.vy to ball.x, ball.y -- off of a polygon with points defined by polyPts, possibly also moving from a previous position at prevPolyPts. Returns true if bounced, false if not. MiniMicro Only! | 
| bounceOffMovingPoly(ball, polyPts, prevPolyPts=null, friction=0.1) | Bounce a ball-like object -- which is attempting to move from ball.x-ball.vx, ball.y-ball.vy to ball.x, ball.y -- off of a polygon with points defined by polyPts, also moving from a previous position at prevPolyPts. Returns true if bounced, false if not. MiniMicro Only! | 
| polyPerimeter(polygon) | returns total distance around polygon (given as list of [x, y]] points) | 
| polyArea(polygon) | returns area of polygon (given as list of [x, y]] points) | 
| pointInPoly(point, polygon) | returns true if point (an [x, y] point) is contained within polygon; or if point is a list of ['x, y] points, returns true if any of those points are within polygon | 
| offsetPoly(polygon, delta) | returns a new polygon by insetting/growing polygon by the given amount | 
| shiftPoly(polygon, dx, dy) | returns a new polygon by translating polygon by the given amounts | 
| rotatePoly(polygon, degrees) | returns a new polygon by rotating polygon by degrees clockwise | 
| randNormal(mean=0, stddev=1) | returns a normally-distributed random number | 
| randRange(min, max) | returns a random number between min and max | 
| dice(numberOfDice=1, sides=6) | roll numberOfDice dice, each with values from 1 to sides, and return the sum | 
| clamp(x, minval=0, maxval'=1) | return x limited to the range minval to maxval | 
| max(a, b) | returns the greater of two values. | 
| min(a, b) | returns the lesser of two values. | 
| numToStr(n, precision) | convert n to a string, with a specified number of digits past the decimal place, with trailing zeros | 
Example
The following program creates a sprite, then uses mathUtil.moveTowardsXY to make it chase the mouse.
import "mathUtil"
s = new Sprite
s.image = file.loadImage("/sys/pics/Wumpus.png")
display(4).sprites.push s
while true
	mathUtil.moveTowardsXY s, mouse, 10
	yield
end while