Gleam - v1.0.2
    Preparing search index...

    Class Polygon

    Convex 2D polygon. The collision API (Polygon.collide) uses SAT, which is mathematically defined only for convex polygons — feeding it a concave polygon silently produces wrong results.

    Index

    Constructors

    Accessors

    • get center(): Readonly<Vec2>

      Centroid (mean of vertex positions). Recomputed whenever the vertex set changes.

      Returns Readonly<Vec2>

    • get points(): readonly Vec2[]

      Read-only view of the current vertex list. Use addPoint/offset/rotate to mutate.

      Returns readonly Vec2[]

    Methods

    • Append a single vertex at (x, y). Mutates and returns this.

      Parameters

      • x: number
      • y: number

      Returns Polygon

    • SAT collision against otherPolygon. Both polygons must be convex — SAT silently misses collisions for concave shapes. Pass a non-zero velocity to also compute whether the polygons would intersect after that displacement. Warns and returns a no-collision result if either polygon has zero edges.

      Parameters

      Returns PolygonCollisionResult

    • Stroke the polygon to context, shifted by offset. Coordinates are truncated to integers (via | 0) for crisp lines.

      Parameters

      • context: CanvasRenderingContext2D
      • offset: Vec2 = ...

      Returns void

    • Translate every vertex by (x, y). Mutates and returns this.

      Parameters

      • x: number = 0
      • y: number = 0

      Returns Polygon

    • Rotate by angle radians around pos (defaults to the centroid). Mutates and returns this.

      Parameters

      • angle: number
      • pos: Readonly<Vec2> = ...

      Returns Polygon

    • Trace an outline around the opaque pixels of canvas via four directional sweeps (top, right, bottom, left). detail is the pixel stride (≥ 2) between scanline samples — higher = faster but coarser. angle is the simplification threshold in radians: vertices whose turn angle wraps to within ±angle of straight are dropped. Throws if fewer than 3 vertices survive.

      Convex shapes only. The sweep ignores anything an outer-perimeter ray can't reach: holes (donuts), inward bays (a "C" opening sideways), or any row/column with multiple disjoint opaque spans. For those inputs the result is either a broken polygon or simply the outer hull, and Polygon.collide relies on convexity anyway.

      Parameters

      • canvas: HTMLCanvasElement
      • detail: number
      • angle: number

      Returns Polygon

    • Regular convex polygon with edges vertices, inscribed in a bounding box of size (number = square).

      Parameters

      • edges: number
      • size: number | Vec2

      Returns Polygon