summaryrefslogtreecommitdiff
path: root/2d/vector.scm
blob: 7dcbcb28d5dcfa8bdb54f60ea776b97b3727be22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
;;; guile-2d
;;; Copyright (C) 2013 David Thompson <dthompson2@worcester.edu>
;;;
;;; Guile-2d is free software: you can redistribute it and/or modify it
;;; under the terms of the GNU Lesser General Public License as
;;; published by the Free Software Foundation, either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; Guile-2d is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; Lesser General Public License for more details.
;;;
;;; You should have received a copy of the GNU Lesser General Public
;;; License along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; Vector math operations.
;;
;;; Code:

(define-module (2d vector)
  #:use-module (rnrs base)
  #:export (vx
            vy
            vz
            v+
            v*
            scale
            mag
            normalize
            vector-reduce))

(define (vx vector)
  "Returns the first element of a vector."
  (vector-ref vector 0))

(define (vy vector)
  "Returns the second element of a vector."
  (vector-ref vector 1))

(define (vz vector)
  "Returns the third element of a vector."
  (vector-ref vector 2))

(define (v+ . vectors)
  "Adds vectors."
  (apply vector-map + vectors))

(define (v* . vectors)
  "Multiplies vectors."
  (apply vector-map * vectors))

(define (scale vector scalar)
  "Multiplies a vector by a scalar."
  (vector-map (lambda (e) (* scalar e)) vector))

(define (mag vector)
  "Returns the magnitude of a vector."
  (sqrt (vector-reduce + 0 (vector-map (lambda (e) (* e e)) vector))))

(define (normalize vector)
  "Normalizes a vector."
  (let ((m (mag vector)))
    (vector-map (lambda (e) (/ e m)) vector)))

(define (vector-reduce proc default vector)
  "Performs a reduce operation on a vector."
  (cond ((= (vector-length vector) 0)
         default)
        ((= (vector-length vector) 1)
         (vector-ref vector 0))
        (else
         (let loop ((i 2)
                    (prev (proc (vector-ref vector 0)
                                (vector-ref vector 1))))
           (if (= (vector-length vector) i)
               prev
               (loop (1+ i) (proc prev (vector-ref vector i))))))))