-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from alexheretic/master
Add generic f64 support
- Loading branch information
Showing
18 changed files
with
523 additions
and
310 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "easer" | ||
version = "0.1.1" | ||
version = "0.2.0" | ||
authors = ["Orhan Balci <[email protected]>"] | ||
description="Tiny library imlementing Robert Penner's easing functions" | ||
documentation="http://orhanbalci.github.io/rust-easing" | ||
|
@@ -10,11 +10,13 @@ keywords=["easing","animation","tween"] | |
license="MIT" | ||
exclude=[".travis.yml"] | ||
|
||
[dependencies] | ||
num-traits="0.1" | ||
|
||
[dev-dependencies] | ||
approx="0.1.0" | ||
gnuplot="0.0.20" | ||
approx="0.1" | ||
gnuplot="0.0" | ||
|
||
[[example]] | ||
name="easer_plotter" | ||
path="examples/easer_plotter.rs" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,70 @@ | ||
use super::ease::Easing; | ||
use functions::util::*; | ||
|
||
/// This struct captures Back easing functions | ||
#[derive(Debug)] | ||
pub struct Back; | ||
|
||
impl Easing for Back { | ||
fn ease_in(t: f32, b: f32, c: f32, d: f32) -> f32 { | ||
let s = 1.70158_f32; | ||
impl<F: Float> Easing<F> for Back { | ||
fn ease_in(t: F, b: F, c: F, d: F) -> F { | ||
let s: F = f(1.70158); | ||
let t = t / d; | ||
c * t * t * ((s + 1.0) * t - s) + b | ||
c * t * t * ((s + f(1.0)) * t - s) + b | ||
} | ||
|
||
fn ease_out(t: f32, b: f32, c: f32, d: f32) -> f32 { | ||
let s = 1.70158_f32; | ||
let t = (t / d) - 1.0; | ||
c * (t * t * ((s + 1.0) * t + s) + 1.0) + b | ||
fn ease_out(t: F, b: F, c: F, d: F) -> F { | ||
let s: F = f(1.70158); | ||
let t = (t / d) - f(1.0); | ||
c * (t * t * ((s + f(1.0)) * t + s) + f(1.0)) + b | ||
} | ||
|
||
fn ease_in_out(t: f32, b: f32, c: f32, d: f32) -> f32 { | ||
let s = 1.70158_f32; | ||
let t = t / (d / 2.0); | ||
if t < 1.0 { | ||
let s = s * 1.525f32; | ||
c / 2.0 * (t * t * ((s + 1.0) * t - s)) + b | ||
fn ease_in_out(t: F, b: F, c: F, d: F) -> F { | ||
let s: F = f(1.70158); | ||
let t = t / (d / f(2.0)); | ||
if t < f(1.0) { | ||
let s = s * f(1.525); | ||
c / f(2.0) * (t * t * ((s + f(1.0)) * t - s)) + b | ||
} | ||
else { | ||
let t = t - 2.0; | ||
let s = s * 1.525f32; | ||
c / 2.0 * (t * t * ((s + 1.0) * t + s) + 2.0) + b | ||
let t = t - f(2.0); | ||
let s = s * f(1.525); | ||
c / f(2.0) * (t * t * ((s + f(1.0)) * t + s) + f(2.0)) + b | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
#[allow(unused_imports)] | ||
use functions::ease::Easing; | ||
use super::*; | ||
|
||
#[test] | ||
fn ease_in() { | ||
assert_relative_eq!(super::Back::ease_in(1.0, 2.0, 3.0, 4.0), 1.8075902); | ||
assert_relative_eq!(Back::ease_in(1.0_f32, 2.0, 3.0, 4.0), 1.8075902); | ||
} | ||
|
||
#[test] | ||
fn ease_out() { | ||
assert_relative_eq!(super::Back::ease_out(1.0, 2.0, 3.0, 4.0), 4.452229); | ||
assert_relative_eq!(Back::ease_out(1.0_f32, 2.0, 3.0, 4.0), 4.452229); | ||
} | ||
|
||
#[test] | ||
fn ease_in_out() { | ||
assert_relative_eq!(super::Back::ease_in_out(1.0, 2.0, 3.0, 4.0), 1.7009544); | ||
assert_relative_eq!(super::Back::ease_in_out(51.0, 1.0, 100.0, 100.0), 56.432546); | ||
assert_relative_eq!(Back::ease_in_out(1.0_f32, 2.0, 3.0, 4.0), 1.7009544); | ||
assert_relative_eq!(Back::ease_in_out(51.0_f32, 1.0, 100.0, 100.0), 56.432546); | ||
} | ||
|
||
const PRECISE_RESULT: f64 = 1.7458203824347307; | ||
|
||
#[test] | ||
fn f32_precision() { | ||
let ease32 = Back::ease_in(10_f32.sqrt(), 2.0, 3.0, 10.0); | ||
assert_relative_ne!(ease32 as f64, PRECISE_RESULT); // f32 maths is actually happening | ||
assert_relative_eq!(ease32, PRECISE_RESULT as f32); | ||
} | ||
|
||
#[test] | ||
fn f64_precision() { | ||
let ease64 = Back::ease_in(10_f64.sqrt(), 2.0, 3.0, 10.0); | ||
assert_relative_eq!(ease64, PRECISE_RESULT); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,78 @@ | ||
use super::ease::Easing; | ||
use functions::util::*; | ||
|
||
/// This struct captures Bounce easing functions | ||
#[derive(Debug)] | ||
pub struct Bounce; | ||
|
||
impl Easing for Bounce { | ||
fn ease_in(t: f32, b: f32, c: f32, d: f32) -> f32 { | ||
c - Bounce::ease_out(d - t, 0.0, c, d) + b | ||
impl<F: Float> Easing<F> for Bounce { | ||
fn ease_in(t: F, b: F, c: F, d: F) -> F { | ||
c - Bounce::ease_out(d - t, f(0.0), c, d) + b | ||
} | ||
|
||
fn ease_out(t: f32, b: f32, c: f32, d: f32) -> f32 { | ||
fn ease_out(t: F, b: F, c: F, d: F) -> F { | ||
cast_constants!(F; _1=1, _1_5=1.5, _2=2, _2_25=2.25, _2_5=2.5, | ||
_2_625=2.625, _7_5625=7.5625); | ||
|
||
let t = t / d; | ||
if t < 1.0 / 2.75 { | ||
c * (7.5625 * t * t) + b | ||
} else if t < 2.0 / 2.75 { | ||
let t = t - 1.5 / 2.75; | ||
c * (7.5625 * t * t + 0.75) + b | ||
} else if t < 2.5 / 2.75 { | ||
let t = t - 2.25 / 2.75; | ||
c * (7.5625 * t * t + 0.9375) + b | ||
if t < _1 / f(2.75) { | ||
c * (_7_5625 * t * t) + b | ||
} else if t < _2 / f(2.75) { | ||
let t = t - _1_5 / f(2.75); | ||
c * (_7_5625 * t * t + f(0.75)) + b | ||
} else if t < _2_5 / f(2.75) { | ||
let t = t - _2_25 / f(2.75); | ||
c * (_7_5625 * t * t + f(0.9375)) + b | ||
} else { | ||
let t = t - 2.625 / 2.75; | ||
c * (7.5625 * t * t + 0.984375) + b | ||
let t = t - _2_625 / f(2.75); | ||
c * (_7_5625 * t * t + f(0.984375)) + b | ||
} | ||
} | ||
|
||
fn ease_in_out(t: f32, b: f32, c: f32, d: f32) -> f32 { | ||
if t < (d / 2.0) { | ||
Bounce::ease_in(t * 2.0, 0.0, c, d) * 0.5 + b | ||
fn ease_in_out(t: F, b: F, c: F, d: F) -> F { | ||
if t < (d / f(2.0)) { | ||
Bounce::ease_in(t * f(2.0), f(0.0), c, d) * f(0.5) + b | ||
} else { | ||
Bounce::ease_out(t * 2.0 - d, 0.0, c, d) * 0.5 + c * 0.5 + b | ||
Bounce::ease_out(t * f(2.0) - d, f(0.0), c, d) * f(0.5) + c * f(0.5) + b | ||
} | ||
|
||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
#[allow(unused_imports)] | ||
use functions::ease::Easing; | ||
use super::*; | ||
|
||
#[test] | ||
fn ease_out() { | ||
assert_relative_eq!(super::Bounce::ease_out(1.0, 2.0, 3.0, 4.0), 3.4179688); | ||
assert_relative_eq!(super::Bounce::ease_out(1.0, 2.0, 3.0, 2.0), 4.296875); | ||
assert_relative_eq!(super::Bounce::ease_out(100.0, 1.0, 100.0, 100.0), | ||
101.000000); | ||
assert_relative_eq!(Bounce::ease_out(1.0_f32, 2.0, 3.0, 4.0), 3.4179688); | ||
assert_relative_eq!(Bounce::ease_out(1.0_f32, 2.0, 3.0, 2.0), 4.296875); | ||
assert_relative_eq!(Bounce::ease_out(100.0_f32, 1.0, 100.0, 100.0), 101.000000); | ||
} | ||
|
||
#[test] | ||
fn ease_in() { | ||
assert_relative_eq!(super::Bounce::ease_in(1.0, 2.0, 3.0, 4.0), 2.082031); | ||
assert_relative_eq!(Bounce::ease_in(1.0_f32, 2.0, 3.0, 4.0), 2.082031); | ||
} | ||
|
||
#[test] | ||
fn ease_in_out() { | ||
assert_relative_eq!(super::Bounce::ease_in_out(1.0, 2.0, 3.0, 4.0), 2.3515625); | ||
assert_relative_eq!(super::Bounce::ease_in_out(51.0, 1.0, 100.0, 100.0), | ||
51.151250); | ||
assert_relative_eq!(Bounce::ease_in_out(1.0_f32, 2.0, 3.0, 4.0), 2.3515625); | ||
assert_relative_eq!(Bounce::ease_in_out(51.0_f32, 1.0, 100.0, 100.0), 51.151250); | ||
} | ||
|
||
const PRECISE_RESULT: f64 = 2.3159476740972824; | ||
|
||
#[test] | ||
fn f32_precision() { | ||
let ease32 = Bounce::ease_in(10_f32.sqrt(), 2.0, 3.0, 10.0); | ||
assert_relative_ne!(ease32 as f64, PRECISE_RESULT); // f32 maths is actually happening | ||
assert_relative_eq!(ease32, PRECISE_RESULT as f32); | ||
} | ||
|
||
#[test] | ||
fn f64_precision() { | ||
let ease64 = Bounce::ease_in(10_f64.sqrt(), 2.0, 3.0, 10.0); | ||
assert_relative_eq!(ease64, PRECISE_RESULT); | ||
} | ||
} |
Oops, something went wrong.