JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 16
  • Score
    100M100P100Q67360F
  • 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');

Classes

Line

Line Class

Polygon

Polygon Class

Vector2

Vector2 Class

Line

Line Class

Kind: global class
Properties

Name Type Description
start Vector2 The start position of the line
end Vector2 The end position of the line

new Line([p1], [p2])

Param Type Description
[p1] Vector2 | object Vector2 like object of the start position
[p2] Vector2 | object Vector2 like object of the end position

line.length ⇒

Length of the line

Kind: instance property of Line
Returns: float

line.intersect(line) ⇒

Get the intersection of this line with an other one Return false if lines does not intersect

Kind: instance method of Line
Returns: boolean|Vector2

Param Type Description
line Line Second line

Line.Intersect(l1, l2) ⇒

Get the intersection of two lines Return false if lines does not intersect

Kind: static method of Line
Returns: boolean|Vector2

Param Type Description
l1 Line First line
l2 Line Second line

Polygon

Polygon Class

Kind: global class
Properties

Name Type Description
first Vector2 The first point of the polygon, follow next on first to iterate
region Array.<Array>
points Array.<Vector2>
points Array.<Line>
area Number

new Polygon([vertices])

Create a new Polygon

Param Type Description
[vertices] Array.<Vector2> Vertices of the polygon

polygon.region

Get region for polybooljs plugin, can be also be used for GeoJson Return a double level array ([[x, y], [x, y], ...])

Kind: instance property of Polygon

polygon.points

Get points of the Polygon

Kind: instance property of Polygon

polygon.lines

Get lines of the Polygon

Kind: instance property of Polygon

polygon.area

Get area of the Polygon

Kind: instance property of Polygon

polygon.pointIsInside(point) ⇒ boolean

Check if a point is inside a polygon

Kind: instance method of Polygon

Param Type Description
point Vector2 Point to check

polygon.setChildren(polygons) ⇒ Polygon

Setting all children polygons (for holes)

Kind: instance method of Polygon

Param Type Description
polygons Array.<Polygon> Children to set

polygon.addChild(polygon) ⇒ Polygon

Adding a child polygon (for hole)

Kind: instance method of Polygon

Param Type Description
polygon Polygon Child polygon to add

polygon.setVertexes(vertices) ⇒ Polygon

Setting vertices of the polygon

Kind: instance method of Polygon

Param Type Description
vertices Array.<Vector2> Vertices to set

polygon.addVertex(vertice) ⇒ Polygon

Add a new vertices to the end

Kind: instance method of Polygon

Param Type Description
vertice Vector2 the vertice to add

polygon.colliding(polygon) ⇒ Boolean

Is the current polygon colliding with the given polygon

Kind: instance method of Polygon

Param Type Description
polygon Polygon the second polygon for the collision

polygon.intersect(polygon) ⇒ Array.<Polygon>

Intersect boolean operation on this polygon with the given polygon

Kind: instance method of Polygon

Param Type Description
polygon Polygon the second polygon for the operation

polygon.difference(polygon) ⇒ Array.<Polygon>

Difference boolean operation on this polygon with the given polygon

Kind: instance method of Polygon

Param Type Description
polygon Polygon the second polygon for the operation

polygon.union(polygon) ⇒ Array.<Polygon>

Union boolean operation on this polygon with the given polygon

Kind: instance method of Polygon

Param Type Description
polygon Polygon the second polygon for the operation

polygon.xor(polygon) ⇒ Array.<Polygon>

Xor boolean operation on this polygon with the given polygon

Kind: instance method of Polygon

Param Type Description
polygon Polygon the second polygon for the operation

Polygon.Colliding(p1, p2) ⇒ Boolean

Is the p1 polygon colliding with the p2 polygon

Kind: static method of Polygon

Param Type Description
p1 Polygon the first polygon for the collision
p2 Polygon the second polygon for the collision

Polygon.FromRegion(region) ⇒ Polygon

Convert region to polygon

Kind: static method of Polygon

Param Type Description
region Array.<Array> the region to convert

Polygon.Intersect(polygon, polygon) ⇒ Array.<Polygon>

Intersect boolean operation on the p1 polygon with the p2 polygon

Kind: static method of Polygon

Param Type Description
polygon Polygon the first polygon for the operation
polygon Polygon the second polygon for the operation

Polygon.Union(polygon, polygon) ⇒ Array.<Polygon>

Union boolean operation on the p1 polygon with the p2 polygon

Kind: static method of Polygon

