diff options
Diffstat (limited to 'src/libjin/math')
-rw-r--r-- | src/libjin/math/je_percentage.h | 36 | ||||
-rw-r--r-- | src/libjin/math/je_ranged_value.cpp | 71 | ||||
-rw-r--r-- | src/libjin/math/je_ranged_value.h | 57 | ||||
-rw-r--r-- | src/libjin/math/je_transform.h | 2 | ||||
-rw-r--r-- | src/libjin/math/je_vector2.hpp | 5 |
5 files changed, 170 insertions, 1 deletions
diff --git a/src/libjin/math/je_percentage.h b/src/libjin/math/je_percentage.h new file mode 100644 index 0000000..2440072 --- /dev/null +++ b/src/libjin/math/je_percentage.h @@ -0,0 +1,36 @@ +#ifndef __JE_PERCENTAGE_H__ +#define __JE_PERCENTAGE_H__ + +#include "je_math.h" + +namespace JinEngine +{ + namespace Math + { + + class Percentage + { + public: + Percentage(float v) + { + value = clamp<float>(v, 0, 1); + } + + Percentage(const Percentage& p) + { + value = p.value; + } + + void operator = (const Percentage& p) + { + value = p.value; + } + + float value; + + }; + + } +} + +#endif
\ No newline at end of file diff --git a/src/libjin/math/je_ranged_value.cpp b/src/libjin/math/je_ranged_value.cpp new file mode 100644 index 0000000..b28e72b --- /dev/null +++ b/src/libjin/math/je_ranged_value.cpp @@ -0,0 +1,71 @@ +#include "je_ranged_value.h" + +namespace JinEngine +{ + namespace Math + { + + RangedValue::RangedValue() + : mCount(0) + { + } + + RangedValue::RangedValue(float* points, uint n) + : mCount(n) + { + for (uint i = 0; i < n; ++i) + { + float x = points[2*i]; + float y = points[2*i + 1]; + mXAxis.push_back(x); + mYAxis.push_back(y); + } + } + + void RangedValue::addPoint(float x, float y) + { + mXAxis.push_back(x); + mYAxis.push_back(y); + ++mCount; + } + + void RangedValue::removePoint(uint i) + { + mXAxis.erase(mXAxis.begin() + i); + mYAxis.erase(mYAxis.begin() + i); + --mCount; + } + + void RangedValue::insertPoint(uint i, float x, float y) + { + mXAxis.insert(mXAxis.begin() + i, x); + mYAxis.insert(mYAxis.begin() + i, y); + ++mCount; + } + + float RangedValue::getValue(float x) + { + int endIndex = -1; + int n = mCount; + for (int i = 1; i < n; i++) { + float t = mXAxis[i]; + if (t > x) { + endIndex = i; + break; + } + } + if (endIndex == -1) return mYAxis[n - 1]; + int startIndex = endIndex - 1; + float startValue = mYAxis[startIndex]; + float startX = mXAxis[startIndex]; + return startValue + (mYAxis[endIndex] - startValue) * ((x - startX) / (mXAxis[endIndex] - startX)); + } + + void RangedValue::clear() + { + mXAxis.clear(); + mYAxis.clear(); + } + + } +}
\ No newline at end of file diff --git a/src/libjin/math/je_ranged_value.h b/src/libjin/math/je_ranged_value.h new file mode 100644 index 0000000..4749e91 --- /dev/null +++ b/src/libjin/math/je_ranged_value.h @@ -0,0 +1,57 @@ +#ifndef __JE_RANGED_VALUE_H__ +#define __JE_RANGED_VALUE_H__ + +#include "../common/je_types.h" + +#include <vector> + +namespace JinEngine +{ + namespace Math + { + /* + * y ^ + * |-------- _ + * | \ / \ + * | --- \ + * | \ + * | \ + * +----------------------> x + */ + class RangedValue + { + public: + RangedValue(); + + /// + /// Points of ranged value. + /// + /// @param points Points. + /// @param n Number of points. + /// + RangedValue(float* points, uint n); + + virtual ~RangedValue() {} + + virtual void addPoint(float x, float y); + + virtual void removePoint(uint i); + + virtual void insertPoint(uint i, float x, float y); + + virtual float getValue(float x); + + virtual void clear(); + + private: + std::vector<float> mXAxis; + std::vector<float> mYAxis; + + uint mCount; + + }; + + } +} + +#endif
\ No newline at end of file diff --git a/src/libjin/math/je_transform.h b/src/libjin/math/je_transform.h index 45f26e0..35a1a13 100644 --- a/src/libjin/math/je_transform.h +++ b/src/libjin/math/je_transform.h @@ -40,7 +40,7 @@ namespace JinEngine Vector2<float> mPosition; Vector2<float> mOrigin; Vector2<float> mScale; - float mRotation; + float mRotation; }; diff --git a/src/libjin/math/je_vector2.hpp b/src/libjin/math/je_vector2.hpp index b4cab44..0a1a1e8 100644 --- a/src/libjin/math/je_vector2.hpp +++ b/src/libjin/math/je_vector2.hpp @@ -48,6 +48,11 @@ namespace JinEngine data[1] = _y; } + bool isZero() + { + return data[0] == 0 && data[1] == 0; + } + T &x = data[0], &y = data[1]; // xy T &w = data[0], &h = data[1]; // wh T &colum = data[0], &row = data[1]; // colum row |