Skip to content

Commit

Permalink
Add polynomial easings
Browse files Browse the repository at this point in the history
  • Loading branch information
jackfirth committed Aug 25, 2015
1 parent 5bf05ff commit c74e9ab
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions ease/main.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ source code: @url["https://github.com/jackfirth/racket-ease"]

@scribble-include/no-subsection["private/base.scrbl"]
@scribble-include/no-subsection["private/invert.scrbl"]
@scribble-include/no-subsection["private/polynomial.scrbl"]
2 changes: 2 additions & 0 deletions ease/private/main.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
require
"base.rkt"
"invert.rkt"
"polynomial.rkt"


provide
all-from-out
"base.rkt"
"invert.rkt"
"polynomial.rkt"
42 changes: 42 additions & 0 deletions ease/private/polynomial.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#lang sweet-exp racket/base

require racket/contract/base

provide
contract-out
polynomial-ease (-> (>/c 0) proper-ease/c)
symmetric-polynomial-ease (-> (>/c 0) proper-ease/c)

require "base.rkt"

module+ test
require racket/list
rackunit
"invert.rkt"


(define ((polynomial-ease degree) x)
(expt x degree))


module+ test
(define (quadratic-ease x) (* x x))
(check-equal? (ease-real (polynomial-ease 1) 0 100 5)
(ease-real linear-ease 0 100 5))
(check-equal? (ease-real (polynomial-ease 2) 0 100 5)
(ease-real quadratic-ease 0 100 5))


(define ((symmetric-polynomial-ease degree) x)
(define x^d (expt x degree))
(/ x^d (+ x^d (expt (- 1 x) degree))))

module+ test
(check-equal? (ease-real (symmetric-polynomial-ease 1) 0 100 5)
(ease-real (ease-invert (symmetric-polynomial-ease 1)) 0 100 5))
(check-true (< (second (ease-real (symmetric-polynomial-ease 2) 0 100 5))
(second (ease-real (symmetric-polynomial-ease 1) 0 100 5))))
(check-true (> (fourth (ease-real (symmetric-polynomial-ease 2) 0 100 5))
(fourth (ease-real (symmetric-polynomial-ease 1) 0 100 5))))


25 changes: 25 additions & 0 deletions ease/private/polynomial.scrbl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#lang scribble/manual

@(require "util/doc.rkt")


@defproc[(polynomial-ease [degree (>/c 0)]) proper-ease/c]{
Returns an easing function that starts out proportionally slow,
then speeds up. The slope of this acceleration is determined by
@racket[degree]. When graphed, the resulting easing is a polynomial
curve of the given degree.
@ease-examples[
(ease-real (polynomial-ease 1) 0 1000 5)
(ease-real (polynomial-ease 2) 0 1000 5)
(ease-real (polynomial-ease 3) 0 1000 5)
]}

@defproc[(symmetric-polynomial-ease [degree (>/c 0)]) proper-ease/c]{
Like @racket[polynomial-ease], but the returned easing is symmetric.
It starts out slow, speeds up towards the middle, then slows down
as it approaches the end.
@ease-examples[
(map exact->inexact (ease-real (symmetric-polynomial-ease 1) 0 1000 4))
(map exact->inexact (ease-real (symmetric-polynomial-ease 2) 0 1000 4))
(map exact->inexact (ease-real (symmetric-polynomial-ease 5) 0 1000 4))
]}

0 comments on commit c74e9ab

Please sign in to comment.