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

##### Parameters:

Type | Description
--- | ---
`args` | array of shapes or array of arrays

### Classes

Polygon

### Members

#### box

(Getter) Returns bounding box of the polygon

Source:
  - classes/polygon.js, line 81

#### edges :PlanarSet

Container of edges

Source:
  - classes/polygon.js, line 42

##### Type:

- `PlanarSet`

#### faces :PlanarSet

Container of faces (closed loops), may be empty

Source:
  - classes/polygon.js, line 37

##### Type:

- `PlanarSet`

#### vertices

(Getter) Returns array of vertices

Source:
  - classes/polygon.js, line 89

### Methods

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

Add new face to polygon. Returns added face

Source:
  - classes/polygon.js, line 154

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

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

##### Returns:

Type:
  `number`

#### clone() → {Polygon}

Create new cloned instance of the polygon

Source:
  - classes/polygon.js, line 97

##### Returns:

Type:
  `Polygon`

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

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

Source:
  - classes/polygon.js, line 380

##### Parameters:

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

##### Returns:

Type:
  `boolean`

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

Cut polygon with multiline and return array of new polygons Multiline should be constructed from a line with intersection point, see notebook: https://next.observablehq.com/@alexbol99/cut-polygon-with-line

Source:
  - classes/polygon.js, line 243

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

##### Parameters:

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

##### Returns:

Type:
  `Array.<Polygon>`

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

Delete existing face from polygon

Source:
  - classes/polygon.js, line 165

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

##### Parameters:

Name | Type | Description
--- | --- | ---
`shape` | `Shape` | Shape of one of the types Point, Circle, Line, Segment, Arc or Polygon

##### Returns:

Type:
  `Number` | `Segment`

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

Returns the first founded edge of polygon that contains given point

Source:
  - classes/polygon.js, line 324

##### Parameters:

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

##### Returns:

Type:
  `Edge`

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

Return array of intersection points between polygon and other shape

Source:
  - classes/polygon.js, line 435

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

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

##### Returns:

Type:
  `boolean`

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

Delete chain of edges from the face.

Source:
  - classes/polygon.js, line 179

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

#### reverse() → {Polygon}

Reverse orientation of all faces to opposite

Source:
  - classes/polygon.js, line 367

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

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

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

##### Returns:

Type:
  `Array.<Flatten.Polygon>`

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

Return string to draw polygon in svg

Source:
  - classes/polygon.js, line 527

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

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

Transform all faces into array of polygons

Source:
  - classes/polygon.js, line 516

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

##### Returns:

Type:
  `Object`

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

Return new polygon transformed using affine transformation matrix

Source:
  - classes/polygon.js, line 495

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

##### Parameters:

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

##### Returns:

Type:
  `Polygon`