# 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(…args)

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:
  - classes/polygon.js, line 32

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`args` | `args` | <repeatable> | array of shapes or array of arrays

### Classes

Polygon

### Members

#### faces :PlanarSet

Container of faces (closed loops), may be empty

Source:
  - classes/polygon.js, line 37

##### Type:

- `PlanarSet`

#### edges :PlanarSet

Container of edges

Source:
  - classes/polygon.js, line 42

##### Type:

- `PlanarSet`

#### box

(Getter) Returns bounding box of the polygon

Source:
  - classes/polygon.js, line 70

#### vertices

(Getter) Returns array of vertices

Source:
  - classes/polygon.js, line 78

### Methods

#### clone() → {Polygon}

Create new cloned instance of the polygon

Source:
  - classes/polygon.js, line 86

##### Returns:

Type:
  `Polygon`

#### isEmpty() → {boolean}

Return true is polygon has no edges

Source:
  - classes/polygon.js, line 98

##### 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:
  - classes/polygon.js, line 110

##### Returns:

Type:
  `boolean`

#### area() → {number}

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

Source:
  - classes/polygon.js, line 128

##### Returns:

Type:
  `number`

#### addFace(…args) → {Face}

Add new face to polygon. Returns added face

Source:
  - classes/polygon.js, line 143

##### 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`

#### deleteFace(face) → {boolean}

Delete existing face from polygon

Source:
  - classes/polygon.js, line 154

##### Parameters:

Name | Type | Description
--- | --- | ---
`face` | `Face` | Face to be deleted

##### Returns:

Type:
  `boolean`

#### removeChain(face, edgeFrom, edgeTo)

Delete chain of edges from the face.

Source:
  - classes/polygon.js, line 168

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

#### 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. If no edge added, it returns edge before vertex

Source:
  - classes/polygon.js, line 194

##### 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`

#### cut(multiline) → {Array.<Polygon>}

Cut polygon with line and return array of new polygons

Source:
  - classes/polygon.js, line 230

##### Parameters:

Name | Type | Description
--- | --- | ---
`multiline` | `Multiline` |

##### Returns:

Type:
  `Array.<Polygon>`

#### cutFace(pt1, pt2) → {Array.<Polygon>}

Cut face of polygon with a segment between two points and create two new polygons Supposed that a segments between points does not intersect any other edge

Source:
  - classes/polygon.js, line 261

##### Parameters:

Name | Type | Description
--- | --- | ---
`pt1` | `Point` |
`pt2` | `Point` |

##### Returns:

Type:
  `Array.<Polygon>`

#### findEdgeByPoint(pt) → {Edge}

Returns the first founded edge of polygon that contains given point

Source:
  - classes/polygon.js, line 311

##### Parameters:

Name | Type | Description
--- | --- | ---
`pt` | `Point` |

##### Returns:

Type:
  `Edge`

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

Split polygon into array of polygons, where each polygon is an island with all hole that it contains

Source:
  - classes/polygon.js, line 326

##### Returns:

Type:
  `Array.<Flatten.Polygon>`

#### reverse() → {Polygon}

Reverse orientation of all faces to opposite

Source:
  - classes/polygon.js, line 354

##### Returns:

Type:
  `Polygon`

#### contains(shape) → {boolean}

Returns true if polygon contains shape: no point of shape lies outside of the polygon, false otherwise

Source:
  - classes/polygon.js, line 367

##### Parameters:

Name | Type | Description
--- | --- | ---
`shape` | `Shape` | test shape

##### Returns:

Type:
  `boolean`

#### distanceTo(shape) → {Number|Segment}

Return distance and shortest segment between polygon and other shape as array [distance, shortest_segment]

Source:
  - classes/polygon.js, line 382

##### 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:
  - classes/polygon.js, line 422

##### Parameters:

Name | Type | Description
--- | --- | ---
`shape` |  | Shape of the one of supported types

##### Returns:

Type:
  `Array.<Point>`

#### translate(vec) → {Polygon}

Returns new polygon translated by vector vec

Source:
  - classes/polygon.js, line 453

##### Parameters:

Name | Type | Description
--- | --- | ---
`vec` | `Vector` |

##### 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 counter clockwise, negative - clockwise

Source:
  - classes/polygon.js, line 469

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

#### transform(matrix) → {Polygon}

Return new polygon transformed using affine transformation matrix

Source:
  - classes/polygon.js, line 482

##### Parameters:

Name | Type | Description
--- | --- | ---
`matrix` | `Matrix` | affine transformation matrix

##### Returns:

Type:
  `Polygon`

- new polygon

#### toJSON() → {Object}

This method returns an object that defines how data will be serialized when called JSON.stringify() method

Source:
  - classes/polygon.js, line 495

##### Returns:

Type:
  `Object`

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

Transform all faces into array of polygons

Source:
  - classes/polygon.js, line 503

##### Returns:

Type:
  `Array.<Flatten.Polygon>`

#### svg(attrs) → {string}

Return string to draw polygon in svg

Source:
  - classes/polygon.js, line 514

##### 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`