Creates a polygon from an outer boundary and a list of holes.
The outer edge of the polygon (must be a closed polyline).
An optional list of holes cut from the interior of the polygon (must be closed polylines).
Gets the area of the polygon. Holes are not included in the area.
Gets the polyline that defines the outer edge of the polygon.
Gets the smallest bounding box that contains the boundary of the polygon.
Gets the list of holes (if any) that define the subtracted regions of the polygon.
Creates a polygon from a list polylines (in the GeoJSON format). The first polyline is the boundary and any subsequent polylines are the holes.
List of points in the format [[[b_x1,b_y1],[b_x2,b_y2],...], [[h_x1,h_y1],[h_x2,h_y2],...],...]
.
Gets the polygon in the GeoJSON format: [[[b_x1,b_y1],[b_x2,b_y2],...], [[h_x1,h_y1],[h_x2,h_y2],...],...]
.
Checks whether a point is inside, outside, or on the edge of a polygon.
The point to test for containment.
The distance the point can be from the edge of the polygon and still considered coincident.
Polygon to compare against.
The amount the points can differ and still be considered equal.
Intersects this polygon with another polygon or closed polyline. Returns the overlapping portion.
Either a closed polyline, polygon, or a list of the two.
The overlapping portion of the two objects. In some cases, this may return an empty list if the polygons don't overlap.
Gets the polygon as a string in the format: [boundary, ...holes]
.
Joins this polygon with another polygon or closed polyline. Returns the result.
Either a closed polyline, polygon, or a list of the two.
The polygon created when the two objects were joined. In some cases, this may return multiple polygons if the objects don't overlap.
Translates the geometry from one coordinate system to another while keeping
the geometry in the same position. In other words, if the geometry is currently
described relative to planeFrom
, after changeBasis,
it will be in the same position but described relative to planeTo
.
The coordinate system the geometry is currently described relative to.
The coordinate system to describe the geometry relative to.
The geometry in the new coordinate system.
Rotates the geometry about (0,0).
Angle to rotate the geometry (in radians). The direction is counter-clockwise.
Rotates the geometry about a point.
Angle to rotate the geometry (in radians). If the environment's y-axis points upwards, the direction is counter-clockwise.
Point to pivot the geometry about.
Scales the geometry and returns the resized geometry. The geometry will be scaled about (0,0), meaning everything will shrink or expand away from this point.
Magnitude to scale in x- and y-direction. If less than 1, the object will shrink. If greater than 1, it will grow.
Scales the geometry and returns the resized geometry. The geometry will be scaled about (0,0), meaning everything will shrink or expand away from this point.
Magnitude to scale in x-direction. If less than 1, the object will shrink. If greater than 1, it will grow.
Magnitude to scale in y-direction. If less than 1, the object will shrink. If greater than 1, it will grow.
Scales the geometry about a point and returns the resized geometry.
Magnitude to scale in x-direction. If less than 1, the object will shrink. If greater than 1, it will grow.
Magnitude to scale in y-direction. If less than 1, the object will shrink. If greater than 1, it will grow.
Center of scaling. Everything will shrink or expand away from this point.
Transforms the polyline by a transform matrix and returns the result.
import { Point, Polygon, Polyline, Transform, Vector } from 'shapetypes';
// Create a new polygon from a triangular polyline
const triangle = new Polyline([new Point(0, 0), new Point(1, 1), new Point(2, 0)], true);
const polygon = new Polygon(triangle);
console.log(polygon.boundary.from.toString());
// => (0,0)
// Translate polygon using a transform matrix
const matrix = Transform.translate(new Vector(3,4));
const shifted = polygon.transform(matrix);
console.log(shifted.boundary.from.toString());
// => (3,4)
// Translate polygon using the direct method
const otherShifted = polygon.translate(new Vector(3, 4));
console.log(otherShifted.boundary.from.toString());
// => (3,4)
Moves the geometry along a vector and returns the moved geometry. The translation is always linear.
The direction and distance to move the geometry.
Moves the geometry along a vector and returns the moved geometry. The translation is always linear.
The direction to move the geometry.
The distance to move the geometry.
Generated using TypeDoc
A two-dimensional shape with an outer boundary and a set of interior holes (optional).
The boundary is always in a counter-clockwise orientation, and the holes are always clockwise. If the environment's y-axis points downwards, the boundary will appear to be clockwise, and the holes will appear to be counter-clockwise.
Example
import { Point, Polygon, Polyline, Rectangle } from 'shapetypes' // Create a polygon from a triangular polyline const triangle = new Polyline([new Point(0, 0), new Point(1, 1), new Point(2, 0)], true); const polygon = new Polygon(triangle); // Get properties of the polygon console.log(polygon.area); // => 1 console.log(polygon.boundary.from.toString()); // => (0,0) // Check for point containment console.log(polygon.contains(new Point(1, 0.5))); // => True // Create a larger polygon from a rectangle const outer = new Rectangle(Plane.worldXY(), 10, 10).toPolyline(); const outerPolygon = new Polygon(outer); console.log(outerPolygon.area); // => 100 // Subtract the triangle from the rectangle const subtracted = outerPolygon.difference(triangle); // Check that the area was reduced console.log(subtracted[0].area); // => 99 // Check to see whether the point is still contained console.log(subtracted[0].contains(new Point(1, 0.5))); // => False