summaryrefslogtreecommitdiff
path: root/2d
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2014-05-27 21:22:09 -0400
committerDavid Thompson <dthompson2@worcester.edu>2014-05-27 21:22:09 -0400
commitaab9846635f8a5a4671684d382aecb23102bb3bc (patch)
tree1adba0b3b86e3067ad50874c28bd53b2deb795eb /2d
parent3544245f263d93e70e645b8eb1941d6272176adc (diff)
Add scalar multiplication support to v* and remove vscale.
* 2d/vector2.scm (vscale): Delete it. (v*): Add support for scalar multiplication.
Diffstat (limited to '2d')
-rw-r--r--2d/vector2.scm15
1 files changed, 7 insertions, 8 deletions
diff --git a/2d/vector2.scm b/2d/vector2.scm
index b85757a..c6beb57 100644
--- a/2d/vector2.scm
+++ b/2d/vector2.scm
@@ -22,6 +22,7 @@
;;; Code:
(define-module (2d vector2)
+ #:use-module (ice-9 match)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-9)
#:export (<vector2>
@@ -34,7 +35,6 @@
vector2-polar
v+
v*
- vscale
vmag
vnorm
vdot
@@ -66,7 +66,8 @@
(add-vectors 0 0 vectors))
(define (v* . vectors)
- "Return the product of all VECTORS."
+ "Return the product of all VECTORS. Alternatively, a single vector
+and a scalar can be specified to perform scalar multiplication."
(define (multiply-vectors x y vectors)
(cond ((null? vectors)
(vector2 x y))
@@ -74,12 +75,10 @@
(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)))
+ (match vectors
+ ((($ <vector2> x y) (? number? k))
+ (vector2 (* x k) (* y k)))
+ (_ (multiply-vectors 1 1 vectors))))
(define (vmag v)
"Return the magnitude of the vector V."