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. With no arguments new polygon is empty.
Constructor accepts as argument array that define loop of shapes or array of arrays in case of multi polygon
Loop may be defined in different ways:
- array of shapes of type Segment or Arc
- array of points (Flatten.Point)
- array of numeric pairs which represent points
- box or circle object
Alternatively, it is possible to use polygon.addFace method
Source:
Parameters:
Type Description
args array of shapes or array of arrays

Classes

Polygon

Members

box

(Getter) Returns bounding box of the polygon
Source:

edges :PlanarSet

Container of edges
Source:
Type:

faces :PlanarSet

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

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.<Point> | Array.<Segment> | Array.<Arc> | 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(pt, edge) → {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. If no edge added, it returns edge before vertex
Source:
Parameters:
Name Type Description
pt Point Point to be added as a new vertex
edge Edge Edge to be split with new vertex and then trimmed from start
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 cloned instance of the polygon
Source:
Returns:
Type:
Polygon

contains(shape) → {boolean}

Returns true if polygon contains shape: no point of shape lay outside of the polygon, false otherwise
Source:
Parameters:
Name Type Description
shape Shape test shape
Returns:
Type:
boolean

cut(multiline) → {Polygon}

Cut polygon with multiline and return a new polygon
Source:
Parameters:
Name Type Description
multiline Multiline
Returns:
Type:
Polygon

cutWithLine(line) → {Polygon}

A special case of cut() function The return is a polygon cut with line
Source:
Parameters:
Name Type Description
line Line cutting line
Returns:
Type:
Polygon
newPoly - resulted polygon

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

dpath() → {*}

Return string to be assigned to 'd' attribute of element
Source:
Returns:
Type:
*

findEdgeByPoint(pt) → {Edge}

Returns the first found edge of polygon that contains given point If point is a vertex, return the edge where the point is an end vertex, not a start one
Source:
Parameters:
Name Type Description
pt Point
Returns:
Type:
Edge

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

recreateFaces()

Clear all faces and create new faces from edges
Source:

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

removeEndVertex(edge)

Merge given edge with next edge and remove vertex between them
Source:
Parameters:
Name Type Description
edge Edge

reverse() → {Polygon}

Reverse orientation of all faces to opposite
Source:
Returns:
Type:
Polygon

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 counterclockwise, 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

scale(sx, sy) → {Polygon}

Return new polygon with coordinates multiplied by scaling factor
Source:
Parameters:
Name Type Description
sx number x-axis scaling factor
sy number y-axis scaling factor
Returns:
Type:
Polygon

splitToIslands() → {Array.<Flatten.Polygon>}

Split polygon into array of polygons, where each polygon is an outer face with all containing inner faces
Source:
Returns:
Type:
Array.<Flatten.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
Returns:
Type:
string

toArray() → {Array.<Flatten.Polygon>}

Transform all faces into array of polygons
Source:
Returns:
Type:
Array.<Flatten.Polygon>

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