Next: , Previous: , Up: Math   [Contents][Index]


2.2.3 Rectangles

The (chickadee math rect) module provides an API for manipulating axis-aligned bounding boxes (AABBs). AABBs are often used for collision detection in games. Common use-cases are defining “hitboxes” in platformers or using them for the “broad phase” of a collision detection algorithm that uses a more complex (and thus slower) method of determining the actual collisions.

Like some of the other math modules, there exists a collection of functions that do in-place modification of rectangles for use in performance critical code paths.

Procedure: rect x y width height
Procedure: make-rect x y width height

Create a new rectangle that is width by height in size and whose bottom-left corner is located at (x, y).

Procedure: rect? obj

Return #t if obj is a rectangle.

Procedure: rect-within? rect1 rect2

Return #t if rect2 is completely within rect1.

Procedure: rect-intersects? rect1 rect2

Return #t if rect2 overlaps rect1.

Procedure: rect-contains? rect x y

Return #t if the coordinates (x, y) are within rect.

Procedure: rect-contains-vec2? rect v

Return #t if the 2D vector v is within the bounds of rect.

Procedure: rect-x rect

Return the X coordinate of the lower-left corner of rect.

Procedure: rect-y rect

Return the Y coordinate of the lower-left corner of rect.

Procedure: rect-left rect

Return the left-most X coordinate of rect.

Procedure: rect-right rect

Return the right-most X coordinate of rect.

Procedure: rect-bottom rect

Return the bottom-most Y coordinate of rect.

Procedure: rect-top rect

Return the top-most Y coordinate of rect.

Procedure: rect-center-x rect

Return the X coordinate of the center of rect.

Procedure: rect-center-y rect

Return the Y coordinate of the center of rect.

Procedure: rect-width rect

Return the width of rect.

Procedure: rect-height rect

Return the height of rect.

Procedure: rect-area rect

Return the surface area covered by rect.

Procedure: rect-clamp-x rect x

Restrict x to the portion of the X axis covered by rect.

Procedure: rect-clamp-y rect y

Restrict y to the portion of the Y axis covered by rect.

Procedure: rect-clamp rect1 rect2

Return a new rect that adjusts the location of rect1 so that it is completely within rect2. An exception is thrown in the case that rect1 cannot fit completely within rect2.

Procedure: rect-move rect x y

Return a new rectangle based on rect but moved to the coordinates (x, y).

Procedure: rect-move-vec2 rect v

Return a new rectangle based on rect but moved to the coordinates in the 2D vector v.

Procedure: rect-move-by rect x y

Return a new rectangle based on rect but moved by (x, y) units relative to its current location.

Procedure: rect-move-by-vec2 rect v

Return a new rectangle based on rect but moved by the 2D vector v relative to its current location.

Procedure: rect-inflate rect width height

Return a new rectangle based on rect, but expanded by width units on the X axis and height units on the Y axis, while keeping the rectangle centered on the same point.

Procedure: rect-union rect1 rect2

Return a new rectangle that completely covers the area of rect1 and rect2.

Procedure: rect-clip rect1 rect2

Return a new rectangle that is the overlapping region of rect1 and rect2. If the two rectangles do not overlap, a rectangle of 0 width and 0 height is returned.

Procedure: set-rect-x! rect x

Set the left X coordinate of rect to x.

Procedure: set-rect-y! rect y

Set the bottom Y coordinate of rect to y.

Procedure: set-rect-width! rect width

Set the width of rect to width.

Procedure: set-rect-height! rect height

Set the height of rect to height.

Procedure: rect-move! rect x y

Move rect to (x, y) in-place.

Procedure: rect-move-vec2! rect v

Move rect to the 2D vector v in-place.

Procedure: rect-move-by! rect x y

Move rect by (x, y) in-place.

Procedure: rect-move-by-vec2! rect v

Move rect by the 2D vector v in-place.

Procedure: rect-inflate! rect width height

Expand rect by width and height in-place.

Procedure: rect-union! rect1 rect2

Modify rect1 in-place to completely cover the area of both rect1 and rect2.

Procedure: rect-clip! rect1 rect2

Modify rect1 in-place to be the overlapping region of rect1 and rect2.

Procedure: rect-clamp! rect1 rect2

Adjust the location of rect1 in-place so that its bounds are completely within rect2. An exception is thrown in the case that rect1 cannot fit completely within rect2.

Procedure: vec2-clamp-to-rect! v rect

Restrict the coordinates of the 2D vector v so that they are within the bounds of rect. v is modified in-place.


Next: , Previous: , Up: Math   [Contents][Index]