]> git.jsancho.org Git - gacela.git/blob - gacela_misc.lisp
(no commit message)
[gacela.git] / gacela_misc.lisp
1 ;;; Gacela, a GNU Common Lisp extension for fast games development
2 ;;; Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
3 ;;;
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.
8 ;;;
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.
13 ;;;
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/>.
16
17
18 (in-package :gacela)
19
20 (defconstant INFINITY MOST-POSITIVE-LONG-FLOAT)
21
22 (defun append-if (new test tree &key (key #'first) (test-if #'equal))
23   (cond ((atom tree) tree)
24         (t (append-if-1
25             new
26             test
27             (mapcar (lambda (x) (append-if new test x :key key :test-if test-if)) tree)
28             :key key
29             :test-if test-if))))
30
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))
33         (t tree)))
34
35 (defun car+ (var)
36   (if (listp var) (car var) var))
37
38 (defun avg (&rest numbers)
39   (let ((total 0))
40     (dolist (n numbers) (incf total n))
41     (/ total (length numbers))))
42
43 (defun neg (num)
44   (* -1 num))
45
46 (defun signum+ (num)
47   (let ((sig (signum num)))
48     (cond ((= sig 0) 1)
49           (t sig))))
50
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)))
57
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)))
64                (t t)))))
65
66 ;Geometry
67 (defun dotp (dot)
68   (match-pattern dot '(0 0)))
69
70 (defun vectorp (vector)
71   (match-pattern vector '(0 0)))
72
73 (defun circlep (circle)
74   (match-pattern circle '((0 0) 0)))
75
76 (defun polygonp (polygon)
77   (cond ((consp polygon)
78          (and (dotp (car polygon))
79               (if (null (cdr polygon)) t (polygonp (cdr polygon)))))))
80
81 (defun make-dot (x y)
82   `(,x ,y))
83
84 (defun make-vector (x y)
85   `(,x ,y))
86
87 (defun make-line (dot1 dot2)
88   `(,dot1 ,dot2))
89
90 (defun make-rectangle (x1 y1 x2 y2)
91   `((,x1 ,y1) (,x2 ,y1) (,x2 ,y2) (,x1 ,y2)))
92
93 (defun polygon-center (polygon)
94   (apply #'mapcar #'avg polygon))
95
96 (defun dots-distance (dot1 dot2)
97   (destructure ((dot1 (x1 y1))
98                 (dot2 (x2 y2)))
99                (sqrt (+ (expt (- x2 x1) 2)
100                         (expt (- y2 y1) 2)))))
101
102 (defun dot-line-distance (dot line)
103   (destructure ((line ((ax ay) (bx by)))
104                 (dot (cx cy)))
105                (let* ((r-numerator (+ (* (- cx ax) (- bx ax)) (* (- cy ay) (- by ay))))
106                       (r-denomenator (+ (expt (- bx ax) 2) (expt (- by ay) 2)))
107                       (r (/ r-numerator r-denomenator)))
108                  (values
109                   (* (abs (/ (- (* (- ay cy) (- bx ax)) (* (- ax cx) (- by ay)))
110                              r-denomenator))
111                      (sqrt r-denomenator))
112                   r))))
113
114 (defun dot-segment-distance (dot segment)
115   (multiple-value-bind
116    (dist r) (dot-line-distance dot segment)
117         (cond ((and (>= r 0) (<= r 1)) dist)
118               (t (let ((dist1 (dots-distance dot (car segment)))
119                        (dist2 (dots-distance dot (cadr segment))))
120                    (if (< dist1 dist2) dist1 dist2))))))
121
122 (defun perpendicular-line (dot line)
123   (destructure ((line ((ax ay) (bx by))))
124                (multiple-value-bind
125                 (dist r) (dot-line-distance dot line)
126                 (make-line dot
127                            (make-dot (+ ax (* r (- bx ax)))
128                                      (+ ay (* r (- by ay))))))))
129
130 (defun line-angle (line)
131   (destructure ((line ((ax ay) (bx by))))
132                (let ((x (- bx ax)) (y (- by ay)))
133                  (if (and (= x 0) (= y 0)) 0 (atan y x)))))
134
135 (defun inverse-angle (angle)
136   (cond ((< angle pi) (+ angle pi))
137         (t (- angle pi))))
138
139 (defun translate-dot (dot dx dy)
140   (destructure ((dot (x y)))
141                (list (+ x dx) (+ y dy))))
142
143 (defun translate-circle (circle dx dy)
144   (destructure ((circle (center radius)))
145                (list (translate-dot center dx dy) radius)))
146
147 (defun translate-polygon (pol dx dy)
148   (mapcar (lambda (dot)
149             (translate-dot dot dx dy))
150           pol))
151
152 (defun polygon-edges (pol)
153   (mapcar (lambda (v1 v2) (list v1 v2))
154           pol
155           (union (cdr pol) (list (car pol)))))
156
157 (defun polygon-dot-intersection (polygon dot)
158 ;Eric Haines algorithm
159   (let ((edges (polygon-edges
160                 (translate-polygon polygon (neg (car dot)) (neg (cadr dot)))))
161         (counter 0))
162     (dolist (edge edges)
163       (destructure ((edge ((x1 y1) (x2 y2))))
164                    (cond ((/= (signum+ y1) (signum+ y2))
165                           (cond ((and (> x1 0) (> x2 0)) (incf counter))
166                                 ((and (or (> x1 0) (> x2 0))
167                                       (> (- x1 (* y1 (/ (- x2 x1) (- y2 y1)))) 0))
168                                  (incf counter)))))))
169     (not (evenp counter))))
170
171 (defun circle-segment-intersection (circle segment)
172   (destructure ((circle (center radius)))
173                (<= (dot-segment-distance center segment) radius)))
174
175 (defun circle-edges-intersection (circle polygon)
176   (let ((edges (polygon-edges polygon))
177         (edges-i nil))
178     (dolist (edge edges)
179       (cond ((circle-segment-intersection circle edge) (setq edges-i (cons edge edges-i)))))
180     edges-i))
181
182 (defun circle-polygon-intersection (circle polygon)
183   (or (polygon-dot-intersection polygon (car circle))
184       (circle-edges-intersection circle polygon)))
185
186 (defun circle-circle-intersection (circle1 circle2)
187   (destructure ((circle1 (center1 radius1))
188                 (circle2 (center2 radius2)))
189                (<= (dots-distance center1 center2) (+ r1 r2))))