Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Polygon

Polygon class provides static functions to create and operate on polygons. A polygon is usually represented as a Group of 3 or more Pts. You can use the static function as-is, or apply the op method in Group or Pt to many of these functions. See Op guide for details.

Hierarchy

  • Polygon

Index

Methods

Static Protected _axisOverlap

  • _axisOverlap(poly1: any, poly2: any, unitAxis: any): number
  • Check overlap dist from projected axis

    Parameters

    • poly1: any

      first polygon

    • poly2: any

      second polygon

    • unitAxis: any

      unit axis

    Returns number

Static adjacentSides

  • adjacentSides(pts: GroupLike, index: number, closePath?: boolean): Group[]
  • Given a Pt in the polygon group, the adjacent sides are the two sides which the Pt touches.

    Parameters

    • pts: GroupLike

      a group of Pts

    • index: number

      the target Pt

    • Default value closePath: boolean = false

      a boolean to specify whether the polygon should be closed (ie, whether the final segment should be counted).

    Returns Group[]

Static area

  • Find the area of a convex polygon.

    Parameters

    Returns any

Static bisector

  • Get a bisector which is a line that split between two sides of a polygon equally.

    Parameters

    • pts: GroupLike

      a group of Pts

    • index: number

      the Pt in the polygon to bisect from

    Returns Pt

    a bisector Pt that's a normalized unit vector

Static centroid

  • Get the centroid of a polygon, which is the average of all its points.

    Parameters

    • pts: GroupLike

      a Group of Pts representing a polygon

    Returns Pt

Static convexHull

  • Get a convex hull of the point set using Melkman's algorithm (Reference: http://geomalgorithms.com/a12-_hull-3.html)

    Parameters

    • pts: GroupLike

      a group of Pt

    • Default value sorted: boolean = false

      a boolean value to indicate if the group is pre-sorted by x position. Default is false.

    Returns Group

    a group of Pt that defines the convex hull polygon

Static fromCenter

  • fromCenter(center: PtLike, radius: number, sides: number): Group

Static hasIntersectCircle

  • Check if a convex polygon and a circle has intersections using Separating Axis Theorem.

    Parameters

    • poly: GroupLike

      a Group representing a convex polygon

    • circle: GroupLike

      a Group representing a circle

    Returns IntersectContext

    an IntersectContext object that stores the intersection info, or undefined if there's no intersection

Static hasIntersectPoint

  • Check if a Pt is inside a convex polygon

    Parameters

    • poly: GroupLike

      a Group of Pt defining a convex polygon

    • pt: PtLike

      the Pt to check

    Returns boolean

Static hasIntersectPolygon

  • Check if two convex polygons has intersections using Separating Axis Theorem.

    Parameters

    • poly1: GroupLike

      a Group representing a convex polygon

    • poly2: GroupLike

      a Group representing a convex polygon

    Returns IntersectContext

    an IntersectContext object that stores the intersection info, or undefined if there's no intersection

Static intersectPolygon2D

  • Find intersection points of 2 polygons by checking every side of both polygons

    Parameters

    • poly1: GroupLike

      a Group representing a polygon

    • poly2: GroupLike

      another Group representing a polygon

    Returns Group

Static lineAt

  • Given a Group of Pts that defines a polygon, get one edge using an index

    Parameters

    • pts: GroupLike

      a Group

    • idx: number

      index of a Pt in the Group

    Returns Group

Static lines

  • Get the line segments in this polygon

    Parameters

    • pts: GroupLike

      a Group of Pts

    • Default value closePath: boolean = true

      a boolean to specify whether the polygon should be closed (ie, whether the final segment should be counted).

    Returns Group[]

    an array of Groups which has 2 Pts in each group

Static midpoints

  • Get a new polygon group that is derived from midpoints in this polygon

    Parameters

    • pts: GroupLike

      a Group of Pts

    • Default value closePath: boolean = false

      a boolean to specify whether the polygon should be closed (ie, whether the final segment should be counted).

    • Default value t: number = 0.5

      a value between 0 to 1 for interpolation. Default to 0.5 which will get the middle point.

    Returns Group

Static nearestPt

  • Given a target Pt, find a Pt in a Group that's nearest to it.

    Parameters

    Returns number

    an index in the pts indicating the nearest Pt, or -1 if none found

Static network

  • Given a point in the polygon as an origin, get an array of lines that connect all the remaining points to the origin point.

    Parameters

    • pts: GroupLike

      a Group representing a polygon

    • Default value originIndex: number = 0

      the origin point's index in the polygon

    Returns Group[]

Static perimeter

  • perimeter(pts: GroupLike, closePath?: boolean): object
  • Find the perimeter of this polygon, ie, the lengths of its sides.

    Parameters

    • pts: GroupLike

      a group of Pts

    • Default value closePath: boolean = false

      a boolean to specify whether the polygon should be closed (ie, whether the final segment should be counted).

    Returns object

    an object with total length, and segments which is a Pt that stores each segment's length

    • segments: Pt
    • total: number

Static projectAxis

  • Project axis (eg, for use in Separation Axis Theorem)

    Parameters

    Returns Pt

Static rectangle

  • Create a rectangular polygon

    Parameters

    • center: PtLike

      center point of the rectangle

    • widthOrSize: number | PtLike

      width as number, or a Pt representing the size of the rectangle

    • Optional height: number

      optional height

    Returns Group

Static toRects

  • Get a bounding box for each polygon group, as well as a union bounding-box for all groups

    Parameters

    • polys: GroupLike[]

      an array of Groups, or an array of Pt arrays

    Returns GroupLike[]

Generated using TypeDoc