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
<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.Parameters
polygon
Array<number> x1, y1, x2, y2...
Returns boolean true if Polygon is simple
IsConvex
Checks, if polygon is convex. Polygon is convex, when each inner angle is <= 180°.Parameters
polygon
Array<number> x1, y1, x2, y2...
Returns boolean
GetArea
Returns the area of polygon.Parameters
polygon
Array<number> x1, y1, x2, y2...
Returns number
GetAABB
Returns the Axis-aligned Bounding Box of polygonParameters
polygon
Array<number> x1, y1, x2, y2...
Examples
//={x:0, y:0, width:0, height:0}
Returns AABB
Triangulate
Computes the triangulation. Output array is array of triangles (triangle = 3 indices of polygon vertices).Works with simple polygons only.
Parameters
polygon
Array<number> x1, y1, x2, y2...
Examples
var ids = PolyK.Triangulate([0, 0, 1, 0, 1, 1, 0, 1]);
//=[0, 1, 2, 0, 2, 3]
Returns Array<number> array of triangles (triangle = 3 indices of polygon vertices)
Slice
Slices the polygon with line segment A-B, defined by ax,ay and bx,by. A, B must not lay inside a polygon. Returns an array of polygons.Works with simple polygons only.
Parameters
polygon
Array<number> x1, y1, x2, y2...startX
number Start Coordinate xstartY
number Start Coordinate yendX
number End Coordinate xendY
number End Coordinate y
ContainsPoint
Checks, if polygon contains x, y.Works with simple polygons only.
Parameters
Returns boolean depth
Raycast
Finds the closest point of polygon, which lays on ray defined by x,y and dx,dy."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 xoriginY
number Origin ydirectionX
number Direction xdirectionY
number Direction yisc
Examples
//={dist:0, edge:0, norm:{x:0, y:0}, refl:{x:0, y:0}}
Returns Raycast
ClosestEdge
Finds the point on polygon edges, which is closest to x,y. Returns an object in this format"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
Reverse
ReverseParameters
polygon
Array<number> x1, y1, x2, y2...