1.11 <GrapheneRect>

Structure: <GrapheneRect>

The location and size of a rectangle region.

The width and height of a <GrapheneRect> can be negative; for instance, a <GrapheneRect> with an origin of [ 0, 0 ] and a size of [ 10, 10 ] is equivalent to a <GrapheneRect> with an origin of [ 10, 10 ] and a size of [ -10, -10 ].

Application code can normalize rectangles using rect:normalize; this function will ensure that the width and height of a rectangle are positive values. All functions taking a <GrapheneRect> as an argument will internally operate on a normalized copy; all functions returning a <GrapheneRect> will always return a normalized rectangle.

Fields

origin

the coordinates of the origin of the rectangle

size

the size of the rectangle

Procedures

Procedure: rect:alloc ⇒ %return

Allocates a new <GrapheneRect>.

The contents of the returned rectangle are undefined.

%return

Returned: a <GrapheneRect>.

the newly allocated rectangle

Method: rect:contains-point? self p ⇒ %return
Method: contains-point? self p ⇒ %return

Checks whether a <GrapheneRect> contains the given coordinates.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

p

Passed: a <GraphenePoint>.

a <GraphenePoint>

%return

Returned: a boolean.

true if the rectangle contains the point

Method: rect:contains-rect? self b ⇒ %return
Method: contains-rect? self b ⇒ %return

Checks whether a <GrapheneRect> fully contains the given rectangle.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

b

Passed: a <GrapheneRect>.

a <GrapheneRect>

%return

Returned: a boolean.

true if the rectangle self fully contains b

Method: rect:equal? self b ⇒ %return
Method: equal? self b ⇒ %return

Checks whether the two given rectangle are equal.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

b

Passed: a <GrapheneRect>.

a <GrapheneRect>

%return

Returned: a boolean.

true if the rectangles are equal

Method: rect:expand! self p res ⇒ res
Method: expand! self p res ⇒ res

Expands a <GrapheneRect> to contain the given <GraphenePoint>.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

p

Passed: a <GraphenePoint>.

a <GraphenePoint>

res

Passed, filled in, and returned: a <GrapheneRect>.

return location for the expanded rectangle

Method: rect:free self
Method: free self

Frees the resources allocated by rect-alloc.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

Method: rect:get-area self ⇒ %return
Method: get-area self ⇒ %return

Compute the area of given normalized rectangle.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

%return

Returned: a real number.

the area of the normalized rectangle

Method: rect:get-bottom-left! self p ⇒ p
Method: get-bottom-left! self p ⇒ p

Retrieves the coordinates of the bottom-left corner of the given rectangle.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

p

Passed, filled in, and returned: a <GraphenePoint>.

return location for a <GraphenePoint>

Method: rect:get-bottom-right! self p ⇒ p
Method: get-bottom-right! self p ⇒ p

Retrieves the coordinates of the bottom-right corner of the given rectangle.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

p

Passed, filled in, and returned: a <GraphenePoint>.

return location for a <GraphenePoint>

Method: rect:get-center! self p ⇒ p
Method: get-center! self p ⇒ p

Retrieves the coordinates of the center of the given rectangle.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

p

Passed, filled in, and returned: a <GraphenePoint>.

return location for a <GraphenePoint>

Method: rect:get-height self ⇒ %return
Method: get-height self ⇒ %return

Retrieves the normalized height of the given rectangle.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

%return

Returned: a real number.

the normalized height of the rectangle

Method: rect:get-top-left! self p ⇒ p
Method: get-top-left! self p ⇒ p

Retrieves the coordinates of the top-left corner of the given rectangle.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

p

Passed, filled in, and returned: a <GraphenePoint>.

return location for a <GraphenePoint>

Method: rect:get-top-right! self p ⇒ p
Method: get-top-right! self p ⇒ p

Retrieves the coordinates of the top-right corner of the given rectangle.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

p

Passed, filled in, and returned: a <GraphenePoint>.

return location for a <GraphenePoint>

Method: rect:get-vertices! self vertices ⇒ vertices
Method: get-vertices! self vertices ⇒ vertices

Computes the four vertices of a <GrapheneRect>.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

vertices

Passed: a list or vector, each a <GrapheneVec2>. It is filled in, and returned: a vector, each a <GrapheneVec2>.

