1 #ifndef GRAIL_LINEARLY_INTERPOLATED_CURVE_H
2 #define GRAIL_LINEARLY_INTERPOLATED_CURVE_H
5 #include "../../libs/vector2/vector2.h"
14 template <
typename ContextType>
28 const std::vector<Vector2>& points)
29 :
Curve<ContextType>{childEvaluator}, points(points)
39 std::vector<Vector2>&& points)
40 :
Curve<ContextType>{childEvaluator}, points(std::move(points))
44 virtual float Sample(
float argument)
const override final
46 assert(points.size() > 1);
48 if(argument < points.front().x)
50 return points.front().y;
52 else if(argument > points.back().x)
54 return points.back().y;
57 auto iter = std::find_if(points.begin(),
59 [argument](
const Vector2& point) { return point.x > argument; });
60 size_t secondPointIndex = std::distance(points.begin(), iter);
61 Vector2 firstPoint = points[secondPointIndex - 1];
62 Vector2 secondPoint = points[secondPointIndex];
64 float deltaY = secondPoint.y - firstPoint.y;
65 float deltaX = secondPoint.x - firstPoint.x;
66 return firstPoint.y + deltaY * (argument - firstPoint.x) / deltaX;
69 void SetPointY(std::size_t pointIndex,
float y) { points[pointIndex].y = y; }
75 std::vector<Vector2>&
GetPoints() {
return points; }
80 const std::vector<Vector2>&
GetPoints()
const {
return points; }
82 virtual EvaluatorType GetEvaluatorType() const override final
84 return EvaluatorType::CURVE_LINEAR_INTERPOLATED;
88 std::vector<Vector2> points{};
93 #endif // GRAIL_LINEARLY_INTERPOLATED_CURVE_H