From 3a908c50ccb9975dda8d6a0fd97e9963c3052454 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Thu, 15 Oct 2020 18:39:33 -0400 Subject: math: rect: Move functional procedures after the imperative ones. The functional procedures weren't working because of a quirk with Guile's handling of the top level. --- chickadee/math/rect.scm | 117 +++++++++++++++++++++++++----------------------- 1 file changed, 61 insertions(+), 56 deletions(-) diff --git a/chickadee/math/rect.scm b/chickadee/math/rect.scm index b4f1ba4..25113ad 100644 --- a/chickadee/math/rect.scm +++ b/chickadee/math/rect.scm @@ -172,62 +172,6 @@ "Restrict Y to the portion of the y axis covered by RECT." (clamp (rect-bottom rect) (rect-top rect) y)) -(define (vec2-clamp-to-rect v rect) - "Return a new vec2 with the x and y coordinates of the vec2 V -restricted so that they are within the bounds of RECT." - (vec2-clamp-to-rect! (vec2-copy v) rect)) - -(define (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." - (with-new-rect new - (rect-copy! rect1 new) - (rect-clamp! new rect2))) - -(define-inlinable (rect-move rect x y) - "Return a new rect based on RECT but moved to location (X, Y)." - (make-rect x y (rect-width rect) (rect-height rect))) - -(define-inlinable (rect-move-vec2 rect v) - "Return a new rect based on RECT but moved to the vec2 V." - (make-rect (vec2-x v) (vec2-y v) (rect-width rect) (rect-height rect))) - -(define-inlinable (rect-move-by rect x y) - "Return a new rect based on RECT but moved by (X, Y) units relative -to its current location." - (with-new-rect new - (rect-copy! rect new) - (rect-move-by! new x y))) - -(define-inlinable (rect-move-by-vec2 rect v) - "Return a new rect based on RECT but moved by the vec2 V relative to -its current location." - (with-new-rect new - (rect-copy! rect new) - (rect-move-by-vec2! new v))) - -(define-inlinable (rect-inflate rect width height) - "Return a new rect based on RECT but grown by WIDTH on the x axis -and HEIGHT on the y axis while keeping the rect centered around the -same point." - (with-new-rect rect - (rect-inflate! rect width height))) - -(define (rect-union rect1 rect2) - "Return a new rect that completely covers the area of RECT1 and -RECT2." - (with-new-rect rect - (rect-copy! rect2 rect1) - (rect-union! rect1 rect2))) - -(define (rect-clip rect1 rect2) - "Return a new rectangle that is the overlapping region of RECT1 and -RECT2. If the rects do not overlap, a rect of size 0 is returned." - (with-new-rect rect - (rect-copy! rect2 rect1) - (rect-clip! rect1 rect2))) - ;;; ;;; In-place operations @@ -335,6 +279,67 @@ fit completely within RECT2." (- (rect-top rect2) (rect-height rect1)) (rect-y rect1)))))) + +;;; +;;; Functional operations +;;; + +(define (vec2-clamp-to-rect v rect) + "Return a new vec2 with the x and y coordinates of the vec2 V +restricted so that they are within the bounds of RECT." + (vec2-clamp-to-rect! (vec2-copy v) rect)) + +(define (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." + (with-new-rect new + (rect-copy! rect1 new) + (rect-clamp! new rect2))) + +(define-inlinable (rect-move rect x y) + "Return a new rect based on RECT but moved to location (X, Y)." + (make-rect x y (rect-width rect) (rect-height rect))) + +(define-inlinable (rect-move-vec2 rect v) + "Return a new rect based on RECT but moved to the vec2 V." + (make-rect (vec2-x v) (vec2-y v) (rect-width rect) (rect-height rect))) + +(define-inlinable (rect-move-by rect x y) + "Return a new rect based on RECT but moved by (X, Y) units relative +to its current location." + (with-new-rect new + (rect-copy! rect new) + (rect-move-by! new x y))) + +(define-inlinable (rect-move-by-vec2 rect v) + "Return a new rect based on RECT but moved by the vec2 V relative to +its current location." + (with-new-rect new + (rect-copy! rect new) + (rect-move-by-vec2! new v))) + +(define-inlinable (rect-inflate rect width height) + "Return a new rect based on RECT but grown by WIDTH on the x axis +and HEIGHT on the y axis while keeping the rect centered around the +same point." + (with-new-rect rect + (rect-inflate! rect width height))) + +(define (rect-union rect1 rect2) + "Return a new rect that completely covers the area of RECT1 and +RECT2." + (with-new-rect rect + (rect-copy! rect2 rect1) + (rect-union! rect1 rect2))) + +(define (rect-clip rect1 rect2) + "Return a new rectangle that is the overlapping region of RECT1 and +RECT2. If the rects do not overlap, a rect of size 0 is returned." + (with-new-rect rect + (rect-copy! rect2 rect1) + (rect-clip! rect1 rect2))) + ;;; ;;; Queries -- cgit v1.2.3