Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Polygon

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

Hierarchy

Index

Constructors

constructor

  • Creates a polygon from an outer boundary and a list of holes.

    note

    Doesn't verify that this is a valid polygon. It is possible to create a polygon with self-intersection, zero-length segments, or holes in weird places. This may cause problems in some functions.

    note

    Will throw an error if the boundary or holes aren't closed.

    Parameters

    • boundary: Polyline

      The outer edge of the polygon (must be a closed polyline).

    • Optional holes: keyof Polyline[] | undefined

      An optional list of holes cut from the interior of the polygon (must be closed polylines).

    Returns Polygon

Accessors

area

  • get area(): number
  • Gets the area of the polygon. Holes are not included in the area.

    Returns number

boundary

boundingBox

holes

  • Gets the list of holes (if any) that define the subtracted regions of the polygon.

    Returns keyof Polyline[]

Create Methods

Static fromCoords

  • fromCoords(coordinates: pcPolygon): 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.

    note

    May throw an error if coordinates do not form a closed polyline.

    Parameters

    • coordinates: pcPolygon

      List of points in the format [[[b_x1,b_y1],[b_x2,b_y2],...], [[h_x1,h_y1],[h_x2,h_y2],...],...].

    Returns Polygon

Other Methods

asGeoJSON

  • asGeoJSON(): pcPolygon
  • Gets the polygon in the GeoJSON format: [[[b_x1,b_y1],[b_x2,b_y2],...], [[h_x1,h_y1],[h_x2,h_y2],...],...].

    Returns pcPolygon

closestLoop

  • Finds the closest boundary or hole. Returns the polyline defining the closest boundary or hole.

    Parameters

    • testPoint: Point

      The target to get closest to.

    Returns Polyline

closestPoint

  • Finds the closest point on the polygon and returns the point. This point could be on the edge of the boundary or one of the holes, or it could be a point within the interior of the polygon.

    Parameters

    • testPoint: Point

      The target to get closest to.

    Returns Point

contains

  • Checks whether a point is inside, outside, or on the edge of a polygon.

    Parameters

    • point: Point

      The point to test for containment.

    • Default value tolerance: number = shapetypesSettings.absoluteTolerance

      The distance the point can be from the edge of the polygon and still considered coincident.

    Returns PointContainment

difference

  • Subtracts a polygon or closed polyline from this polygon. Returns the part left over.

    Parameters

    Returns ReadonlyArray<Polygon>

    The part left over.

equals

  • equals(otherPolygon: Polygon, tolerance?: number): boolean
  • Checks whether another polygon has the same boundary and holes. Returns true if it does.

    Parameters

    • otherPolygon: Polygon

      Polygon to compare against.

    • Default value tolerance: number = shapetypesSettings.absoluteTolerance

      The amount the points can differ and still be considered equal.

    Returns boolean

intersection

  • Intersects this polygon with another polygon or closed polyline. Returns the overlapping portion.

    Parameters

    Returns ReadonlyArray<Polygon>

    The overlapping portion of the two objects. In some cases, this may return an empty list if the polygons don't overlap.

toString

  • toString(): string
  • Gets the polygon as a string in the format: [boundary, ...holes].

    Returns string

union

  • Joins this polygon with another polygon or closed polyline. Returns the result.

    Parameters

    Returns ReadonlyArray<Polygon>

    The polygon created when the two objects were joined. In some cases, this may return multiple polygons if the objects don't overlap.

Transform Methods

changeBasis

  • changeBasis(planeFrom: Plane, planeTo: Plane): this
  • 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.

    see

    Transform.changeBasis.

    Parameters

    • planeFrom: Plane

      The coordinate system the geometry is currently described relative to.

    • planeTo: Plane

      The coordinate system to describe the geometry relative to.

    Returns this

    The geometry in the new coordinate system.

planeToPlane

  • planeToPlane(planeFrom: Plane, planeTo: Plane): this

rotate

  • rotate(angle: number): this
  • rotate(angle: number, pivot: Point): this
  • Rotates the geometry about (0,0).

    see

    Transform.rotate.

    Parameters

    • angle: number

      Angle to rotate the geometry (in radians). The direction is counter-clockwise.

    Returns this

  • Rotates the geometry about a point.

    see

    Transform.rotate.

    category

    Transform

    Parameters

    • angle: number

      Angle to rotate the geometry (in radians). If the environment's y-axis points upwards, the direction is counter-clockwise.

    • pivot: Point

      Point to pivot the geometry about.

    Returns this

scale

  • scale(amount: number): this
  • scale(x: number, y: number): this
  • scale(x: number, y: number, center: Point): this
  • 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.

    see

    Transform.scale.

    Parameters

    • amount: number

      Magnitude to scale in x- and y-direction. If less than 1, the object will shrink. If greater than 1, it will grow.

    Returns this

  • 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.

    see

    Transform.scale.

    category

    Transform

    Parameters

    • x: number

      Magnitude to scale in x-direction. If less than 1, the object will shrink. If greater than 1, it will grow.

    • y: number

      Magnitude to scale in y-direction. If less than 1, the object will shrink. If greater than 1, it will grow.

    Returns this

  • Scales the geometry about a point and returns the resized geometry.

    see

    Transform.scale.

    category

    Transform

    Parameters

    • x: number

      Magnitude to scale in x-direction. If less than 1, the object will shrink. If greater than 1, it will grow.

    • y: number

      Magnitude to scale in y-direction. If less than 1, the object will shrink. If greater than 1, it will grow.

    • center: Point

      Center of scaling. Everything will shrink or expand away from this point.

    Returns this

transform

  • Transforms the polyline by a transform matrix and returns the result.

    Example

    
    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)
    note

    If you're applying the same transformation a lot of geometry, creating the Transform matrix once and calling this function is faster than using the direct methods.

    Parameters

    Returns this

translate

  • translate(move: Vector): this
  • translate(move: Vector, distance: number | undefined): this
  • Moves the geometry along a vector and returns the moved geometry. The translation is always linear.

    see

    Transform.translate.

    Parameters

    • move: Vector

      The direction and distance to move the geometry.

    Returns this

  • Moves the geometry along a vector and returns the moved geometry. The translation is always linear.

    see

    Transform.translate.

    category

    Transform

    Parameters

    • move: Vector

      The direction to move the geometry.

    • distance: number | undefined

      The distance to move the geometry.

    Returns this

Generated using TypeDoc