Noodles, Poodles and More!

    polyk
    TypeScript icon, indicating that this package has built-in type declarations

    0.24.0 • Public • Published

    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

    API

    IsSimple

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

    Parameters

    Returns boolean true if Polygon is simple

    IsConvex

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

    Parameters

    Returns boolean

    GetArea

    Returns the area of polygon.

    Parameters

    Returns number

    GetAABB

    Returns the Axis-aligned Bounding Box of polygon

    Parameters

    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

    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 [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

    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

    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

    Reverse

    Parameters

    Install

    npm i polyk

    DownloadsWeekly Downloads

    1,904

    Version

    0.24.0

    License

    MIT

    Last publish

    Collaborators

    • deniscarriere
    • febeling