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/>.
19 (defconstant INFINITY MOST-POSITIVE-LONG-FLOAT)
21 (defun append-if (new test tree &key (key #'first) (test-if #'equal))
22 (cond ((atom tree) tree)
26 (mapcar (lambda (x) (append-if new test x :key key :test-if test-if)) tree)
30 (defun append-if-1 (new test tree &key (key #'first) (test-if #'equal))
31 (cond ((funcall test-if (funcall key tree) test) (append tree new))
35 (if (listp var) (car var) var))
37 (defun avg (&rest numbers)
39 (dolist (n numbers) (incf total n))
40 (/ total (length numbers))))
46 (let ((sig (signum num)))
50 (defmacro destructure (destructuring-list &body body)
51 (let ((lambda-list nil) (exp-list nil))
52 (dolist (pair destructuring-list)
53 (setq exp-list (cons (car pair) exp-list))
54 (setq lambda-list (cons (cadr pair) lambda-list)))
55 `(destructuring-bind ,lambda-list ,(cons 'list exp-list) ,@body)))
57 (defun match-pattern (list pattern)
58 (cond ((and (null list) (null pattern)) t)
59 ((and (consp list) (consp pattern))
60 (and (match-pattern (car list) (car pattern)) (match-pattern (cdr list) (cdr pattern))))
61 ((and (atom list) (atom pattern))
62 (cond ((or (numberp list) (numberp pattern)) (and (numberp list) (numberp pattern)))
65 (defun nearest-power-of-two (n)
67 (cond ((> (* p 2) n) p)
68 (t (power (* p 2) n)))))
71 (defmacro secured-eval (form &optional output-stream)
72 `(let ((error-handler #'si::universal-error-handler)
74 (defun si::universal-error-handler (error-name correctable function-name continue-format-string error-format-string &rest args)
75 ,(when output-stream `(format ,output-stream error-format-string)))
76 (setq result-eval (eval ,form))
77 (setf (symbol-function 'si::universal-error-handler) error-handler)
82 (match-pattern dot '(0 0)))
84 (defun vectorp (vector)
85 (match-pattern vector '(0 0)))
87 (defun circlep (circle)
88 (match-pattern circle '((0 0) 0)))
90 (defun polygonp (polygon)
91 (cond ((consp polygon)
92 (and (dotp (car polygon))
93 (if (null (cdr polygon)) t (polygonp (cdr polygon)))))))
98 (defun make-vector (x y)
101 (defun make-line (dot1 dot2)
104 (defun make-rectangle (x1 y1 x2 y2)
105 `((,x1 ,y1) (,x2 ,y1) (,x2 ,y2) (,x1 ,y2)))
107 (defun polygon-center (polygon)
108 (apply #'mapcar #'avg polygon))
110 (defun dots-distance (dot1 dot2)
111 (destructure ((dot1 (x1 y1))
113 (sqrt (+ (expt (- x2 x1) 2)
114 (expt (- y2 y1) 2)))))
116 (defun dot-line-distance (dot line)
117 (destructure ((line ((ax ay) (bx by)))
119 (let* ((r-numerator (+ (* (- cx ax) (- bx ax)) (* (- cy ay) (- by ay))))
120 (r-denomenator (+ (expt (- bx ax) 2) (expt (- by ay) 2)))
121 (r (/ r-numerator r-denomenator)))
123 (* (abs (/ (- (* (- ay cy) (- bx ax)) (* (- ax cx) (- by ay)))
125 (sqrt r-denomenator))
128 (defun dot-segment-distance (dot segment)
130 (dist r) (dot-line-distance dot segment)
131 (cond ((and (>= r 0) (<= r 1)) dist)
132 (t (let ((dist1 (dots-distance dot (car segment)))
133 (dist2 (dots-distance dot (cadr segment))))
134 (if (< dist1 dist2) dist1 dist2))))))
136 (defun perpendicular-line (dot line)
137 (destructure ((line ((ax ay) (bx by))))
139 (dist r) (dot-line-distance dot line)
141 (make-dot (+ ax (* r (- bx ax)))
142 (+ ay (* r (- by ay))))))))
144 (defun line-angle (line)
145 (destructure ((line ((ax ay) (bx by))))
146 (let ((x (- bx ax)) (y (- by ay)))
147 (if (and (= x 0) (= y 0)) 0 (atan y x)))))
149 (defun inverse-angle (angle)
150 (cond ((< angle pi) (+ angle pi))
153 (defun translate-dot (dot dx dy)
154 (destructure ((dot (x y)))
155 (list (+ x dx) (+ y dy))))
157 (defun translate-circle (circle dx dy)
158 (destructure ((circle (center radius)))
159 (list (translate-dot center dx dy) radius)))
161 (defun translate-polygon (pol dx dy)
162 (mapcar (lambda (dot)
163 (translate-dot dot dx dy))
166 (defun polygon-edges (pol)
167 (mapcar (lambda (v1 v2) (list v1 v2))
169 (union (cdr pol) (list (car pol)))))
171 (defun polygon-dot-intersection (polygon dot)
172 ;Eric Haines algorithm
173 (let ((edges (polygon-edges
174 (translate-polygon polygon (neg (car dot)) (neg (cadr dot)))))
177 (destructure ((edge ((x1 y1) (x2 y2))))
178 (cond ((/= (signum+ y1) (signum+ y2))
179 (cond ((and (> x1 0) (> x2 0)) (incf counter))
180 ((and (or (> x1 0) (> x2 0))
181 (> (- x1 (* y1 (/ (- x2 x1) (- y2 y1)))) 0))
183 (not (evenp counter))))
185 (defun circle-segment-intersection (circle segment)
186 (destructure ((circle (center radius)))
187 (<= (dot-segment-distance center segment) radius)))
189 (defun circle-edges-intersection (circle polygon)
190 (let ((edges (polygon-edges polygon))
193 (cond ((circle-segment-intersection circle edge) (setq edges-i (cons edge edges-i)))))
196 (defun circle-polygon-intersection (circle polygon)
197 (or (polygon-dot-intersection polygon (car circle))
198 (circle-edges-intersection circle polygon)))
200 (defun circle-circle-intersection (circle1 circle2)
201 (destructure ((circle1 (center1 radius1))
202 (circle2 (center2 radius2)))
203 (<= (dots-distance center1 center2) (+ r1 r2))))