An extension of type-enforcer with Math related data types and enforcer functions
Point model with helper methods
- Point
- new Point([x], [y])
- instance
- .set(x, [y]) ⇒
this
- .toString([suffix]) ⇒
string
- .valueOf() ⇒
Array
- .isSame(point2) ⇒
boolean
- .add(point) ⇒
Point
- .subtract(point) ⇒
Point
- .multiply(point) ⇒
Point
- .round([fractionDigits], [precision]) ⇒
Point
- .distance() ⇒
number
- .angle() ⇒
number
- .pointAtDistance(angle, distance) ⇒
Point
- .clone() ⇒
Point
- .set(x, [y]) ⇒
- static
- .isValid(value) ⇒
boolean
- .normalizeAngle(angle) ⇒
number
- .isValid(value) ⇒
Param | Type | Default |
---|---|---|
[x] | number , Array , object |
0 |
[y] | number |
0 |
Example
import { Point } from 'type-enforcer-math';
const point1 = new Point();
console.log(point1.toString());
// => '0,0'
const point2 = new Point({x:1, y:2});
console.log(point2.toString());
// => '1,2'
const point3 = new Point([3, 4]);
console.log(point3.toString());
// => '3,4'
const point4 = new Point(5, 6);
console.log(point4.toString());
// => '5,6'
console.log(point4.x);
// => 5
console.log(point4.y);
// => 6
Set x and y.
Param | Type | Description |
---|---|---|
x | number , string , Array , object , Point |
The x coordinate as a number (must also provide y param), or a comma separated string as 'x,y', or an Array as [x, y], or an object as { x: 0, y: 0 }, or another valid Point. |
[y] | number |
The y coordinate as a number. |
Get the point as a string with an optional suffix.
Param | Type | Description |
---|---|---|
[suffix] | string |
A suffix to append to each coordinate. |
Get the value of the point as an array.
Determine if another point is the same as this one.
Param | Type | Description |
---|---|---|
point2 | Point |
Another point. |
point.add(point) ⇒ Point
Adds the coordinates of another point to this one and returns a new point.
Param | Type | Description |
---|---|---|
point | Point |
Another point. |
point.subtract(point) ⇒ Point
Subtracts the coordinates of another point from this one and returns a new point.
Param | Type | Description |
---|---|---|
point | Point |
Another point. |
point.multiply(point) ⇒ Point
Multiplies the coordinates of another point with this one and returns a new point.
Param | Type | Description |
---|---|---|
point | Point |
Another point. |
point.round([fractionDigits], [precision]) ⇒ Point
Rounds the coordinates of this point and returns a new point.
Param | Type | Default | Description |
---|---|---|---|
[fractionDigits] | number |
0 |
Must be a positive integer or null. |
[precision] | number |
Significant digits. |
Finds the distance from point to origin (0, 0). Always returns a positive number or 0.
Finds the angle to this point from origin.
point.pointAtDistance(angle, distance) ⇒ Point
Returns a new point at a specific angle and distance from this point.
Param | Type | Description |
---|---|---|
angle | number |
The angle. |
distance | number |
The distance. |
point.clone() ⇒ Point
Get a clone of this point.
Determine if something is a valid point.
Param | Type | Description |
---|---|---|
value | unknown |
The value to check. |
Returns the same angle between 0 and 2 * PI.
Returns: number
- - The normalized angle.
Param | Type | Description |
---|---|---|
angle | number |
The angle to normalize. |