Skip to content
Browse files

All tests in, minor fixes made

  • Loading branch information...
1 parent 1004445 commit 5d812b1281a2974c4ae8e10b70a9f1055c6a48fe @dleslie committed Jan 7, 2013
Showing with 45 additions and 19 deletions.
  1. +2 −0 .gitignore
  2. +1 −1 easing.scm
  3. +19 −18 selectors.scm
  4. +22 −0 test.scm
  5. +1 −0 tween.import.scm
View
2 .gitignore
@@ -0,0 +1,2 @@
+.so
+.import.scm
View
2 easing.scm
@@ -11,7 +11,7 @@
(expt percent 2))
((eq? direction 'out)
(* percent (- 2 percent)))
- ((eq? direction 'inout)u
+ ((eq? direction 'inout)
(let ((p (* percent 2)))
(if (< p 1)
(* 0.5 (expt percent 2))
View
37 selectors.scm
@@ -1,33 +1,34 @@
(define (linear-select v p)
+ (assert (and (>= p 0) (<= p 1)
+ (vector? v)))
(define (fn p0 p1 t)
(+ (* (- p1 p0) t) p0))
(define m (- (vector-length v) 1))
(define f (* m p))
- (define idx (floor f))
- (case
- ((< p 0) (fn (vector-ref v 0)
- (vector-ref v 1)
- f))
- ((> p 1) (fn (vector-ref v m)
- (vector-ref v (- m 1))
- (- m f)))
- (else (fn (vector-ref v idx)
- (vector-ref v (if (> (+ idx 1) m) m (+ idx 1)))
- (- f idx)))))
+ (define idx (inexact->exact (floor f)))
+ (fn (vector-ref v idx)
+ (vector-ref v (if (> (+ idx 1) m) m (+ idx 1)))
+ (- f idx)))
(define (bezier-select v p)
+ (assert (and (>= p 0) (<= p 1)
+ (vector? v)))
(define (fn n i)
(/ (factorial n) (* (factorial i) (factorial (- n i)))))
- (define n (vector-length v))
+ (define n (- (vector-length v) 1))
(define (loop i b)
- (if (> i n) b
+ (if (> i n)
+ b
(loop (+ i 1)
(+ b (* (expt (- 1 p) (- n i))
+ (expt p i)
(vector-ref v i)
(fn n i))))))
(loop 0 0))
(define (catmullrom-select v p)
+ (assert (and (>= p 0) (<= p 1)
+ (vector? v)))
(define (fn p0 p1 p2 p3 t)
(define v0 (* 0.5 (- p2 p0)))
(define v1 (* 0.5 (- p3 p1)))
@@ -37,7 +38,7 @@
(* v0 t)
(* (- v1 (* 2 v0) (* -3 p2) (* 3 p1)) t2)
(* (+ (* 2 p1) (* -2 p2) v0 v1) t3)))
- (define m (- (vector-length v) 1))
+ (define m (- (vector-length v) 1))
(define f (* m p))
(define i (floor f))
(if
@@ -65,8 +66,8 @@
(vector-ref v (- m 1))
(- f m))
(vector-ref v m)))
- (else (fn (vector-ref v (if (< 0 i) (- i 1) 0))
- (vector-ref v i)
- (vector-ref v (if (< m (+ i 1)) m (+ i 1)))
- (vector-ref v (if (< m (+ i 2)) m (+ i 2)))
+ (else (fn (vector-ref v (inexact->exact (if (< 0 i) (- i 1) 0)))
+ (vector-ref v (inexact->exact i))
+ (vector-ref v (inexact->exact (if (< m (+ i 1)) m (+ i 1))))
+ (vector-ref v (inexact->exact (if (< m (+ i 2)) m (+ i 2))))
(- f i))))))
View
22 test.scm
@@ -156,3 +156,25 @@
(assert (= (tween bounce-ease 'inout 0 10 0) 0))
(assert (= (rationalize 4 (tween bounce-ease 'inout 0 10 1) 0.00001) 10))
(assert (= (rationalize 4 (tween bounce-ease 'inout 0 10 0.5) 0.00001) 5.0))
+
+;; Selectors
+
+(define test-set (list->vector '(0 1 2 3 4 5 6 7 8 9 10)))
+
+;; Linear select
+
+(assert (= (linear-select test-set 0) 0))
+(assert (= (linear-select test-set 0.5) 5.0))
+(assert (= (linear-select test-set 1) 10.0))
+
+;; Bezier select
+
+(assert (= (bezier-select test-set 0) 0))
+(assert (= (bezier-select test-set 0.5) 5.0))
+(assert (= (bezier-select test-set 1) 10.0))
+
+;; Catmullrom select
+
+(assert (= (catmullrom-select test-set 0) 0))
+(assert (= (catmullrom-select test-set 0.5) 5.0))
+(assert (= (catmullrom-select test-set 1) 10.0))
View
1 tween.import.scm
@@ -20,6 +20,7 @@
(linear-select . tween#linear-select)
(interpolate . tween#interpolate)
(tween . tween#tween)
+ (rationalize . tween#rationalize)
(factorial . tween#factorial)
(factorial* . tween#factorial*))
(list)

0 comments on commit 5d812b1

Please sign in to comment.
Something went wrong with that request. Please try again.