GithubHelp home page GithubHelp logo

baozdemir / polyk Goto Github PK

View Code? Open in Web Editor NEW

This project forked from martywallace/polyk

0.0 2.0 0.0 97 KB

A JavaScript tool for working with polygons.

Home Page: http://polyk.ivank.net/

License: MIT License

JavaScript 100.00%

polyk's Introduction

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 polyk

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.