Param Type Description
polygon Polygon the first polygon for the operation
polygon Polygon the second polygon for the operation

Polygon.Difference(polygon, polygon) ⇒ Array.<Polygon>

Difference boolean operation on the p1 polygon with the p2 polygon

Kind: static method of Polygon

Param Type Description
polygon Polygon the first polygon for the operation
polygon Polygon the second polygon for the operation

Polygon.Xor(polygon, polygon) ⇒ Array.<Polygon>

Xor boolean operation on the p1 polygon with the p2 polygon

Kind: static method of Polygon

Param Type Description
polygon Polygon the first polygon for the operation
polygon Polygon the second polygon for the operation

Vector2

Vector2 Class

Kind: global class
Properties

Name Type Description
x Number The x value of the vertice
y Number The y value of the vertice
approximated Vector2 Approximated value of the Vector2 (used to remove duplicates)
normalized Vector2 Normalized Vector2
norm Vector2 Alias of normalized
magnitude Number Magnitude of the Vector2
clone Vector2 A clone of the Vector2

new Vector2([p1], [p2])

Param Type Description
[p1] Vector2 | Number Vector2: copy, Number: assign
[p2] Number Number: assign

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

Kind: instance property of 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

Kind: instance property of Vector2
See: norm
Example

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

vector2.norm

Kind: instance property of Vector2
See: normalized

vector2.magnitude

Kind: instance property of Vector2
Example

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

vector2.clone

Kind: instance property of 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) ⇒ Vector2

Flooring the Vector2

Kind: instance method of Vector2

Param Type Default Description
v Number 1 Precision

vector2.floorX([v]) ⇒ Vector2

Flooring the x value of the Vector2

Kind: instance method of Vector2

Param Type Default Description
[v] Number 1 Precision

vector2.floorY([v]) ⇒ Vector2

Flooring the y value of the Vector2

Kind: instance method of Vector2

Param Type Default Description
[v] Number 1 Precision

vector2.ceil([v]) ⇒ Vector2

Ceiling the Vector2

Kind: instance method of Vector2

Param Type Default Description
[v] Number 1 Precision

vector2.ceilX([v]) ⇒ Vector2

Ceiling the x value of the Vector2

Kind: instance method of Vector2

Param Type Default Description
[v] Number 1 Precision

vector2.ceilY([v]) ⇒ Vector2

Ceiling the y value of the Vector2

Kind: instance method of Vector2

Param Type Default Description
[v] Number 1 Precision

vector2.round([v]) ⇒ Vector2

Rounding the Vector2

Kind: instance method of Vector2

Param Type Default Description
[v] Number 1 Precision

vector2.roundX([v]) ⇒ Vector2

Rounding the x value of the Vector2

Kind: instance method of Vector2

Param Type Default Description
[v] Number 1 Precision

vector2.roundY([v]) ⇒ Vector2

Rounding the y value of the Vector2

Kind: instance method of Vector2

Param Type Default Description
[v] Number 1 Precision

vector2.set([p1], [p2]) ⇒ Vector2

Setting the value of the Vector2

Kind: instance method of Vector2

Param Type Description
[p1] Vector2 | Number Vector2: copy, Number: assign
[p2] Number Number: assign

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) ⇒ Vector2

Setting the x value of the Vector2

Kind: instance method of Vector2

Param Type Description
v Number The value

vector2.setY(v) ⇒ Vector2

Setting the y value of the Vector2

Kind: instance method of Vector2

Param Type Description
v Number The value

vector2.add([p1], [p2]) ⇒ Vector2

Adding values to the the Vector2

Kind: instance method of Vector2

Param Type Description
[p1] Vector2 | Number Vector2: copy, Number: assign
[p2] Number Number: assign

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) ⇒ Vector2

Adding v to x value of the Vector2

Kind: instance method of Vector2

Param Type Description
v Number The value

vector2.addY(v) ⇒ Vector2

Adding v to y value of the Vector2

Kind: instance method of Vector2

Param Type Description
v Number The value

vector2.substract([p1], [p2]) ⇒ Vector2

Substracting values to the the Vector2

Kind: instance method of Vector2

Param Type Description
[p1] Vector2 | Number Vector2: copy, Number: assign
[p2] Number Number: assign

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) ⇒ Vector2

Substracting v to x value of the Vector2

Kind: instance method of Vector2

Param Type Description
v Number The value

vector2.substractY(v) ⇒ Vector2

Substracting v to x value of the Vector2

Kind: instance method of Vector2

Param Type Description
v Number The value

vector2.multiply([p1], [p2]) ⇒ Vector2

