blob: d231f93fc77b10b87b11fcb1084904bbe3e4098f (
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 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:
;;
;; 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)
"Converts polar coordinates into a cartesian vector."
(vector2 (* r (cos theta))
(* r (sin theta))))
(define (v+ . vectors)
"Adds 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)
"Multiplies 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 vector by a scalar."
(vector2 (* (vx v) scalar)
(* (vy v) scalar)))
(define (vmag v)
"Returns the magnitude of a vector"
(sqrt (+ (expt (vx v) 2)
(expt (vy v) 2))))
(define (vnorm v)
"Normalizes a vector."
(let ((m (vmag v)))
(if (zero? m)
null-vector2
(vector2 (/ (vx v) m)
(/ (vy v) m)))))
(define (vdot v1 v2)
"Returns the dot product of two vectors."
(+ (* (vx v1) (vx v2))
(* (vy v1) (vy v2))))
(define (vcross v1 v2)
"Returns the cross product of two vectors. 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 matrix operation with the given
vector2."
(gl-translate (vx v) (vy v) 0))
(define (vector2-scale v)
"Perform an OpenGL scale matrix operation with the given vector2."
(gl-scale (vx v) (vy v) 1))
|