1.0.2 • Published 10 years ago
polygon v1.0.2
polygon.js
Install
nodejs
npm install polygon
Usage
Create a new polygon:
var p = new Polygon([
Vec2(0, 0),
Vec2(10, 0),
Vec2(0, 10)
]);You can pass an array of Vec2s, arrays [x, y], or objects { x: 10, y: 20 }
Supported Methods
each(function(prev, current, next, idx) {})point(idx)- returns the point at indexidx. note: this will wrap in both directionsdedupe(returnNew)- ensure all of the points are uniqueinsert(vec2, index)- insertvec2at the specified indexremove(vecOrIndex)- remove the specifiedvec2or numeric index from this polygonclean(returnNew)- removes contiguous points that are the samewinding()- returns the direction in which a polygon is wound (true === clockwise)rewind(bool)- rewinds the polygon in the specified direction (true === clockwise)area()- computes the area of the polygonclosestPointTo(vec2)- finds the closest point in this polygon tovec2center()- returns aVec2at the center of the AABBscale(amount, origin, returnNew)- scales this polygon aroundorigin(default isthis.center()) and will return a new polygon if requested withreturnNewcontainsPoint(vec2)- returns true ifvec2is inside the polygoncontainsPolygon(poly)- returns true ifpolyis completely contained in this polygonaabb()- returns an object{x:_, y:_, w:_, h:_}representing the axis-aligned bounding box of this polygyonoffset(amount)- performs an offset/buffering operation on this polygon and returns a new oneline(index)- return an array[startpoint, endpoint]representing the line at the specifiedindexlines(function(start, end, index) {})- iterate over the lines in this polygonselfIntersections- find self-intersections and return them as a new polygonpruneSelfIntersections- remove self intersections from this polygon. returns an array of polygonslength- returns the number of points in this polygonclone- return a new instance of this polygonrotate(rads, vec2, returnNew)- rotate by originvec2(defaultthis.center()) by radiansradsand return a clone ifreturnNewis specifiedtranslate(vec2, returnNew)- translate byvec2and return a clone ifreturnNewis specifiedequal(poly)- return true if this polygon has the same components and the incomingpolycontains(thing)- works with an array of vec2's, an object containing a.positionand.radius, an object populated with x1,y1,x2,y2, an object populated with x,y,w,h, and an object populated with x,y,width,height. See the tests for more infounion(polygon)returns a new polygon representing the boolean union ofthisand the incomingpolygoncut(polygon)returns a new polygon representing the boolean cut ofpolygonfromthistoArray()convert this polygon into an array of arrays ([[x, y]])
license
MIT