From ae21c9cc5a72b3fe7a3207a82c8a42d464cd4a15 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Sun, 19 Oct 2014 15:21:10 -0400 Subject: quaternion: Make better use of pattern matching. * sly/quaternion.scm (axis-angle->quaternion, quaternion->vector, vector->quaternion): Use match-lambda and record destructuring. --- sly/quaternion.scm | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/sly/quaternion.scm b/sly/quaternion.scm index 663b0dd..8f34602 100644 --- a/sly/quaternion.scm +++ b/sly/quaternion.scm @@ -94,23 +94,21 @@ Q2 and blending factor DELTA." (define (axis-angle->quaternion axis theta) "Convert the rotation of THETA radians about AXIS to a quaternion. AXIS must be a 3D vector." - (let* ((cos (cos (/ theta 2))) - (sin (sin (/ theta 2)))) - (match axis - ((? vector3? v) - (make-quaternion cos (* (vx v) sin) (* (vy v) sin) (* (vz v) sin)))))) + (match axis + (($ x y z) + (let* ((theta/2 (/ theta 2)) + (sin (sin theta/2))) + (make-quaternion (cos theta/2) (* x sin) (* y sin) (* z sin)))))) -(define (quaternion->vector q) - "Convert the quaternion Q into a 4D vector." - (match q +(define quaternion->vector + (match-lambda (($ w x y z) (vector4 w x y z)))) -(define (vector->quaternion v) - "Convert the 4D vector V into a quaternion." - (match v - ((? vector4? v) - (make-quaternion (vx v) (vy v) (vz v) (vw v))))) +(define vector->quaternion + (match-lambda + (($ x y z w) + (make-quaternion x y z w)))) (define rotate (match-lambda* -- cgit v1.2.3