Skip to content

Instantly share code, notes, and snippets.

@mmj-the-fighter
Last active May 17, 2024 13:14
Show Gist options
  • Save mmj-the-fighter/1dd70c9393b05a577c9f3d3e1e311a92 to your computer and use it in GitHub Desktop.
Save mmj-the-fighter/1dd70c9393b05a577c9f3d3e1e311a92 to your computer and use it in GitHub Desktop.
Linear Interpolator C++ class for graphics programming
#include "Interpolator.h"
bool Interpolator::Set(float aStartValue, float aEndValue, float aDuration)
{
bool isValidArgs = true;
startValue = aStartValue;
endValue = aEndValue;
if (aDuration <= 0) {
duration = 0;
isValidArgs = false;
}
else {
duration = aDuration;
}
deltaValue = endValue - startValue;
elapsedTime = 0;
currentValue = startValue;
return isValidArgs;
}
float Interpolator::Update(float deltaTime)
{
elapsedTime += deltaTime;
if (elapsedTime >= duration) {
elapsedTime = duration;
currentValue = endValue;
return endValue;
}
currentValue =
startValue
+ deltaValue * (elapsedTime / duration);
return currentValue;
}
float Interpolator::GetCurrentValue() const
{
return currentValue;
}
bool Interpolator::IsEndValueReached() const
{
return (elapsedTime >= duration);
}
void Interpolator::Reset()
{
elapsedTime = 0;
currentValue = startValue;
}
#ifndef _INTERPOLATOR_H_
#define _INTERPOLATOR_H_
class Interpolator
{
private:
float startValue;
float endValue;
float deltaValue;
float duration;
float elapsedTime;
float currentValue;
public:
bool Set(float aStartValue, float aEndValue, float aDuration);
float Update(float deltaTime);
float GetCurrentValue() const;
bool IsEndValueReached() const;
void Reset();
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment