point.js
v0.0.5
Published
A JavaScript utility for point calculations
Maintainers
Readme
Point.js
Point.js is a JavaScript library for point calculations. Point.js has a general definition of "point" and most methods takes multiple parameters, allowing for complex, interdependent chaining and transformations.
Install
npm i point.jsUsage
Point.js is exported as a named export, under P. It can be instantiated with or without using the new operator.
import { P } from 'point.js';
const point = P()
// const point = new P()
// Do some math with multiple parameters and different point signatures
point.add([ 20, 33 ], { x: 2, y: 5 }, 5)
// => P { x: 27, y: 43 }
// Can be chained
point.mult(1.5).floor()
// => P { x: 40, y: 64 }
// End with transformnation
point.toArray()
// => [ 40, 64 ]API
- Static methods
P.add(...points: GeneralPoint[]): PointP.sub(...points: GeneralPoint[]): PointP.mult(...points: GeneralPoint[]): PointP.div(...points: GeneralPoint[]): PointP.mod(...points: GeneralPoint[]): PointP.pow(...points: GeneralPoint[]): PointP.random(...args: [max: number] | [min: number, max: number]): PointP.min(...points: GeneralPoint[]): PointP.max(...points: GeneralPoint[]): Point
- Instance methods
p.set(...point: [GeneralPoint] | [x: number, y: number] | undefined[]): thisp.add(...points: GeneralPoint[]): thisp.sub(...points: GeneralPoint[]): thisp.mult(...points: GeneralPoint[]): thisp.div(...points: GeneralPoint[]): thisp.mod(...points: GeneralPoint[]): thisp.pow(...points: GeneralPoint[]): thisp.ceil(precision?: number): thislp.floor(precision?: number): thisp.round(precision?: number): thisp.trunc(precision?: number): thisp.sq(): thisp.sqrt(): thisp.cb(): thisp.cbrt(): thisp.abs(): thisp.inv(): thisp.clamp([upper: number] | [lower: number, upper: number]): thisp.between(point: GeneralPoint, distance?: number): thisp.getSum(): thisp.getDistSq(point: GeneralPoint): thisp.getDist(point: GeneralPoint): thisp.clone(): thisp.copy(point: Point): thisp.random(...args: [max: number] | [min: number, max: number]): thisp.min(...points: GeneralPoint[]): thisp.max(...points: GeneralPoint[]): thisp.operation(resolver: (n: number) => number): thisp.transform<T>(resolver: (p: this) => T): Tp.check(resolver: (p: this) => boolean): booleanp.is(point: GeneralPoint, threshold?: number): booleanp.toObject(): {x: number, y: number}p.eject(): {x: number, y: number}p.toArray(): [x: number, y: number]p.toString(): `{x: ${number}, y: ${number}}`p.clg(): this
Static methods
P.add(...points: GeneralPoint[]): Point
Adds addends, instantiating P with the sum.
P.sub(...points: GeneralPoint[]): Point
Subtracts subtrahends, instantiating P with the difference.
P.mult(...points: GeneralPoint[]): Point
Multiplies multiplicands, instantiating P with the product.
P.div(...points: GeneralPoint[]): Point
Divides divisors, instantiating P with the product.
P.mod(...points: GeneralPoint[]): Point
Divides divisors, instantiating P with the modulus.
P.pow(...points: GeneralPoint[]): Point
Exponentiates exponents, instantiating P with the product.
P.random(...args: [max: number] | [min: number, max: number]): Point
Generates a random number, instantiating P with the result.
P.min(...points: GeneralPoint[]): Point
Resolves the min x and min y of points, instantiating P with the result.
P.max(...points: GeneralPoint[]): Point
Resolves the max x and max y of points, instantiating P with the result.
Instance methods
p.set(...point: [GeneralPoint] | [x: number, y: number] | undefined[]): this
Sets this to a given point.
p.add(...points: GeneralPoint[]): this
Adds addends to this, mutating this to the sum.
p.sub(...points: GeneralPoint[]): this
Subtracts subtrahends from this, mutating this to the difference.
p.mult(...points: GeneralPoint[]): this
Multiplies this by multiplicands, mutating this to the product.
p.div(...points: GeneralPoint[]): this
Divides this by divisors, mutating this to the quotient.
p.mod(...points: GeneralPoint[]): this
Divides this by divisors, mutating this to the modulus.
p.pow(...points: GeneralPoint[]): this
Exponentiates this by exponents, mutating this to the product.
p.ceil(precision?: number): this
Mutates this by rounding up to precision.
p.floor(precision?: number): this
Mutates this by rounding down to precision.
p.round(precision?: number): this
Mutates this by rounding to precision.
p.trunc(precision?: number): this
Mutates this by rounding towards 0 to precision.
p.sq(): this
Mutates this by its square.
p.sqrt(): this
Mutates this by its square root.
p.cb(): this
Mutates this by its cube.
p.cbrt(): this
Mutates this by its cube root.
p.abs(): this
Mutates this to its absolute.
p.inv(): this
Mutates this to its inverse.
p.clamp([upper: number] | [lower: number, upper: number]): this
Mutates this by clamping it within the inclusive lower and upper bounds.
P(10, -10).clamp(-5, 5)
// => P { x: 5, y: -5 }p.between(point: GeneralPoint, distance?: number): this
Mutates this to the point between this and point.
The fraction of the distance is determined by the multiplicand distance.
distance is expected to be a number between 0-1.
where 0 becomes the value ofthis and 1 the value of point
P(10, -10).between(-20, 20)
// => P { x: -5, y: 5 }p.getSum(): number
Gets the sum of this.
p.getDistSq(point: GeneralPoint): number
Get the square distance between this and point.
p.getDist(point: GeneralPoint): number
Get the distance between this and point.
p.clone(): this
Clones this to a new instance of P.
const p = P(10, -10)
const clone = p.clone()
Object.is(p, clone) // => false
p.is(clone) // => true p.copy(point: Point): this
Copies the properties of point to this.
Essentially an alias for this.set .
const p = P(10, -10)
const otherP = P(0, 0)
p.copy(otherP)
Object.is(p, clone) // => false
p.is(clone) // => true p.random(...args: [max: number] | [min: number, max: number]): this
Mutates this to a random number.
p.min(...points: GeneralPoint[]): this
Mutates this to the respective min x and min y of this and points.
p.max(...points: GeneralPoint[]): this
Mutates this to the respective max x and max y of this and points.
p.operation(resolver: (n: number) => number): this
Mutates this by executing a method on its properties.
P(NaN, 10).operation(n => Number.isNaN(n) ? 0 : n)
// => P { x: 0, y: 10 }p.transform<T>(resolver: (p: this) => T): T
Transforms this to the return type of the resolver.
P(20, 10).transform(p => ({ width: p.x, height: p.y }))
// => P { width: 20, height: 10 }p.check(resolver: (p: this) => boolean): boolean
Executes a test on this.
P(NaN, 10).operation(p => Number.isNaN(p.x) || Number.isNaN(p.y))
// => truep.is(point: GeneralPoint, threshold?: number): boolean
Compares the properties of this with point within the threshold.
P(10, 10).is([11,11]) // => false
P(10, 10).is([13,7], 5) // => truep.toObject(): {x: number, y: number}
Transforms this into an object.
p.eject(): {x: number, y: number}
Alias for p.toObject.
p.toArray(): [x: number, y: number]
Transforms this into an array.
p.toString(): `{x: ${number}, y: ${number}}`
Transforms this into a string.
p.clg(): this
Executes console.log on this.
P(20, 10).clg()
/*
___In the console___
P {x: 20, y: 10}
*/