Polygon

Polygon

Class representing a polygon.
Polygon in FlattenJS is a multipolygon comprised from a set of faces.
Face, in turn, is a closed loop of edges, where edge may be segment or circular arc

Constructor

new Polygon()

Constructor creates new instance of polygon.
New polygon is empty. Add face to the polygon using method
polygon.addFace(Points[]|Segments[]|Arcs[])
Source:

Members

box

(Getter) Returns bounding box of the polygon
Source:

edges :PlanarSet

Container of edges
Source:
Type:
  • PlanarSet

faces :PlanarSet

Container of faces (closed loops), may be empty
Source:
Type:
  • PlanarSet

vertices

(Getter) Returns array of vertices
Source:

Methods

addFace(…args) → {Face}

Add new face to polygon. Returns added face
Source:
Parameters:
Name Type Attributes Description
args Array.<Points> | Array.<Segments> | Array.<Arcs> | Circle | Box <repeatable>
new face may be create with one of the following ways:
1) array of points that describe closed path (edges are segments)
2) array of shapes (segments and arcs) which describe closed path
3) circle - will be added as counterclockwise arc
4) box - will be added as counterclockwise rectangle
You can chain method face.reverse() is you need to change direction of the creates face
Returns:
Type:
Face

addVertex(edge, pt) → {Edge}

Add point as a new vertex and split edge. Point supposed to belong to an edge. When edge is split, new edge created from the start of the edge to the new vertex and inserted before current edge. Current edge is trimmed and updated. Method returns new edge added.
Source:
Parameters:
Name Type Description
edge Edge Edge to be split with new vertex and then trimmed from start
pt Point Point to be added as a new vertex
Returns:
Type:
Edge

area() → {number}

Returns area of the polygon. Area of an island will be added, area of a hole will be subtracted
Source:
Returns:
Type:
number

clone() → {Polygon}

Create new copied instance of the polygon
Source:
Returns:
Type:
Polygon

contains(point) → {boolean}

Returns true if polygon contains point, including polygon boundary, false otherwise Point in polygon test based on ray shooting algorithm
Source:
Parameters:
Name Type Description
point Point test point
Returns:
Type:
boolean

deleteFace(face) → {boolean}

Delete existing face from polygon
Source:
Parameters:
Name Type Description
face Face Face to be deleted
Returns:
Type:
boolean

distanceTo(shape) → {Number|Segment}

Return distance and shortest segment between polygon and other shape as array [distance, shortest_segment]
Source:
Parameters:
Name Type Description
shape Shape Shape of one of the types Point, Circle, Line, Segment, Arc or Polygon
Returns:
Type:
Number | Segment

intersect(shape) → {Array.<Point>}

Return array of intersection points between polygon and other shape
Source:
Parameters:
Name Type Description
shape Shape of the one of supported types
Returns:
Type:
Array.<Point>

isEmpty() → {boolean}

Return true is polygon has no edges
Source:
Returns:
Type:
boolean

isValid() → {boolean}

Return true if polygon is valid for boolean operations Polygon is valid if
1. All faces are simple polygons (there are no self-intersected polygons)
2. All faces are orientable and there is no island inside island or hole inside hole - TODO
3. There is no intersections between faces (excluding touching) - TODO
Source:
Returns:
Type:
boolean

removeChain(face, edgeFrom, edgeTo)

Delete chain of edges from the face.
Source:
Parameters:
Name Type Description
face Face Face to remove chain
edgeFrom Edge Start of the chain of edges to be removed
edgeTo Edge End of the chain of edges to be removed

rotate(angle, center) → {Polygon}

Return new polygon rotated by given angle around given point If point omitted, rotate around origin (0,0) Positive value of angle defines rotation counter clockwise, negative - clockwise
Source:
Parameters:
Name Type Default Description
angle number 0 rotation angle in radians
center Point rotation center, default is (0,0)
Returns:
Type:
Polygon
- new rotated polygon

svg(attrs) → {string}

Return string to draw polygon in svg
Source:
Parameters:
Name Type Description
attrs an object with attributes for svg path element, like "stroke", "strokeWidth", "fill", "fillRule", "fillOpacity" Defaults are stroke:"black", strokeWidth:"1", fill:"lightcyan", fillRule:"evenodd", fillOpacity: "1"
Returns:
Type:
string

toJSON() → {Object}

This method returns an object that defines how data will be serialized when called JSON.stringify() method
Source:
Returns:
Type:
Object

transform(matrix) → {Polygon}

Return new polygon transformed using affine transformation matrix
Source:
Parameters:
Name Type Description
matrix Matrix affine transformation matrix
Returns:
Type:
Polygon
- new polygon

translate(vec) → {Polygon}

Returns new polygon translated by vector vec
Source:
Parameters:
Name Type Description
vec Vector
Returns:
Type:
Polygon