MathUtil
Revision as of 22:32, 26 August 2021 by JoeStrout (talk  contribs) (Created page with "In Mini Micro, <c>mathUtil</c> is an import module in the <c>/sys/lib</c> directory. It provides various additional mathrelated functions and constants. Like all th...")
In Mini Micro, mathUtil
is an import module in the /sys/lib
directory. It provides various additional mathrelated 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(radians)  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

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 normallydistributed 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 
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