JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 46
  • Score
    100M100P100Q62588F
  • License ISC

Library for polygons operations

Package Exports

  • @slydock/poly-js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@slydock/poly-js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Poly-JS

Poly-JS is a library for manipulating 2D polygons, shapes, points...

Features

  1. 2D vertices operations
  2. 2D lines operations
  3. 2D polygons operations
  4. 2D rect operations
  5. 2D circle operations

Installation

npm i @slydock/poly-js

Basic Usage

For any component need, you'll need to import the plugin

const {
    Vector2,
    Line,
    Polygon,
    Rect,
    Circle
} =  require('@slydock/poly-js');

Vector2

Vector2 Class

Kind: global class
Properties

Name Type Description
x float

The x value of the vertice

y float

The y value of the vertice

new Vector2(p1, p2)

Param Type Description
p1 Vector2 | float | null

Vector2: copy, float: assign, null: 0

p2 float | null

float: assign, null: 0

Example

// returns Vector2 with x:0 & y:0
new Vector2()

Example

// returns Vector2 with x:2 & y:2
new Vector2(2)

Example

// returns Vector2 with x:4 & y:5
new Vector2(4, 5)

Example

// returns Vector2 with x:1 & y:3
new Vector2({x: 1, y: 3})

Example

// returns Vector2 with x:6 & y:7
const v1 = new Vector2(6, 7);
new Vector2(v1);

vector2.approximated ⇒

Getter for approximate() method

Kind: instance property of Vector2
Returns:

Vector2


See: approximate
Example

const v1 = new Vector2(Math.PI, Math.PI);
console.log(v1.toString()); // {x: 3.141592653589793, y: 3.141592653589793}
console.log(v1.approximate.toString()); // {x: 3.1416, y: 3.1416}

vector2.normalized ⇒

Normalize the Vector2

Kind: instance property of Vector2
Returns:

Vector2


See: norm
Example

const v1 = new Vector2(1, 1);
console.log(v1.norm.toString()); // {x: 0.7071067811865476, y: 0.7071067811865476}

vector2.norm ⇒

Alias of normalized

Kind: instance property of Vector2
Returns:

Vector2


See: normalized

vector2.magnitude ⇒

Get the magnitude of the Vector2

Kind: instance property of Vector2
Returns:

float


Example

const v1 = new Vector2(12, 32);
console.log(v1.magnitude); // 34.17601498127012

vector2.clone ⇒

Get a clone of this Vector2 useful for not modify a vector before operation

Kind: instance property of Vector2
Returns:

Vector2


Example

const v1 = new Vector2(5, 5);
console.log(v1.clone.add(2, 3)); // {x: 7, y: 8}
// v1 is unchanged

vector2.floor(v) ⇒

Flooring the Vector2

Kind: instance method of Vector2
Returns:

Vector2

Param Type Default Description
v float 1

Precision

vector2.floorX([v]) ⇒

Flooring the x value of the Vector2

Kind: instance method of Vector2
Returns:

Vector2

Param Type Default Description
[v] float 1

Precision

vector2.floorY([v]) ⇒

Flooring the y value of the Vector2

Kind: instance method of Vector2
Returns:

Vector2

Param Type Default Description
[v] float 1

Precision

vector2.ceil([v]) ⇒

Ceiling the Vector2

Kind: instance method of Vector2
Returns:

Vector2

Param Type Default Description
[v] float 1

Precision

vector2.ceilX([v]) ⇒

Ceiling the x value of the Vector2

Kind: instance method of Vector2
Returns:

Vector2

Param Type Default Description
[v] float 1

Precision

vector2.ceilY([v]) ⇒

Ceiling the y value of the Vector2

Kind: instance method of Vector2
Returns:

Vector2

Param Type Default Description
[v] float 1

Precision

vector2.round([v]) ⇒

Rounding the Vector2

Kind: instance method of Vector2
Returns:

Vector2

Param Type Default Description
[v] float 1

Precision

vector2.roundX([v]) ⇒

Rounding the x value of the Vector2

Kind: instance method of Vector2
Returns:

Vector2

Param Type Default Description
[v] float 1

Precision

vector2.roundY([v]) ⇒

Rounding the y value of the Vector2

Kind: instance method of Vector2
Returns:

Vector2

Param Type Default Description
[v] float 1

Precision

vector2.set(p1, p2) ⇒

Setting the value of the Vector2

Kind: instance method of Vector2
Returns:

Vector2

Param Type Description
p1 Vector2 | float | null

Vector2: copy, float: assign

p2 float | null

float: assign, null: 0

Example

const v1 = new Vector2();
// change v1 Vector2 to x:3 & y:3
v1.set(3);

Example

const v1 = new Vector2();
// change v1 Vector2 to x:3 & y:4
v1.set(3, 4);

Example

const v1 = new Vector2();
// change v1 Vector2 to x:5 & y:6
v1.set({x:5, y:6});

Example

const v1 = new Vector2();
const v2 = new Vector2(7, 8);
// change v1 Vector2 to x:7 & y:8
v1.set(v2);

