]> git.jsancho.org Git - dungeon-master.git/blob - dungeon-master/geom/point.scm
d13cdf75e8e413a7b084a790d4147c70a9125e8b
[dungeon-master.git] / dungeon-master / geom / point.scm
1 ;;; Dungeon Master --- Adventure generator for GNU Guile
2 ;;; Copyright © 2019 Javier Sancho <jsf@jsancho.org>
3 ;;;
4 ;;; Dungeon Master is free software; you can redistribute it and/or modify it
5 ;;; 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 ;;; Dungeon Master is distributed in the hope that it will be useful, but
10 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 ;;; General Public License for more details.
13 ;;;
14 ;;; You should have received a copy of the GNU General Public License
15 ;;; along with Haunt.  If not, see <http://www.gnu.org/licenses/>.
16
17
18 (define-module (dungeon-master geom point)
19   #:use-module (srfi srfi-9)
20   #:export (make-point
21             point?
22             point-x
23             point-y
24             points-distance
25             sum-points
26             scale-point))
27
28 (define-record-type <point>
29   (make-point x y)
30   point?
31   (x point-x)
32   (y point-y))
33
34 (define (points-distance p1 p2)
35   (abs
36    (sqrt (+ (expt (- (point-x p1) (point-x p2)) 2)
37             (expt (- (point-y p1) (point-y p2)) 2)))))
38
39 (define (sum-points . points-to-sum)
40   (let loop ((points points-to-sum)
41              (x 0)
42              (y 0))
43     (cond ((null? points)
44            (make-point x y))
45           (else
46            (loop (cdr points)
47                  (+ x (point-x (car points)))
48                  (+ y (point-y (car points))))))))
49
50 (define (scale-point point scale)
51   (make-point
52    (* (point-x point) scale)
53    (* (point-y point) scale)))