PolyK

This library was written by Ivan Kuckir.## What is PolyK?

PolyK is JavaScript tool for working with polygons. It uses several basic principles to be super simple and super universal:**No classes.**PolyK does not have any classes or special data structures. PolyK consists of static functions only.**What is polygon?**For PolyK, polygon is an array of numbers. Each two of them are X and Y coordinate of polygon vertex.**Simplicity and speed.**Since PolyK does not make you use any special data structures, it is very easy to implement into your project. Since all JS engines are optimised for work with arrays, PolyK runs very fast.

PolyK was used in the game Tiny Monsters. Do you like the project? Make a donation!

## Install

**npm**

`$ npm install --save deep-slice`

**bower**

`$ bower install polyk`

**web browser ES5**

**Parameters**

`<script src="https://unpkg.com/polyk/dist/polyk.min.js"></script>`

## Resources

```
- [Point Intersection](http://polyk.ivank.net/?p=demos&d=intersect)
- [Triangulation](http://polyk.ivank.net/?p=demos&d=triangulate)
- [Slicing](http://polyk.ivank.net/?p=demos&d=slice)
- [Ray Casting](http://polyk.ivank.net/?p=demos&d=raycast)
- [Closest Edge](http://polyk.ivank.net/?p=demos&d=closestedge)
```

## API

### IsSimple

Checks, if polygon is simple. Polygon is simple, when its edges don't cross each other.**boolean**

Returns

`polygon`

**Array**<number>**x1, y1, x2, y2...**

Returns

**true if Polygon is simple**

Parameters

### IsConvex

Checks, if polygon is convex. Polygon is convex, when each inner angle is <= 180°.**boolean**

Returns

`polygon`

**Array**<number>**x1, y1, x2, y2...**

Returns

**Parameters**

### GetArea

Returns the area of polygon.**number**

Returns

`polygon`

**Array**<number>**x1, y1, x2, y2...**

Returns

**Parameters**

### GetAABB

Returns the Axis-aligned Bounding Box of polygon**Examples**

`polygon`

**Array**<number>**x1, y1, x2, y2...**

**AABB**

Returns

`//={x:0, y:0, width:0, height:0}`

Returns

**Parameters**

Works with simple polygons only.

### Triangulate

Computes the triangulation. Output array is array of triangles (triangle = 3 indices of polygon vertices).Works with simple polygons only.

**Examples**

`polygon`

**Array**<number>**x1, y1, x2, y2...**

**Array<number>**

Returns

```
var ids = PolyK.Triangulate([0, 0, 1, 0, 1, 1, 0, 1]);
//=[0, 1, 2, 0, 2, 3]
```

Returns

**array of triangles (triangle = 3 indices of polygon vertices)**and bx,by. A, B must not lay inside a polygon. Returns an array of polygons.

### Slice

Slices the polygon with line segment A-B, defined by ax,ayWorks with simple polygons only.

**Parameters**

`polygon`

**Array**<number>**x1, y1, x2, y2...**`startX`

**number****Start Coordinate x**`startY`

**number****Start Coordinate y**`endX`

**number****End Coordinate x**`endY`

**number****End Coordinate y**

### ContainsPoint

Checks, if polygon contains x, y.Works with simple polygons only.

**Parameters**

Returns

**boolean**

**depth**and dx,dy.

### Raycast

Finds the closest point of polygon, which lays on ray defined by x,y"dist" is the distance of the polygon point, "edge" is the number of the edge, on which intersection occurs, "norm" is the normal in that place, "refl" is reflected direction.

Works with simple polygons only.

**Parameters**

`polygon`

**Array**<number>**x1, y1, x2, y2...**`originX`

**number****Origin x**`originY`

**number****Origin y**`directionX`

**number****Direction x**`directionY`

**number****Direction y**`isc`

**Examples**

`//={dist:0, edge:0, norm:{x:0, y:0}, refl:{x:0, y:0}}`

Returns

**Raycast**

**. Returns an object in this format**

### ClosestEdge

Finds the point on polygon edges, which is closest to x,y"dist" is the distance of the polygon point, "edge" is the number of the closest edge, "point" is the closest point on that edge, "norm" is the normal from "point" to x,y.

**Parameters**

**Examples**

`//={dist:0, edge:0, point:{x:0, y:0}, norm:{x:0, y:0}}`

Returns

**ClosestEdge**

**Parameters**

### Reverse

Reverse

`polygon`

**Array**<number>**x1, y1, x2, y2...**