-
Notifications
You must be signed in to change notification settings - Fork 4
/
region.lisp
52 lines (46 loc) · 1.49 KB
/
region.lisp
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
;;;; region.lisp
;;;;
;;;; Copyright (c) 2014 Robert Smith
;;;;
;;;; Definition of a "region" used to identify which parts of a canvas
;;;; refer to which parts of tree of boxes.
(in-package #:formulador)
(defstruct (region (:predicate regionp)
(:constructor make-region (min-x min-y max-x max-y)))
min-x ; Inclusive
min-y ; Inclusive
max-x ; Exclusive
max-y) ; Exclusive
(defun make-region-by-dimensions (x y width height)
"Construct a new region starting at (X, Y) and having width WIDTH and height HEIGHT."
(make-region x
y
(+ x width)
(+ y height)))
(defun region= (r1 r2)
"Are the regions R1 and R2 equivalent?"
(and (= (region-min-x r1)
(region-min-x r2))
(= (region-min-y r1)
(region-min-y r2))
(= (region-max-x r1)
(region-max-x r2))
(= (region-max-y r1)
(region-max-y r2))))
(defun in-region-p (r x y)
"Is the point (X, Y) in the region R?"
(flet ((in (a x b)
(and (<= a x)
(< x b))))
(and (in (region-min-x r)
x
(region-max-x r))
(in (region-min-y r)
y
(region-max-y r)))))
(defun region-area (r)
"Compute the area of a region R."
(* (- (1- (region-max-x r))
(region-min-x r))
(- (1- (region-max-y r))
(region-min-y r))))