Home Reference Source Repository
import {Rect} from 'webgame-lib/src/geom.js'
public class | source

Rect

Rectangle in 2D space.

Constructor Summary

Public Constructor
public

Initializes a rectangle.

Member Summary

Public Members
public

Height of rectangle.

public

Width of rectangle.

public

Left of rectangle.

public

Top of rectangle.

Method Summary

Public Methods
public

Returns the area under the rectangle.

public

center(): Vec

Calculates the center of the rectangle.

public

clone(): Rect

Returns an identical instance.

public

Checks whether a point is contained in the rectangle.

public

dim(w: number, h: number): Rect

Sets the dimensions of the rectangle.

public

Checks that the dimensions of the rectangle are in the supplied range.

public

floor(): Rect

Floors all the properties of the rectangle.

public

Checks whether two rectangles intersect.

public

order(other: Rect, axis: string): Rect[]

Sorts the rectangles along the supplied axis.

public

overlap(other: Rect, axis: string): int[]

Calculates the overlap with other rectangle along one axis.

public

pos(x: number, y: number): Rect

Sets the position of the rectangle.

public

round(): Rect

Rounds all the properties of the rectangle.

public

Scales the entire rectangle.

public

Finds the axis that seperate the rectangles.

public

set(x: number, y: number, w: number, h: number): Rect

Sets all properties.

public

Centers the rectangle on given point.

public

shrink(val: number): Rect

Shrinks the rectangle about the center.

public

split(f: number, axis: string): Rect[]

Splits the rectangle into two along one axis.

public

Scales the dimensions about the center.

Public Constructors

public constructor(x: number, y: number, w: number, h: number) source

Initializes a rectangle.

Params:

NameTypeAttributeDescription
x number

Left

y number

Top

w number

Width

h number

Height

Public Members

public h: number source

Height of rectangle.

public w: number source

Width of rectangle.

public x: number source

Left of rectangle.

public y: number source

Top of rectangle.

Public Methods

public area(): number source

Returns the area under the rectangle.

Return:

number

Area

public center(): Vec source

Calculates the center of the rectangle.

Return:

Vec

Center

public clone(): Rect source

Returns an identical instance.

Return:

Rect

Copy of self

public contains(v: Vec): boolean source

Checks whether a point is contained in the rectangle.

Params:

NameTypeAttributeDescription
v Vec

Point

Return:

boolean

Whether contained

public dim(w: number, h: number): Rect source

Sets the dimensions of the rectangle.

Params:

NameTypeAttributeDescription
w number

Width

h number

Height

Return:

Rect

Self

public dimInRange(min: number, max: number): boolean source

Checks that the dimensions of the rectangle are in the supplied range.

Params:

NameTypeAttributeDescription
min number

Minimum

max number

Maximum

Return:

boolean

Whether dimensions are in range

public floor(): Rect source

Floors all the properties of the rectangle.

Return:

Rect

Self

public intersect(b: Rect): boolean source

Checks whether two rectangles intersect.

Params:

NameTypeAttributeDescription
b Rect

Rectangle to test against.

Return:

boolean

Whether intersects

public order(other: Rect, axis: string): Rect[] source

Sorts the rectangles along the supplied axis.

Params:

NameTypeAttributeDescription
other Rect

Other rectangle

axis string

Axis

Return:

Rect[]

Sorted rectangles

public overlap(other: Rect, axis: string): int[] source

Calculates the overlap with other rectangle along one axis.

Params:

NameTypeAttributeDescription
other Rect

Other rectangle

axis string

Axis

Return:

int[] (nullable: true)

Two element tuple of [start, end] both inclusive

public pos(x: number, y: number): Rect source

Sets the position of the rectangle.

Params:

NameTypeAttributeDescription
x number

Left

y number

Top

Return:

Rect

Self

public round(): Rect source

Rounds all the properties of the rectangle.

Return:

Rect

Self

public scale(f: number): Rect source

Scales the entire rectangle.

Params:

NameTypeAttributeDescription
f number

Factor to scale by

Return:

Rect

Self

public seperationAxis(b: Rect): string[] source

Finds the axis that seperate the rectangles.

Params:

NameTypeAttributeDescription
b Rect

Other rectangle

Return:

string[]

List of axis that seperate the two rectangles

public set(x: number, y: number, w: number, h: number): Rect source

Sets all properties.

Params:

NameTypeAttributeDescription
x number

Left

y number

Top

w number

Width

h number

Height

Return:

Rect

Self

public setCenter(v: Vec): Rect source

Centers the rectangle on given point.

Params:

NameTypeAttributeDescription
v Vec

Center point

Return:

Rect

Self

public shrink(val: number): Rect source

Shrinks the rectangle about the center.

Params:

NameTypeAttributeDescription
val number

Amount to shrink

Return:

Rect

Self

public split(f: number, axis: string): Rect[] source

Splits the rectangle into two along one axis.

Params:

NameTypeAttributeDescription
f number

Ratio of first part to original

axis string

Axis along which to split

Return:

Rect[]

Parts

public zoom(z: number): Rect source

Scales the dimensions about the center.

Params:

NameTypeAttributeDescription
z number

Factor to scale by

Return:

Rect

Self