vector2.setX(v) ⇒

Setting the x value of the Vector2

Kind: instance method of Vector2
Returns:

Vector2

Param Type Description
v float

The value

vector2.setY(v) ⇒

Setting the y value of the Vector2

Kind: instance method of Vector2
Returns:

Vector2

Param Type Description
v float

The value

vector2.add(p1, p2) ⇒

Adding values to the the Vector2

Kind: instance method of Vector2
Returns:

Vector2

Param Type Description
p1 Vector2 | float | null

Vector2: copy, float: assign

p2 float | null

float: assign, null: 0

Example

const v1 = new Vector2(2, 3);
// add  3 to v1 Vector2
v1.add(3);
// v1 values are now x:5 & y:6

Example

const v1 = new Vector2(2, 3);
// add 5, 7 to v1 Vector2
v1.add(5, 7);
// v1 values are now x:7 & y:10

Example

const v1 = new Vector2(2, 3);
// add  3 to v1 Vector2
v1.add({x:1, y:2});
// v1 values are now x:3 & y:5

Example

const v1 = new Vector2(2, 3);
const v2 = new Vector2(4, 1);
// add  3 to v1 Vector2
v1.add(v2);
// v1 values are now x:6 & y:4, v2 is unchanged

vector2.addX(v) ⇒

Adding v to x value of the Vector2

Kind: instance method of Vector2
Returns:

Vector2

Param Type Description
v float

The value

vector2.addY(v) ⇒

Adding v to y value of the Vector2

Kind: instance method of Vector2
Returns:

Vector2

Param Type Description
v float

The value

vector2.substract(p1, p2) ⇒

Substracting values to the the Vector2

Kind: instance method of Vector2
Returns:

Vector2

Param Type Description
p1 Vector2 | float | null

Vector2: copy, float: assign

p2 float | null

float: assign, null: 0

Example

const v1 = new Vector2(2, 3);
// Substracting  3 to v1 Vector2
v1.substract(3);
// v1 values are now x:-1 & y:0

Example

const v1 = new Vector2(2, 3);
// Substracting 5, 7 to v1 Vector2
v1.substract(5, 7);
// v1 values are now x:-3 & y:-4

Example

const v1 = new Vector2(2, 3);
// Substracting  3 to v1 Vector2
v1.substract({x:1, y:2});
// v1 values are now x:1 & y:1

Example

const v1 = new Vector2(2, 3);
const v2 = new Vector2(4, 1);
// Substracting 3 to v1 Vector2
v1.substract(v2);
// v1 values are now x:-2 & y:2, v2 is unchanged

vector2.substractX(v) ⇒

Substracting v to x value of the Vector2

Kind: instance method of Vector2
Returns:

Vector2

Param Type Description
v float

The value

vector2.substractY(v) ⇒

Substracting v to x value of the Vector2

Kind: instance method of Vector2
Returns:

Vector2

Param Type Description
v float

The value

vector2.multiply(p1, p2) ⇒

Multiply values to the the Vector2

Kind: instance method of Vector2
Returns:

Vector2

Param Type Description
p1 Vector2 | float | null

Vector2: copy, float: assign

p2 float | null

float: assign, null: 0

Example

const v1 = new Vector2(2, 3);
// Multiply 3 to v1 Vector2
v1.multiply(3);
// v1 values are now x:6 & y:9

Example

const v1 = new Vector2(2, 3);
// Multiply 5, 7 to v1 Vector2
v1.multiply(5, 7);
// v1 values are now x:10 & y:21

Example

const v1 = new Vector2(2, 3);
// Multiply  3 to v1 Vector2
v1.multiply({x:1, y:2});
// v1 values are now x:2 & y:6

Example

const v1 = new Vector2(2, 3);
const v2 = new Vector2(4, 1);
// Multiply 3 to v1 Vector2
v1.multiply(v2);
// v1 values are now x:8 & y:3, v2 is unchanged

vector2.multiplyX(v) ⇒

Multiply x value of the Vector2 by v

Kind: instance method of Vector2
Returns:

Vector2

Param Type Description
v float

The multiplicator

vector2.multiplyY(v) ⇒

Multiply y value of the Vector2 by v

Kind: instance method of Vector2
Returns:

Vector2

Param Type Description
v float

The multiplicator

vector2.divide(p1, p2) ⇒

Dividing values to the the Vector2

Kind: instance method of Vector2
Returns:

Vector2

Param Type Description
p1 Vector2 | float | null

Vector2: copy, float: assign

p2 float | null

float: assign, null: 0

Example

const v1 = new Vector2(12, 9);
// Dividing v1 Vector2 by 3
v1.divide(3);
// v1 values are now x:4 & y:3

Example

const v1 = new Vector2(10, 15);
// Dividing v1 Vector2 by 2, 5
v1.divide(2, 5);
// v1 values are now x:5 & y:3

Example

const v1 = new Vector2(5, 8);
// Dividing v1 Vector2 by 2, 4
v1.divide({x:2, y:4});
// v1 values are now x:2.5 & y:2

Example

