aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/math/ranged_value.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-01-12 21:48:33 +0800
committerchai <chaifix@163.com>2019-01-12 21:48:33 +0800
commit8b00d67febf133e89f6a0bfabc41feed555dc4a9 (patch)
treefe48ef17c250afa40c2588300fcdb5920dba6951 /src/libjin/math/ranged_value.cpp
parenta907c39756ef6b368d06643afa491c49a9044a8e (diff)
*去掉文件前缀je_
Diffstat (limited to 'src/libjin/math/ranged_value.cpp')
-rw-r--r--src/libjin/math/ranged_value.cpp71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/libjin/math/ranged_value.cpp b/src/libjin/math/ranged_value.cpp
new file mode 100644
index 0000000..1a14467
--- /dev/null
+++ b/src/libjin/math/ranged_value.cpp
@@ -0,0 +1,71 @@
+#include "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