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
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
Members
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(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
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) → {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:
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:
Parameters:
Name | Type | Description |
---|---|---|
pt1 |
Point
|
|
pt2 |
Point
|
Returns:
- Type:
-
Array.<Polygon>
cutWithLine(line) → {Polygon}
Return a result of cutting polygon with line
- Source:
Parameters:
Name | Type | Description |
---|---|---|
line |
Line
|
cutting line |
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
findEdgeByPoint(pt) → {Edge}
Returns the first founded edge of polygon that contains given point
- 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
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
|
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) |
splitToIslands() → {Array.<Flatten.Polygon>}
Split polygon into array of polygons, where each polygon is an island with all
hole that it contains
- 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 |