Joshua Moerman
14 years ago
4 changed files with 173 additions and 5 deletions
@ -0,0 +1,18 @@ |
|||
//
|
|||
// array.h
|
|||
// J
|
|||
//
|
|||
// Created by Joshua Moerman on 9/2/11.
|
|||
// Copyright 2011 Vadovas. All rights reserved.
|
|||
//
|
|||
|
|||
#ifndef J_array_h |
|||
#define J_array_h |
|||
|
|||
#include <tr1/array> |
|||
|
|||
namespace std { |
|||
using tr1::array; |
|||
} |
|||
|
|||
#endif |
@ -0,0 +1,119 @@ |
|||
//
|
|||
// interpolator.h
|
|||
// J
|
|||
//
|
|||
// Created by Joshua Moerman on 9/2/11.
|
|||
// Copyright 2011 Vadovas. All rights reserved.
|
|||
//
|
|||
|
|||
#ifndef J_interpolator_h |
|||
#define J_interpolator_h |
|||
|
|||
#include <tr1/functional> |
|||
|
|||
namespace J { |
|||
|
|||
namespace interpolators { |
|||
struct linear{ |
|||
template <typename T> |
|||
T operator()(T x){ |
|||
return x; |
|||
} |
|||
}; |
|||
|
|||
struct cubic_in_out{ |
|||
template <typename T> |
|||
T operator()(T x){ |
|||
return 3*x*x - 2*x*x*x; |
|||
} |
|||
}; |
|||
|
|||
struct quintic_in_out{ |
|||
template <typename T> |
|||
T operator()(T x){ |
|||
return 6*x*x*x*x*x - 15*x*x*x*x + 10*x*x*x; |
|||
return x*x*x*(x*(x*6-15)+10); |
|||
} |
|||
}; |
|||
|
|||
struct cosine_in_out{ |
|||
template <typename T> |
|||
T operator()(T x){ |
|||
return 0.5f - 0.5f*std::cos(x*M_PI); |
|||
} |
|||
}; |
|||
} |
|||
|
|||
template <typename T> |
|||
class interpolator { |
|||
|
|||
std::tr1::function<T (T)> ease_function; |
|||
|
|||
size_t length; |
|||
size_t steps; |
|||
|
|||
T value; |
|||
T begin_value; |
|||
T end_value; |
|||
|
|||
public: |
|||
|
|||
interpolator(T begin_value_ = T(0), size_t length_ = 100) : |
|||
ease_function(interpolators::cubic_in_out()) |
|||
, length(length_) |
|||
, steps(0) |
|||
, value(begin_value_) |
|||
, begin_value(begin_value_) |
|||
, end_value(begin_value_) {} |
|||
|
|||
template <typename F> |
|||
interpolator(T begin_value_, size_t length_, F ease_function_) : |
|||
ease_function(ease_function_) |
|||
, length(length_) |
|||
, steps(0) |
|||
, value(begin_value_) |
|||
, begin_value(begin_value_) |
|||
, end_value(begin_value_) {} |
|||
|
|||
operator T&(){ |
|||
return value; |
|||
} |
|||
|
|||
operator T const () const{ |
|||
return value; |
|||
} |
|||
|
|||
void set_value(T new_value){ |
|||
begin_value = value; |
|||
end_value = new_value; |
|||
steps = 0; |
|||
} |
|||
|
|||
void set_length(size_t new_length){ |
|||
T ratio = (T) steps / (T) length; |
|||
length = new_length; |
|||
steps = ratio*length; |
|||
} |
|||
|
|||
template <typename F> |
|||
void set_ease_function(F new_ease_function){ |
|||
ease_function = new_ease_function; |
|||
} |
|||
|
|||
void interpolate(){ |
|||
if(steps >= length) return; |
|||
++steps; |
|||
T ratio = (T) steps / (T) length; |
|||
ratio = ease_function(ratio); |
|||
value = (T(1) - ratio)*begin_value + ratio*end_value; |
|||
} |
|||
|
|||
}; |
|||
|
|||
struct foo{ |
|||
foo(int){} |
|||
}; |
|||
|
|||
} // namespace J
|
|||
|
|||
#endif |
Reference in new issue