return location for an array of 4 <GrapheneVec2>

Method: rect:get-width self ⇒ %return
Method: get-width self ⇒ %return

Retrieves the normalized width of the given rectangle.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

%return

Returned: a real number.

the normalized width of the rectangle

Method: rect:get-x self ⇒ %return
Method: get-x self ⇒ %return

Retrieves the normalized X coordinate of the origin of the given rectangle.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

%return

Returned: a real number.

the normalized X coordinate of the rectangle

Method: rect:get-y self ⇒ %return
Method: get-y self ⇒ %return

Retrieves the normalized Y coordinate of the origin of the given rectangle.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

%return

Returned: a real number.

the normalized Y coordinate of the rectangle

Method: rect:init self x y width height ⇒ %return
Method: init self x y width height ⇒ %return

Initializes the given <GrapheneRect> with the given values.

This function will implicitly normalize the <GrapheneRect> before returning.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

x

Passed: a real number.

the X coordinate of the graphene-rect-t.origin

y

Passed: a real number.

the Y coordinate of the graphene-rect-t.origin

width

Passed: a real number.

the width of the graphene-rect-t.size

height

Passed: a real number.

the height of the graphene-rect-t.size

%return

Returned: a <GrapheneRect>.

the initialized rectangle

Method: rect:init-from-rect self src ⇒ %return
Method: init-from-rect self src ⇒ %return

Initializes self using the given src rectangle.

This function will implicitly normalize the <GrapheneRect> before returning.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

src

Passed: a <GrapheneRect>.

a <GrapheneRect>

%return

Returned: a <GrapheneRect>.

the initialized rectangle

Method: rect:inset self d-x d-y ⇒ %return
Method: inset self d-x d-y ⇒ %return

Changes the given rectangle to be smaller, or larger depending on the given inset parameters.

To create an inset rectangle, use positive d-x or d-y values; to create a larger, encompassing rectangle, use negative d-x or d-y values.

The origin of the rectangle is offset by d-x and d-y, while the size is adjusted by (2 * @d_x, 2 * @d_y). If d-x and d-y are positive values, the size of the rectangle is decreased; if d-x and d-y are negative values, the size of the rectangle is increased.

If the size of the resulting inset rectangle has a negative width or height then the size will be set to zero.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

d-x

Passed: a real number.

the horizontal inset

d-y

Passed: a real number.

the vertical inset

%return

Returned: a <GrapheneRect>.

the inset rectangle

Method: rect:inset-r! self d-x d-y res ⇒ res
Method: inset-r! self d-x d-y res ⇒ res

Changes the given rectangle to be smaller, or larger depending on the given inset parameters.

To create an inset rectangle, use positive d-x or d-y values; to create a larger, encompassing rectangle, use negative d-x or d-y values.

The origin of the rectangle is offset by d-x and d-y, while the size is adjusted by (2 * @d_x, 2 * @d_y). If d-x and d-y are positive values, the size of the rectangle is decreased; if d-x and d-y are negative values, the size of the rectangle is increased.

If the size of the resulting inset rectangle has a negative width or height then the size will be set to zero.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

d-x

Passed: a real number.

the horizontal inset

d-y

Passed: a real number.

the vertical inset

res

Passed, filled in, and returned: a <GrapheneRect>.

return location for the inset rectangle

Method: rect:interpolate! self b factor res ⇒ res
Method: interpolate! self b factor res ⇒ res

Linearly interpolates the origin and size of the two given rectangles.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

b

Passed: a <GrapheneRect>.

a <GrapheneRect>

factor

Passed: a real number.

the linear interpolation factor

res

Passed, filled in, and returned: a <GrapheneRect>.

return location for the interpolated rectangle

Method: rect:intersection! self b res ⇒ %return res
Method: intersection! self b res ⇒ %return res

Computes the intersection of the two given rectangles.

![](rectangle-intersection.png)

The intersection in the image above is the blue outline.

If the two rectangles do not intersect, res will contain a degenerate rectangle with origin in (0, 0) and a size of 0.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

b

Passed: a <GrapheneRect>.

a <GrapheneRect>

res

Passed, filled in, and returned: a <GrapheneRect>.

return location for a <GrapheneRect>

%return

Returned: a boolean.

