Last active
May 17, 2024 13:14
-
-
Save mmj-the-fighter/1dd70c9393b05a577c9f3d3e1e311a92 to your computer and use it in GitHub Desktop.
Linear Interpolator C++ class for graphics programming
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
#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; | |
} |
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
#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