summaryrefslogtreecommitdiff
path: root/2d/rect.scm
blob: 4cabac3b1b6d748a8273cec4f429d77348f6811c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
;;; guile-2d
;;; Copyright (C) 2013 David Thompson <dthompson2@worcester.edu>
;;;
;;; Guile-2d is free software: you can redistribute it and/or modify it
;;; under the terms of the GNU Lesser General Public License as
;;; published by the Free Software Foundation, either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; Guile-2d is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; Lesser General Public License for more details.
;;;
;;; You should have received a copy of the GNU Lesser General Public
;;; License along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; Rects are axis-aligned bounding boxes that can be used for
;; performing simple collision detection.
;;
;;; Code:

(define-module (2d rect)
  #:use-module (srfi srfi-9)
  #:use-module (2d vector2)
  #:export (<rect>
            make-rect
            rect?
            rect-x
            rect-y
            rect-x2
            rect-y2
            rect-center-x
            rect-center-y
            rect-half-width
            rect-half-height
            rect-width
            rect-height
            rect-position
            rect-size
            rect-move
            rect-inflate
            rect-union
            rect-clip
            rect-within?
            rect-intersects?
            rect-contains?))

;;;
;;; Rectangles
;;;

;; The rect API is very similar to the Pygame rect API, but rects are
;; immutable.

(define-record-type <rect>
  (make-rect x y width height)
  rect?
  (x rect-x)
  (y rect-y)
  (width rect-width)
  (height rect-height))

(define (rect-x2 rect)
  (+ (rect-x rect) (rect-width rect) -1))

(define (rect-y2 rect)
  (+ (rect-y rect) (rect-height rect) -1))

(define (rect-center-x rect)
  (+ (rect-x rect) (rect-half-width rect)))

(define (rect-center-y rect)
  (+ (rect-y rect) (rect-half-height rect)))

(define (rect-half-width rect)
  (/ (rect-width rect) 2))

(define (rect-half-height rect)
  (/ (rect-height rect) 2))

(define (rect-position rect)
  "Return the top-left corner of RECT as a vector2."
  (vector2 (rect-x rect)
           (rect-y rect)))

(define (rect-size rect)
  "Return the size of RECT as a vector2."
  (vector2 (rect-width rect)
           (rect-height rect)))

(define (%rect-move rect x y)
  "Move a rect by the given offset."
  (make-rect (+ (rect-x rect) x)
             (+ (rect-y rect) y)
             (rect-width rect)
             (rect-height rect)))

(define rect-move
  (case-lambda
    "Create a new rectangle by moving RECT by the given
offset. rect-move accepts a vector2 or x and y coordinates as separate
arguments."
    ((rect v)
     (%rect-move rect (vx v) (vy v)))
    ((rect x y)
     (%rect-move rect x y))))

(define (%rect-inflate rect width height)
  "Grows the rect by the given amount. The rect stays centered around
its current center."
  (make-rect (+ (rect-x rect) (/ width 2))
             (+ (rect-y rect) (/ height 2))
             (+ (rect-width rect) width)
             (+ (rect-height rect) height)))

(define rect-inflate
  (case-lambda
    "Create a new rectangle by growing RECT by the given amount
without changing the center point. rect-inflate accepts a vector2 or x
and y coordinates as separate arguments."
    ((rect v)
     (%rect-inflate rect (vx v) (vy v)))
    ((rect x y)
     (%rect-inflate rect x y))))

(define (rect-union rect1 rect2)
  "Returns a rect that covers the area of rect1 and rect2."
  (let ((x1 (min (rect-x  rect1) (rect-x  rect2)))
        (x2 (max (rect-x2 rect1) (rect-x2 rect2)))
        (y1 (min (rect-y  rect1) (rect-y  rect2)))
        (y2 (max (rect-y2 rect1) (rect-y2 rect2))))
    (make-rect x1 y1 (- x2 x1) (- y2 y1))))

(define (rect-clip rect1 rect2)
  "Returns the overlapping region of rect1 and rect2. If the rects do
not overlap, a rect of size 0 is returned."
  (let ((x1 (max (rect-x  rect1) (rect-x  rect2)))
        (x2 (min (rect-x2 rect1) (rect-x2 rect2)))
        (y1 (max (rect-y  rect1) (rect-y  rect2)))
        (y2 (min (rect-y2 rect1) (rect-y2 rect2))))
    (make-rect x1 y1 (max (- x2 x1) 0) (max (- y2 y1) 0))))

(define (rect-within? rect1 rect2)
  "Tests if rect2 is completely within rect1."
  (and (>= (rect-x  rect2) (rect-x  rect1))
       (<= (rect-x  rect2) (rect-x2 rect1))
       (>= (rect-x2 rect2) (rect-x  rect1))
       (<= (rect-x2 rect2) (rect-x2 rect1))
       (>= (rect-y  rect2) (rect-y  rect1))
       (<= (rect-y  rect2) (rect-y2 rect1))
       (>= (rect-y2 rect2) (rect-y  rect1))
       (<= (rect-y2 rect2) (rect-y2 rect1))))

(define (rect-intersects? rect1 rect2)
  "Tests if rect2 overlaps rect1."
  (or (and (>= (rect-x  rect2) (rect-x  rect1))
           (<= (rect-x  rect2) (rect-x2 rect1)))
      (and (>= (rect-x2 rect2) (rect-x  rect1))
           (<= (rect-x2 rect2) (rect-x2 rect1))))
  (or (and (>= (rect-y  rect2) (rect-y  rect1))
           (<= (rect-y  rect2) (rect-y2 rect1)))
      (and (>= (rect-y2 rect2) (rect-y  rect1))
           (<= (rect-y2 rect2) (rect-y2 rect1)))))

(define (%rect-contains? rect x y)
  (and (>= x (rect-x  rect))
       (<= x (rect-x2 rect))
       (>= y (rect-y  rect))
       (<= y (rect-y2 rect))))

(define rect-contains?
  (case-lambda
    "Tests if the given point is within rect."
    ((rect v)
     (%rect-contains? rect (vx v) (vy v)))
    ((rect x y)
     (%rect-contains? rect x y))))