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/>.
17 (when (not (find-package 'gacela))
18 (make-package 'gacela :nicknames '(gg) :use '(lisp)))
20 (eval-when (eval) (in-package 'gacela :nicknames '(gg) :use '(lisp)))
22 (defconstant INFINITY MOST-POSITIVE-LONG-FLOAT)
24 (defun append-if (new test tree &key (key #'first) (test-if #'equal))
25 (cond ((atom tree) tree)
29 (mapcar (lambda (x) (append-if new test x :key key :test-if test-if)) tree)
33 (defun append-if-1 (new test tree &key (key #'first) (test-if #'equal))
34 (cond ((funcall test-if (funcall key tree) test) (append tree new))
38 (if (listp var) (car var) var))
40 (defun avg (&rest numbers)
42 (dolist (n numbers) (incf total n))
43 (/ total (length numbers))))
49 (let ((sig (signum num)))
53 (defmacro destructure (destructuring-list &body body)
54 (let ((lambda-list nil) (exp-list nil))
55 (dolist (pair destructuring-list)
56 (setq exp-list (cons (car pair) exp-list))
57 (setq lambda-list (cons (cadr pair) lambda-list)))
58 `(destructuring-bind ,lambda-list ,(cons 'list exp-list) ,@body)))
60 (defun match-pattern (list pattern)
61 (cond ((and (null list) (null pattern)) t)
62 ((and (consp list) (consp pattern))
63 (and (match-pattern (car list) (car pattern)) (match-pattern (cdr list) (cdr pattern))))
64 ((and (atom list) (atom pattern))
65 (cond ((or (numberp list) (numberp pattern)) (and (numberp list) (numberp pattern)))
68 (defun nearest-power-of-two (n)
70 (cond ((> (* p 2) n) p)
71 (t (power (* p 2) n)))))
74 (defmacro secure-block (output-stream &rest forms)
75 (let ((error-handler #'si::universal-error-handler))
77 (defun si::universal-error-handler (error-name correctable function-name continue-format-string error-format-string &rest args)
80 (cond ((eq error-name :WRONG-TYPE-ARGUMENT) (string error-name))
81 (t error-format-string))
83 (setf (symbol-function 'si::universal-error-handler) ,error-handler)
86 (setq result-eval (progn ,@forms))
87 (setf (symbol-function 'si::universal-error-handler) ,error-handler)
90 (defmacro persistent-let (name vars &rest forms)
91 (labels ((get-vars (vars)
92 (cond ((null vars) nil)
93 (t (cons (if (consp (car vars)) (caar vars) (car vars))
94 (get-vars (cdr vars)))))))
96 `(let ,(cond ((functionp name)
97 (let ((old-vars (funcall name)))
98 (cond ((equal (get-vars vars) (get-vars old-vars)) old-vars)
102 ,(let ((lvars (get-vars vars)))
103 `(mapcar (lambda (x y) (list x y)) ',lvars ,(cons 'list lvars))))
108 (match-pattern dot '(0 0)))
110 (defun vectorp (vector)
111 (match-pattern vector '(0 0)))
113 (defun circlep (circle)
114 (match-pattern circle '((0 0) 0)))
116 (defun polygonp (polygon)
117 (cond ((consp polygon)
118 (and (dotp (car polygon))
119 (if (null (cdr polygon)) t (polygonp (cdr polygon)))))))
121 (defun make-dot (x y)
124 (defun make-vector (x y)
127 (defun make-line (dot1 dot2)
130 (defun make-rectangle (x1 y1 x2 y2)
131 `((,x1 ,y1) (,x2 ,y1) (,x2 ,y2) (,x1 ,y2)))
133 (defun polygon-center (polygon)
134 (apply #'mapcar #'avg polygon))
136 (defun dots-distance (dot1 dot2)
137 (destructure ((dot1 (x1 y1))
139 (sqrt (+ (expt (- x2 x1) 2)
140 (expt (- y2 y1) 2)))))
142 (defun dot-line-distance (dot line)
143 (destructure ((line ((ax ay) (bx by)))
145 (let* ((r-numerator (+ (* (- cx ax) (- bx ax)) (* (- cy ay) (- by ay))))
146 (r-denomenator (+ (expt (- bx ax) 2) (expt (- by ay) 2)))
147 (r (/ r-numerator r-denomenator)))
149 (* (abs (/ (- (* (- ay cy) (- bx ax)) (* (- ax cx) (- by ay)))
151 (sqrt r-denomenator))
154 (defun dot-segment-distance (dot segment)
156 (dist r) (dot-line-distance dot segment)
157 (cond ((and (>= r 0) (<= r 1)) dist)
158 (t (let ((dist1 (dots-distance dot (car segment)))
159 (dist2 (dots-distance dot (cadr segment))))
160 (if (< dist1 dist2) dist1 dist2))))))
162 (defun perpendicular-line (dot line)
163 (destructure ((line ((ax ay) (bx by))))
165 (dist r) (dot-line-distance dot line)
167 (make-dot (+ ax (* r (- bx ax)))
168 (+ ay (* r (- by ay))))))))
170 (defun line-angle (line)
171 (destructure ((line ((ax ay) (bx by))))
172 (let ((x (- bx ax)) (y (- by ay)))
173 (if (and (= x 0) (= y 0)) 0 (atan y x)))))
175 (defun inverse-angle (angle)
176 (cond ((< angle pi) (+ angle pi))
179 (defun translate-dot (dot dx dy)
180 (destructure ((dot (x y)))
181 (list (+ x dx) (+ y dy))))
183 (defun translate-circle (circle dx dy)
184 (destructure ((circle (center radius)))
185 (list (translate-dot center dx dy) radius)))
187 (defun translate-polygon (pol dx dy)
188 (mapcar (lambda (dot)
189 (translate-dot dot dx dy))
192 (defun polygon-edges (pol)
193 (mapcar (lambda (v1 v2) (list v1 v2))
195 (union (cdr pol) (list (car pol)))))
197 (defun polygon-dot-intersection (polygon dot)
198 ;Eric Haines algorithm
199 (let ((edges (polygon-edges
200 (translate-polygon polygon (neg (car dot)) (neg (cadr dot)))))
203 (destructure ((edge ((x1 y1) (x2 y2))))
204 (cond ((/= (signum+ y1) (signum+ y2))
205 (cond ((and (> x1 0) (> x2 0)) (incf counter))
206 ((and (or (> x1 0) (> x2 0))
207 (> (- x1 (* y1 (/ (- x2 x1) (- y2 y1)))) 0))
209 (not (evenp counter))))
211 (defun circle-segment-intersection (circle segment)
212 (destructure ((circle (center radius)))
213 (<= (dot-segment-distance center segment) radius)))
215 (defun circle-edges-intersection (circle polygon)
216 (let ((edges (polygon-edges polygon))
219 (cond ((circle-segment-intersection circle edge) (setq edges-i (cons edge edges-i)))))
222 (defun circle-polygon-intersection (circle polygon)
223 (or (polygon-dot-intersection polygon (car circle))
224 (circle-edges-intersection circle polygon)))
226 (defun circle-circle-intersection (circle1 circle2)
227 (destructure ((circle1 (center1 radius1))
228 (circle2 (center2 radius2)))
229 (<= (dots-distance center1 center2) (+ r1 r2))))