1 ;;; Gacela, a GNU Common Lisp extension for fast games development
2 ;;; Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License as published by
6 ;;; the Free Software Foundation, either version 3 of the License, or
7 ;;; (at your option) any later version.
9 ;;; This program is distributed in the hope that it will be useful,
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ;;; GNU General Public License for more details.
14 ;;; You should have received a copy of the GNU General Public License
15 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
20 (defconstant INFINITY MOST-POSITIVE-LONG-FLOAT)
22 (defun append-if (new test tree &key (key #'first) (test-if #'equal))
23 (cond ((atom tree) tree)
27 (mapcar (lambda (x) (append-if new test x :key key :test-if test-if)) tree)
31 (defun append-if-1 (new test tree &key (key #'first) (test-if #'equal))
32 (cond ((funcall test-if (funcall key tree) test) (append tree new))
36 (if (listp var) (car var) var))
38 (defun avg (&rest numbers)
40 (dolist (n numbers) (incf total n))
41 (/ total (length numbers))))
47 (let ((sig (signum num)))
51 (defmacro destructure (destructuring-list &body body)
52 (let ((lambda-list nil) (exp-list nil))
53 (dolist (pair destructuring-list)
54 (setq exp-list (cons (car pair) exp-list))
55 (setq lambda-list (cons (cadr pair) lambda-list)))
56 `(destructuring-bind ,lambda-list ,(cons 'list exp-list) ,@body)))
58 (defun match-pattern (list pattern)
59 (cond ((and (null list) (null pattern)) t)
60 ((and (consp list) (consp pattern))
61 (and (match-pattern (car list) (car pattern)) (match-pattern (cdr list) (cdr pattern))))
62 ((and (atom list) (atom pattern))
63 (cond ((or (numberp list) (numberp pattern)) (and (numberp list) (numberp pattern)))
66 (defun nearest-power-of-two (n)
68 (cond ((> (* p 2) n) p)
69 (t (power (* p 2) n)))))
74 (match-pattern dot '(0 0)))
76 (defun vectorp (vector)
77 (match-pattern vector '(0 0)))
79 (defun circlep (circle)
80 (match-pattern circle '((0 0) 0)))
82 (defun polygonp (polygon)
83 (cond ((consp polygon)
84 (and (dotp (car polygon))
85 (if (null (cdr polygon)) t (polygonp (cdr polygon)))))))
90 (defun make-vector (x y)
93 (defun make-line (dot1 dot2)
96 (defun make-rectangle (x1 y1 x2 y2)
97 `((,x1 ,y1) (,x2 ,y1) (,x2 ,y2) (,x1 ,y2)))
99 (defun polygon-center (polygon)
100 (apply #'mapcar #'avg polygon))
102 (defun dots-distance (dot1 dot2)
103 (destructure ((dot1 (x1 y1))
105 (sqrt (+ (expt (- x2 x1) 2)
106 (expt (- y2 y1) 2)))))
108 (defun dot-line-distance (dot line)
109 (destructure ((line ((ax ay) (bx by)))
111 (let* ((r-numerator (+ (* (- cx ax) (- bx ax)) (* (- cy ay) (- by ay))))
112 (r-denomenator (+ (expt (- bx ax) 2) (expt (- by ay) 2)))
113 (r (/ r-numerator r-denomenator)))
115 (* (abs (/ (- (* (- ay cy) (- bx ax)) (* (- ax cx) (- by ay)))
117 (sqrt r-denomenator))
120 (defun dot-segment-distance (dot segment)
122 (dist r) (dot-line-distance dot segment)
123 (cond ((and (>= r 0) (<= r 1)) dist)
124 (t (let ((dist1 (dots-distance dot (car segment)))
125 (dist2 (dots-distance dot (cadr segment))))
126 (if (< dist1 dist2) dist1 dist2))))))
128 (defun perpendicular-line (dot line)
129 (destructure ((line ((ax ay) (bx by))))
131 (dist r) (dot-line-distance dot line)
133 (make-dot (+ ax (* r (- bx ax)))
134 (+ ay (* r (- by ay))))))))
136 (defun line-angle (line)
137 (destructure ((line ((ax ay) (bx by))))
138 (let ((x (- bx ax)) (y (- by ay)))
139 (if (and (= x 0) (= y 0)) 0 (atan y x)))))
141 (defun inverse-angle (angle)
142 (cond ((< angle pi) (+ angle pi))
145 (defun translate-dot (dot dx dy)
146 (destructure ((dot (x y)))
147 (list (+ x dx) (+ y dy))))
149 (defun translate-circle (circle dx dy)
150 (destructure ((circle (center radius)))
151 (list (translate-dot center dx dy) radius)))
153 (defun translate-polygon (pol dx dy)
154 (mapcar (lambda (dot)
155 (translate-dot dot dx dy))
158 (defun polygon-edges (pol)
159 (mapcar (lambda (v1 v2) (list v1 v2))
161 (union (cdr pol) (list (car pol)))))
163 (defun polygon-dot-intersection (polygon dot)
164 ;Eric Haines algorithm
165 (let ((edges (polygon-edges
166 (translate-polygon polygon (neg (car dot)) (neg (cadr dot)))))
169 (destructure ((edge ((x1 y1) (x2 y2))))
170 (cond ((/= (signum+ y1) (signum+ y2))
171 (cond ((and (> x1 0) (> x2 0)) (incf counter))
172 ((and (or (> x1 0) (> x2 0))
173 (> (- x1 (* y1 (/ (- x2 x1) (- y2 y1)))) 0))
175 (not (evenp counter))))
177 (defun circle-segment-intersection (circle segment)
178 (destructure ((circle (center radius)))
179 (<= (dot-segment-distance center segment) radius)))
181 (defun circle-edges-intersection (circle polygon)
182 (let ((edges (polygon-edges polygon))
185 (cond ((circle-segment-intersection circle edge) (setq edges-i (cons edge edges-i)))))
188 (defun circle-polygon-intersection (circle polygon)
189 (or (polygon-dot-intersection polygon (car circle))
190 (circle-edges-intersection circle polygon)))
192 (defun circle-circle-intersection (circle1 circle2)
193 (destructure ((circle1 (center1 radius1))
194 (circle2 (center2 radius2)))
195 (<= (dots-distance center1 center2) (+ r1 r2))))