diff options
author | David Thompson <dthompson2@worcester.edu> | 2014-05-27 21:22:09 -0400 |
---|---|---|
committer | David Thompson <dthompson2@worcester.edu> | 2014-05-27 21:22:09 -0400 |
commit | aab9846635f8a5a4671684d382aecb23102bb3bc (patch) | |
tree | 1adba0b3b86e3067ad50874c28bd53b2deb795eb | |
parent | 3544245f263d93e70e645b8eb1941d6272176adc (diff) |
Add scalar multiplication support to v* and remove vscale.
* 2d/vector2.scm (vscale): Delete it.
(v*): Add support for scalar multiplication.
-rw-r--r-- | 2d/vector2.scm | 15 |
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." |