Coords.js is a lightweight math library for point and vector calculations in javascript. Modeled loosely off of PVector from processing.
There are two main object types that can be created with coords.js: Points and Vectors. A point is a simple object that consists of an x coordinate and a y coordinate. A vector is an object calculated from two points that represents the vector between those points. Vectors, at their base, also consist of an x and y value, here representative of the two axes that make up the vector, as well as several calculated properties. While there are a lot of apparent similarities between the vector and point object types, the conceptual distinction is important to maintain, as vectors represent a unit of change, and points represent a static position.
To create a new point, initialize as so:
var myPoint = CPoint(x, y)
// myPoint was created with properties myPoint.x and myPoint.y
x
: The x value for the pointy
: The y value for the point
add(Point)
: Add the values of another point to the current point.sub(Point)
: Subtract the values of another point from the current point.mult(factor)
: Multiply the values of the current point by a numberdiv(factor)
: Divide the values of the current point by a numberdist(Point)
: The distance between the current point and any given point.
To create a new vector, do this:
var start = CPoint(5,10);
var end = CPoint(7,12);
var myVector = CVector(start, end);
// myVector was created and properties were calculated
start
: The first point of the vectorend
: The second point of the vectorx
: The x value for the vectory
: The y value for the vectorcenter
: The center point along the vectorunit
: The unit vector object --unit.x
: x value of the unit vector --unit.y
: y value of the unit vector --unit.scalar
: The value used to divide the vector into the unit vectordirection
: The direction of the vector, denoted with either 1, 2, 3, or 4, corresponding with the quadrant the normalized vector would be in.magnitude
: The length of the vector
add(Vector)
: Adds the values of a given vector to the current vectorsub(Vector)
: Subtracts the values of a given vector from the current vectormult(scalar)
: Multiplies the current vector by a given scalar. The scalar can be supplied as a function requesting argument v to perform more complex conditional multiplication if desired.div(scalar)
: Divides the current vector by a given scalar The scalar can be supplied as a function requesting argument v to perform more complex conditional multiplication if desired.norm()
: Sets the value of the vector to the value of the unit vector (original value can be recovered later if necessary by multiplying the vector by it's own unit.scalar property.translate(Point)
: Move the vector by a given x and y valuerotate(centerPoint, radians)
: Rotate the vector around a centerPoint by a given number of radians.mirror(axis, position)
: Reflect the vector across either thex
ory
axis, at a given position. Foraxis
supply a string containing either "x" or "y". For position, give the y or x position that you want to use for x or y axis (respectively) to be mirrored over.