summaryrefslogtreecommitdiff
path: root/2d/vector2.scm
blob: bbbe19f7ec788d8afdfeaad1f0a29e2d9c5bdedf (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
;;; guile-2d
;;; Copyright (C) 2013, 2014 David Thompson <dthompson2@worcester.edu>
;;;
;;; This program is free software: you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License as
;;; published by the Free Software Foundation, either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; This program 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
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; 2D vector math operations.
;;
;;; Code:

(define-module (2d vector2)
  #:use-module (figl gl)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-9)
  #:export (<vector2>
            vector2
            vector2?
            vx
            vy
            null-vector2
            identity-vector2
            vector2-polar
            v+
            v*
            vscale
            vmag
            vnorm
            vdot
            vcross
            vector2-translate
            vector2-scale))

(define-record-type <vector2>
  (vector2 x y)
  vector2?
  (x vx)
  (y vy))

(define null-vector2 (vector2 0 0))
(define identity-vector2 (vector2 1 1))

(define (vector2-polar r theta)
  "Convert the polar coordinates (R, THETA) into a cartesian vector."
  (vector2 (* r (cos theta))
           (* r (sin theta))))

(define (v+ . vectors)
  "Return the sum of all VECTORS."
  (define (add-vectors x y vectors)
    (cond ((null? vectors)
           (vector2 x y))
          (else
           (add-vectors (+ x (vx (car vectors)))
                        (+ y (vy (car vectors)))
                        (cdr vectors)))))
  (add-vectors 0 0 vectors))

(define (v* . vectors)
  "Return the product of all VECTORS."
  (define (multiply-vectors x y vectors)
    (cond ((null? vectors)
           (vector2 x y))
          (else
           (multiply-vectors (* x (vx (car vectors)))
                             (* y (vy (car vectors)))
                             (cdr vectors)))))
  (multiply-vectors 1 1 vectors))

(define (vscale v scalar)
  "Multiply the vector V by a scalar value."
  (vector2 (* (vx v) scalar)
           (* (vy v) scalar)))

(define (vmag v)
  "Return the magnitude of the vector V."
  (sqrt (+ (expt (vx v) 2)
           (expt (vy v) 2))))

(define (vnorm v)
  "Normalize the vector V."
  (let ((m (vmag v)))
    (if (zero? m)
        null-vector2
        (vector2 (/ (vx v) m)
                 (/ (vy v) m)))))

(define (vdot v1 v2)
  "Return the dot product of the vectors V1 and V2."
  (+ (* (vx v1) (vx v2))
     (* (vy v1) (vy v2))))

(define (vcross v1 v2)
  "Return the cross product of the vectors V1 and V2. Technically, the
cross product of a 2D vector is not defined. This function instead
returns the Z coordinate of the cross product as if the vectors were
in 3D space."
  (- (* (vx v1) (vy v2))
     (* (vy v1) (vx v2))))

(define (vector2-translate v)
  "Perform an OpenGL translate operation with the vector V."
  (gl-translate (vx v) (vy v) 0))

(define (vector2-scale v)
  "Perform an OpenGL scale operation with the vector V."
  (gl-scale (vx v) (vy v) 1))