true if the two rectangles intersect

Method: rect:normalize self ⇒ %return
Method: normalize self ⇒ %return

Normalizes the passed rectangle.

This function ensures that the size of the rectangle is made of positive values, and that the origin is the top-left corner of the rectangle.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

%return

Returned: a <GrapheneRect>.

the normalized rectangle

Method: rect:normalize-r! self res ⇒ res
Method: normalize-r! self res ⇒ res

Normalizes the passed rectangle.

This function ensures that the size of the rectangle is made of positive values, and that the origin is in the top-left corner of the rectangle.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

res

Passed, filled in, and returned: a <GrapheneRect>.

the return location for the normalized rectangle

Method: rect:offset self d-x d-y ⇒ %return
Method: offset self d-x d-y ⇒ %return

Offsets the origin by d-x and d-y.

The size of the rectangle is unchanged.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

d-x

Passed: a real number.

the horizontal offset

d-y

Passed: a real number.

the vertical offset

%return

Returned: a <GrapheneRect>.

the offset rectangle

Method: rect:offset-r! self d-x d-y res ⇒ res
Method: offset-r! self d-x d-y res ⇒ res

Offsets the origin of the given rectangle by d-x and d-y.

The size of the rectangle is left unchanged.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

d-x

Passed: a real number.

the horizontal offset

d-y

Passed: a real number.

the vertical offset

res

Passed, filled in, and returned: a <GrapheneRect>.

return location for the offset rectangle

Method: rect:round! self res ⇒ res
Method: round! self res ⇒ res

Rounds the origin and size of the given rectangle to their nearest integer values; the rounding is guaranteed to be large enough to have an area bigger or equal to the original rectangle, but might not fully contain its extents. Use rect:round-extents! in case you need to round to a rectangle that covers fully the original one.

This function is the equivalent of calling floor on the coordinates of the origin, and ceil on the size.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

res

Passed, filled in, and returned: a <GrapheneRect>.

return location for the rounded rectangle

Method: rect:round-extents! self res ⇒ res
Method: round-extents! self res ⇒ res

Rounds the origin of the given rectangle to its nearest integer value and and recompute the size so that the rectangle is large enough to contain all the conrners of the original rectangle.

This function is the equivalent of calling floor on the coordinates of the origin, and recomputing the size calling ceil on the bottom-right coordinates.

If you want to be sure that the rounded rectangle completely covers the area that was covered by the original rectangle — i.e. you want to cover the area including all its corners — this function will make sure that the size is recomputed taking into account the ceiling of the coordinates of the bottom-right corner. If the difference between the original coordinates and the coordinates of the rounded rectangle is greater than the difference between the original size and and the rounded size, then the move of the origin would not be compensated by a move in the anti-origin, leaving the corners of the original rectangle outside the rounded one.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

res

Passed, filled in, and returned: a <GrapheneRect>.

return location for the rectangle with rounded extents

Method: rect:round-to-pixel self ⇒ %return
Method: round-to-pixel self ⇒ %return

Rounds the origin and the size of the given rectangle to their nearest integer values; the rounding is guaranteed to be large enough to contain the original rectangle.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

%return

Returned: a <GrapheneRect>.

the pixel-aligned rectangle.

Method: rect:scale! self s-h s-v res ⇒ res
Method: scale! self s-h s-v res ⇒ res

Scales the size and origin of a rectangle horizontaly by s-h, and vertically by s-v. The result res is normalized.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

s-h

Passed: a real number.

horizontal scale factor

s-v

Passed: a real number.

vertical scale factor

res

Passed, filled in, and returned: a <GrapheneRect>.

return location for the scaled rectangle

Method: rect:union! self b res ⇒ res
Method: union! self b res ⇒ res

Computes the union of the two given rectangles.

![](rectangle-union.png)

The union in the image above is the blue outline.

self

Passed: a <GrapheneRect>.

a <GrapheneRect>

b

Passed: a <GrapheneRect>.

a <GrapheneRect>

res

Passed, filled in, and returned: a <GrapheneRect>.

return location for a <GrapheneRect>

Procedure: rect:zero ⇒ %return

Returns a degenerate rectangle with origin fixed at (0, 0) and a size of 0, 0.

%return

Returned: a <GrapheneRect>.

a fixed rectangle