gpc.js
v1.0.1
Published
A port of General Polygon Clipper
Downloads
3
Readme
GPC.js
A Port of the General Polygon Clipper library by the University of Manchester (http://www.cs.man.ac.uk/~toby/gpc/)
This module exports a single data type, Polygon
, which can represent euclidean polygons of arbitrary complexity and perform binary set operations on them. Polygon
s are not directly constructable with new
; new Polygon
instances can be created with Polygon.fromPoints()
or Polygon.fromVertices()
, described below. Polygon
s are also immutable; all geometric operations return new Polygon
instances as their results.
API
readonly isEmpty: boolean
Indicates whether the polygon has non-zero area.readonly isHole: boolean
Indicates whether the polygon has negative area / is the boundary of a hole in a complex polygon.bounds: { minx: number, maxx: number, miny: number, maxy: number }
Returns the minimal coordinate-aligned rectangular bounding box for the polygon.equals(obj: Polygon): boolean
Determine whether two polygons have the same vertex set, including the categorization of vertices as belonging to positive boundaries or holes.toVertices(): { bounds: { x: number, y: number }[][], holes: { x: number, y: number }[][] }
Exports a description of the polygon as a set of vanilla JS objects.bounds
is a list of lists of vertices representing the boundaries of positive-area sub-polygons, while holes is a list of lists of vertices representing the boundaries of interior holes.static fromVertices({ bounds, holes }: { bounds: Vertex[][], holes: Vertex[][] }): Polygon
Creates a complex polygon from an object of the same shape as returned bytoVertices()
. AVertex
can be an onject of the form{ x: number, y: number }
or a two-element array of[x, y]
.static fromPoints(points: Vertex[]): Polygon
Creates a simple polygon from a list of vertices, where aVertex
can be an onject of the form{ x: number, y: number }
or a two-element array of[x, y]
.static intersection(...p: Polygon[]): Polygon
Computes the geometric set intersection of a list ofPolygon
s.intersection(...p: Polygon[]): Polygon
Computes the geometric set intersection ofthis
with a list of additionalPolygon
s.static union(...p: Polygon[]): Polygon
Computes the geometric set union of a list ofPolygon
s.union(...p: Polygon[]): Polygon
Computes the geometric set union ofthis
with a list of additionalPolygon
s.static xor(...p: Polygon[]): Polygon
Computes the geometric symmetric set difference of a list ofPolygon
s.xor(...p: Polygon[]): Polygon
Computes the geometric symmetric set difference ofthis
with a list of additionalPolygon
s.static difference(first: Polygon, ...p: Polygon[]): Polygon
Computes the geometric set difference of the firstPolygon
with a list of additionalPolygon
s.difference(...p: Polygon[]): Polygon
Computes the geometric set difference ofthis
with a list of additionalPolygon
s.