Multiply values to the the Vector2

Kind: instance method of Vector2

Param Type Description
[p1] Vector2 | Number Vector2: copy, Number: assign
[p2] Number Number: assign

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  1, 2 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 v2 to v1 Vector2
v1.multiply(v2);
// v1 values are now x:8 & y:3, v2 is unchanged

vector2.multiplyX(v) ⇒ Vector2

Multiply x value of the Vector2 by v

Kind: instance method of Vector2

Param Type Description
v Number The multiplicator

vector2.multiplyY(v) ⇒ Vector2

Multiply y value of the Vector2 by v

Kind: instance method of Vector2

Param Type Description
v Number The multiplicator

vector2.divide(p1, p2) ⇒ Vector2

Dividing values to the the Vector2

Kind: instance method of Vector2

Param Type Description
p1 Vector2 | Number Vector2: copy, Number: assign
p2 Number Number: assign

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) ⇒ Vector2

Divinding x value of the Vector2 by v

Kind: instance method of Vector2

Param Type Description
v Number The divisor

vector2.divideY(v) ⇒ Vector2

Divinding x value of the Vector2 by v

Kind: instance method of Vector2

Param Type Description
v Number The divisor

vector2.invert() ⇒ Vector2

Inverting the Vector2

Kind: instance method of Vector2
Example

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

vector2.invertX() ⇒ Vector2

Inverting the x value of the Vector2

Kind: instance method of Vector2
Example

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

vector2.invertY() ⇒ Vector2

Inverting the y value of the Vector2

Kind: instance method of Vector2
Example

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

vector2.rotate(angle) ⇒ Vector2

Rotate the Vector2 arround zero by angle

Kind: instance method of Vector2

Param Type Description
angle Number degrees angle

Example

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

vector2.equals([p1], [p2]) ⇒ Boolean

Test if the Vector2 is equal to an other

Kind: instance method of Vector2

Param Type Description
[p1] Vector2 | Number Vector2: copy, Number: assign
[p2] Number Number: assign

Example

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

vector2.equalsX(v) ⇒ Boolean

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

Kind: instance method of Vector2

Param Type Description
v Number The value to compare

Example

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

vector2.equalsY(v) ⇒ Boolean

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

Kind: instance method of Vector2

Param Type Description
v Number The value to compare

Example

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

vector2.isInside(polygon) ⇒ Boolean

Test if the current Vector2 is inside a polygon

Kind: instance method of Vector2

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()

Kind: instance method of 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() ⇒ String

Getting JSON string version of the Vector2

Kind: instance method of Vector2

Vector2.Distance(v1, v2) ⇒ Number

Getting the distance between two Vector2

Kind: static method of Vector2

Param Type Description
v1 Vector2 first Vector2
v2 Vector2 second Vector2

Vector2.Max(v1, v2) ⇒ Vector2

Getting the max values from two Vector2

Kind: static method of Vector2

Param Type Description
v1 Vector2 first Vector2
v2 Vector2 second Vector2

Vector2.Min(v1, v2) ⇒ Vector2

Getting the min values from two Vector2

Kind: static method of Vector2

Param Type Description
v1 Vector2 first Vector2
v2 Vector2 second Vector2

Vector2.Lerp(v1, v2, t) ⇒ Vector2

Getting a Lerp Vector2 from v1 to v2 with t time

Kind: static method of Vector2

Param Type Description
v1 Vector2 first Vector2
v2 Vector2 second Vector2
t Number time of lerp, value should be between 0 and 1

Vector2.LerpUnclamped(v1, v2, t) ⇒ Vector2

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

Kind: static method of Vector2

Param Type Description
v1 Vector2 first Vector2
v2 Vector2 second Vector2
t Number time of lerp, value should be between 0 and 1, but it can exceed this values

Vector2.IsVector2Like(subject) ⇒ Vector2 | false

Test if the passed element is a Vector2 like and return Vector2 corresponding, or false

Kind: static method of Vector2
Returns: Vector2 | false - false on failure

Param Type Description
subject any The subject to test

Vector2.Zero() ⇒ Vector2

A zero Vector2

Kind: static method of Vector2

Vector2.Top() ⇒ Vector2

A top Vector2

Kind: static method of Vector2

Vector2.Left() ⇒ Vector2

A left Vector2

Kind: static method of Vector2

Vector2.Bottom() ⇒ Vector2

A bottom Vector2

Kind: static method of Vector2

Vector2.Right() ⇒ Vector2

A right Vector2

Kind: static method of Vector2