Skip to main content

Box2

Represents an axis-aligned bounding box (AABB) in 2D space.

Constructor

Box2

function Box2( min: Vector2, max: Vector2 ): void;  

min - (optional) Vector2 representing the lower (x, y) boundary of the box. Default is ( + Infinity, + Infinity ).
max - (optional) Vector2 representing the upper (x, y) boundary of the box. Default is ( - Infinity, - Infinity ).

Creates a Box2 bounded by min and max.

Properties

min

min: Vector2;  

Vector2 representing the lower (x, y) boundary of the box.
Default is ( + Infinity, + Infinity ).

max

max: Vector2;  

Vector2 representing the lower upper (x, y) boundary of the box.
Default is ( - Infinity, - Infinity ).

Methods

clampPoint

function clampPoint( point: Vector2, target: Vector2 ): Vector2;  

point - Vector2 to clamp.
target — the result will be copied into this Vector2.

Clamps the point within the bounds of this box.

clone

function clone( ): Box2;  

Returns a new Box2 with the same .min and .max as this one.

containsBox

function containsBox( box: Box2 ): Boolean;  

box - Box2 to test for inclusion.

Returns true if this box includes the entirety of box. If this and box are identical,
this function also returns true.

containsPoint

function containsPoint( point: Vector2 ): Boolean;  

point - Vector2 to check for inclusion.

Returns true if the specified point lies within or on the boundaries of this box.

copy

function copy( box: Box2 ): this;  

Copies the .min and .max from .ox2 to this box.

distanceToPoint

function distanceToPoint( point: Vector2 ): Float;  

point - Vector2 to measure distance to.

Returns the distance from any edge of this box to the specified point. If the point lies inside of this box, the distance will be 0.

equals

function equals( box: Box2 ): Boolean;  

box - Box to compare with this one.

Returns true if this box and box share the same lower and upper bounds.

expandByPoint

function expandByPoint( point: Vector2 ): this;  

point - Vector2 that should be included in the box.

Expands the boundaries of this box to include point.

expandByScalar

function expandByScalar( scalar: Float ): this;  

scalar - Distance to expand the box by.

Expands each dimension of the box by scalar. If negative, the dimensions of the box will be contracted.

expandByVector

function expandByVector( vector: Vector2 ): this;  

vector - Vector2 to expand the box by.

Expands this box equilaterally by vector. The width of this box will be expanded by the x component of vector in both directions. The height of this box will be expanded by the y component of vector in both directions.

getCenter

function getCenter( target: Vector2 ): Vector2;  

target — the result will be copied into this Vector2.

Returns the center point of the box as a Vector2.

getParameter

function getParameter( point: Vector2, target: Vector2 ): Vector2;  

point - Vector2.
target — the result will be copied into this Vector2.

Returns a point as a proportion of this box's width and height.

getSize

function getSize( target: Vector2 ): Vector2;  

target — the result will be copied into this Vector2.

Returns the width and height of this box.

intersect

function intersect( box: Box2 ): this;  

box - Box to intersect with.

Returns the intersection of this and box, setting the upper bound of this box to the lesser of the two boxes' upper bounds and the lower bound of this box to the greater of the two boxes' lower bounds.

intersectsBox

function intersectsBox( box: Box2 ): Boolean;  

box - Box to check for intersection against.

Determines whether or not this box intersects box.

isEmpty

function isEmpty( ): Boolean;  

Returns true if this box includes zero points within its bounds.
Note that a box with equal lower and upper bounds still includes one point, the one both bounds share.

makeEmpty

function makeEmpty( ): this;  

Makes this box empty.

set

function set( min: Vector2, max: Vector2 ): this;  

min - (required ) Vector2 representing the lower (x, y) boundary of the box.
max - (required) Vector2 representing the upper (x, y) boundary of the box.

Sets the lower and upper (x, y) boundaries of this box.
Please note that this method only copies the values from the given objects.

setFromCenterAndSize

function setFromCenterAndSize( center: Vector2, size: Vector2 ): this;  

center - Desired center position of the box (Vector2).
size - Desired x and y dimensions of the box (Vector2).

Centers this box on center and sets this box's width and height to the values specified in size.

setFromPoints

function setFromPoints( points: Array ): this;  

points - Array of Vector2s that the resulting box will contain.

Sets the upper and lower bounds of this box to include all of the points in points.

translate

function translate( offset: Vector2 ): this;  

offset - Direction and distance of offset.

Adds offset to both the upper and lower bounds of this box, effectively moving this box offset units in 2D space.

union

function union( box: Box2 ): this;  

box - Box that will be unioned with this box.

Unions this box with box, setting the upper bound of this box to the greater of the two boxes' upper bounds and the lower bound of this box to the lesser of the two boxes' lower bounds.

Source

src/math/Box2.js