const v1 = new Vector2(12, 10);
const v2 = new Vector2(6, 2);
// Dividing v1 Vector2 by 6, 2
v1.divide(v2);
// v1 values are now x:2 & y:5, v2 is unchanged

vector2.divideX(v) ⇒

Divinding x value of the Vector2 by v

Kind: instance method of Vector2
Returns:

Vector2

Param Type Description
v float

The divisor

vector2.divideY(v) ⇒

Divinding x value of the Vector2 by v

Kind: instance method of Vector2
Returns:

Vector2

Param Type Description
v float

The divisor

vector2.invert() ⇒

Inverting the Vector2

Kind: instance method of Vector2
Returns:

Vector2


Example

const v1 = new Vector2(3, 6);
v1.invert();
// v1 values are now x:-3 & y:-6

vector2.invertX() ⇒

Inverting the x value of the Vector2

Kind: instance method of Vector2
Returns:

Vector2


Example

const v1 = new Vector2(3, 6);
v1.invertX();
// v1 values are now x:-3 & y:6

vector2.invertY() ⇒

Inverting the y value of the Vector2

Kind: instance method of Vector2
Returns:

Vector2


Example

const v1 = new Vector2(3, 6);
v1.invertY();
// v1 values are now x:3 & y:-6

vector2.rotate(angle) ⇒

Rotate the Vector2 arround zero by angle

Kind: instance method of Vector2
Returns:

Vector2

Param Type Description
angle float

degrees angle

Example

const v1 = new Vector2(3, 6);
v1.rotate(180);
// v1 values are now x:-3 & y:-6

vector2.equals() ⇒

Test if the Vector2 is equal to an other

Kind: instance method of Vector2
Returns:

boolean


Example

const v1 = new Vector2(3, 6);
const v2 = new Vector2(4, 6);
return v1.equals(v2); // false

vector2.equalsX() ⇒

Test if the x value of the Vector2 is equal to an other

Kind: instance method of Vector2
Returns:

boolean


Example

const v1 = new Vector2(3, 6);
const v2 = new Vector2(4, 6);
return v1.equalsX(v2); // false

vector2.equalsY() ⇒

Test if the y value of the Vector2 is equal to an other

Kind: instance method of Vector2
Returns:

boolean


Example

const v1 = new Vector2(3, 6);
const v2 = new Vector2(4, 6);
return v1.equalsY(v2); // true

vector2.isInside(polygon) ⇒

Test if the current Vector2 is inside a polygon

Kind: instance method of Vector2
Returns:

boolean

Param Type Description
polygon Polygon

The polygon to check

Example

const polygon = new Polygon([
    new Vector2(0,0),
    new Vector2(10,0),
    new Vector2(10,10),
    new Vector2(0,10)
]);
const v1 = new Vector2(5, 5);
return v1.isInside(polygon); // true

vector2.approximate() ⇒

Modify the current Vector2 and get an approximated value Can be usefull for removing near duplicate vertices

Kind: instance method of Vector2
Returns:

Vector2


See: approximated
Example

const v1 = new Vector2(Math.PI, Math.PI);
console.log(v1.toString()); // {x: 3.141592653589793, y: 3.141592653589793}
console.log(v1.approximate().toString()); // {x: 3.1416, y: 3.1416}

vector2.toString() ⇒

Getting JSON string version of the Vector2

Kind: instance method of Vector2
Returns:

string


Vector2.Distance(v1, v2) ⇒

Getting the distance between two Vector2

Kind: static method of Vector2
Returns:

float

Param Type Description
v1 Vector2

first Vector2

v2 Vector2

second Vector2

Vector2.Max(v1, v2) ⇒

Getting the max values from two Vector2

Kind: static method of Vector2
Returns:

Vector2

Param Type Description
v1 Vector2

first Vector2

v2 Vector2

second Vector2

Vector2.Min(v1, v2) ⇒

Getting the min values from two Vector2

Kind: static method of Vector2
Returns:

Vector2

Param Type Description
v1 Vector2

first Vector2

v2 Vector2

second Vector2

Vector2.Lerp(v1, v2, t) ⇒

Getting a Lerp Vector2 from v1 to v2 with t time

Kind: static method of Vector2
Returns:

Vector2

Param Type Description
v1 Vector2

first Vector2

v2 Vector2

second Vector2

t float

time of lerp, value should be between 0 and 1

Vector2.LerpUnclamped(v1, v2, t) ⇒

Getting a Lerp Vector2 from v1 to v2 with t time (Unclamped version)

Kind: static method of Vector2
Returns:

Vector2

Param Type Description
v1 Vector2

first Vector2

v2 Vector2

second Vector2

t float

time of lerp, value should be between 0 and 1, but it can exceed this values

Vector2.Zero()

A zero Vector2

Kind: static method of Vector2

Vector2.Top()

A top Vector2

Kind: static method of Vector2

Vector2.Left()

A left Vector2

Kind: static method of Vector2

Vector2.Bottom()

A bottom Vector2

Kind: static method of Vector2

Vector2.Right()

A right Vector2

Kind: static method of Vector2