Namespace R.math.Math2D
A static class with methods and fields that are helpful
when dealing with two dimensional mathematics.
Defined in: math2d.js.
Constructor Attributes | Constructor Name and Description |
---|---|
Field Attributes | Field Name and Description |
---|---|
An approximation of the inverse of PI (0.31831)
|
|
An approximation of PI (3.14159)
|
|
An approximation of PI*2 (6.28318)
|
Method Attributes | Method Name and Description |
---|---|
boxBoxCollision(box1, box2)
Perform AAB (axis-aligned box) to AAB collision testing, returning true
if the two boxes overlap.
|
|
boxPointCollision(box, point)
Perform point to AAB collision, returning
true
if a collision occurs. |
|
bresenham(start, end)
Calculates all of the points along a line using Bresenham's algorithm.
|
|
circleCircleCollision(circle, velocity, targetCircle)
Helper method to determine if one circle will collide with another circle
based on its direction of movement.
|
|
convexHull(pts, k)
Calculate an approximate 2D convex hull for the given array of points.
|
|
degToRad(degrees)
Convert degrees to radians.
|
|
getBoundingBox(points, rect)
Calculate the smallest bounding box which contains
the given set of points.
|
|
getCenterOfPoints(points)
Get a point which represents the logical center of all of the
given points.
|
|
getDirectionVector(origin, baseVec, angle, vec)
A static method used to calculate a direction vector
from a heading angle.
|
|
Returns an identity matrix
|
|
isPointOnLine(point, anchor, vector)
Returns true if the point lies on the line defined by
anchor in the direction of the normalized vector.
|
|
lineBoxCollision(p1, p2, rect)
Test to see if a line intersects a Rectangle.
|
|
lineLineCollision(p1, p2, p3, p4)
Check to see if a line intersects another
|
|
minkDiff(hullA, hullB)
Determine the Minkowski Difference of two convex hulls.
|
|
pointInCircle(point, center, radius)
Determine if the given
point is within the circle defined by the
center and radius . |
|
pointInPoly(point, poly)
Determine if the given
point is within the polygon defined by the array of
points in poly . |
|
pointLeftOfLine(endPoint0, endPoint1, testPoint)
Tests if a point is Left|On|Right of an infinite line defined by
two endpoints.
|
|
radToDeg(radians)
Convert radians to degrees.
|
|
randomPoint(rect)
Given a R.math.Rectangle2D, generate a random point within it.
|
|
regularPolygon(sides, radius)
Generate an array of points which represents a regular polygon
with N sides.
|
|
rotationMatrix(angle, origin)
Returns a matrix which can be used to rotate points by
the given angle.
|
|
scalingMatrix(scaleX, scaleY)
Returns a matrix which can be used to scale points by
the given amounts.
|
|
transformPoints(points, matrix)
Transform a point or an array of points by the given matrix.
|
|
translationMatrix(vector)
Returns a matrix which can be used to translate points by
the given vector.
|
Field Detail
INV_PI
An approximation of the inverse of PI (0.31831)
PI
An approximation of PI (3.14159)
TWO_PI
An approximation of PI*2 (6.28318)
Method Detail
{Boolean}
boxBoxCollision(box1, box2)
Perform AAB (axis-aligned box) to AAB collision testing, returning true
if the two boxes overlap.
- Parameters:
- box1
- {R.math.Rectangle2D} The collision box of object 1
- box2
- {R.math.Rectangle2D} The collision box of object 2
- Returns:
- {Boolean} true if the rectangles overlap
{Boolean}
boxPointCollision(box, point)
Perform point to AAB collision, returning
true
if a collision occurs.
- Parameters:
- box
- {R.math.Rectangle2D} The collision box of the object
- point
- {R.math.Point2D} The point to test, in world coordinates
- Returns:
- {Boolean} true if the point is within the rectangle
{Array}
bresenham(start, end)
Calculates all of the points along a line using Bresenham's algorithm.
This method will return an array of points which need to be cleaned up
when done using them.
- Parameters:
- start
- {R.math.Point2D} The starting point for the line
- end
- {R.math.Point2D} The ending point for the line
- Returns:
- {Array} An array of R.math.Point2D. Be sure to destroy the points in the array when done using them.
{R.math.Vector2D}
circleCircleCollision(circle, velocity, targetCircle)
Helper method to determine if one circle will collide with another circle
based on its direction of movement. The circle's centers should be in
world coordinates.
- Parameters:
- circle
- {R.math.Circle2D} The first circle
- velocity
- {R.math.Vector2D} The first circle's velocity vector
- targetCircle
- {R.math.Circle2D} The second circle
- Returns:
- {R.math.Vector2D} The vector which keeps the two circles from overlapping, or null if they cannot overlap.
{Array}
convexHull(pts, k)
Calculate an approximate 2D convex hull for the given array of points.
Copyright 2001, softSurfer (www.softsurfer.com)
This code may be freely used and modified for any purpose
providing that this copyright notice is included with it.
SoftSurfer makes no warranty for this code, and cannot be held
liable for any real or imagined damage resulting from its use.
Users of this code must verify correctness for their application.
- Parameters:
- pts
- {Array} An array of R.math.Point2D instances
- k
- {Number} The approximation accuracy (larger = more accurate)
- Returns:
- {Array} An array of R.math.Point2D which contains the approximate hull of the given points
{Number}
degToRad(degrees)
Convert degrees to radians.
- Parameters:
- degrees
- {Number} An angle in degrees
- Returns:
- {Number} The degrees value converted to radians
{R.math.Rectangle2D}
getBoundingBox(points, rect)
Calculate the smallest bounding box which contains
the given set of points.
- Parameters:
- points
- {Array} An array of R.math.Point2D
- rect Optional
- {R.math.Rectangle2D} Optional rectangle to set to the bounding box
- Returns:
- {R.math.Rectangle2D} The bounding box of the points
{R.math.Point2D}
getCenterOfPoints(points)
Get a point which represents the logical center of all of the
given points.
- Parameters:
- points
- {Array} An array of R.math.Point2D
- Returns:
- {R.math.Point2D}
{R.math.Vector2D}
getDirectionVector(origin, baseVec, angle, vec)
A static method used to calculate a direction vector
from a heading angle.
- Parameters:
- origin
- {R.math.Point2D} The origin of the shape
- baseVec
- {R.math.Vector2D} The base vector
- angle
- {Number} The rotation in degrees
- vec Optional
- {R.math.Vector2D} optional. If provided, the result will be stored in this vector rather than creating a new one.
- Returns:
- {R.math.Vector2D} The direction vector
{Matrix}
identityMatrix()
Returns an identity matrix
- Returns:
- {Matrix}
{Boolean}
isPointOnLine(point, anchor, vector)
Returns true if the point lies on the line defined by
anchor in the direction of the normalized vector.
- Parameters:
- point
- {R.math.Point2D} The point to test
- anchor
- {R.math.Point2D} The anchor of the line
- vector
- {R.math.Vector2D} The normalized direction vector for the line
- Returns:
- {Boolean}
{Boolean}
lineBoxCollision(p1, p2, rect)
Test to see if a line intersects a Rectangle.
- Parameters:
- p1
- {R.math.Point2D} The start of the line
- p2
- {R.math.Point2D} The end of the line
- rect
- {R.math.Rectangle} The box to test against
- Returns:
- {Boolean} true if the line intersects the box
{Boolean}
lineLineCollision(p1, p2, p3, p4)
Check to see if a line intersects another
- Parameters:
- p1
- {R.math.Point2D} Start of line 1
- p2
- {R.math.Point2D} End of line 1
- p3
- {R.math.Point2D} Start of line 2
- p4
- {R.math.Point2D} End of line 2
- Returns:
- {Boolean} true if the lines intersect
{Array}
minkDiff(hullA, hullB)
Determine the Minkowski Difference of two convex hulls. Useful for
calculating collision response.
- Parameters:
- hullA
- {Array} An array of R.math.Point2D
- hullB
- {Array} An array of R.math.Point2D
- Returns:
- {Array} An array of R.math.Point2D which are the Minkowski Difference of the two hulls.
{Boolean}
pointInCircle(point, center, radius)
Determine if the given
point
is within the circle defined by the
center
and radius
.
- Parameters:
- point
- {R.math.Point2D} The point to test
- center
- {R.math.Point2D} The center of the circle
- radius
- {Number} The radius of the circle
- Returns:
- {Boolean}
true
if the point is within the circle
{Boolean}
pointInPoly(point, poly)
Determine if the given
point
is within the polygon defined by the array of
points in poly
.
- Parameters:
- point
- {R.math.Point2D} The point to test
- poly
- {Array} An array of
R.math.Point2D
- Returns:
- {Boolean}
true
if the point is within the polygon
{Number}
pointLeftOfLine(endPoint0, endPoint1, testPoint)
Tests if a point is Left|On|Right of an infinite line defined by
two endpoints.
- Parameters:
- endPoint0
- {R.math.Point2D} A point on the line
- endPoint1
- {R.math.Point2D} A second point on the line
- testPoint
- {R.math.Point2D} The point to test
- Returns:
- {Number} <0 (to left), 0 (on), >0 (to right)
{Number}
radToDeg(radians)
Convert radians to degrees.
- Parameters:
- radians
- {Number} An angle in radians
- Returns:
- {Number} The radians value converted to degrees
{R.math.Point2D}
randomPoint(rect)
Given a R.math.Rectangle2D, generate a random point within it.
- Parameters:
- rect
- {R.math.Rectangle2D} The rectangle
- Returns:
- {R.math.Point2D} A random point within the rectangle
{Array}
regularPolygon(sides, radius)
Generate an array of points which represents a regular polygon
with N sides.
- Parameters:
- sides
- {Number} The number of sides in the polygon, must be more than 2.
- radius Optional
- {Number} The radius for the polygon. Default: 100
- Returns:
- {Array} an array of R.math.Point2D
{Matrix}
rotationMatrix(angle, origin)
Returns a matrix which can be used to rotate points by
the given angle.
- Parameters:
- angle
- {Number} The rotation, in degrees
- origin Optional
- {R.math.Point2D} Optional origin to rotate around
- Returns:
- {Matrix}
{Matrix}
scalingMatrix(scaleX, scaleY)
Returns a matrix which can be used to scale points by
the given amounts. Providing neither the scale along X nor Y will
return the identity matrix.
- Parameters:
- scaleX
- {Number} Scale along the X axis, null for 1.0
- scaleY
- {Number} Scale along the Y axis, null to use the X scaling amount
- Returns:
- {Matrix}
transformPoints(points, matrix)
Transform a point or an array of points by the given matrix. This method
transforms the points by mutating them.
- Parameters:
- points
- {R.math.Point2D|Array} A single point or an array of R.math.Point2D
- matrix
- {Matrix} The matrix to transform the points with
{Matrix}
translationMatrix(vector)
Returns a matrix which can be used to translate points by
the given vector.
- Parameters:
- vector
- {R.math.Vector2D} The translation vector
- Returns:
- {Matrix}