3 #ifndef GRAIL_LINEARLY_INTERPOLATED_CURVE_H
4 #define GRAIL_LINEARLY_INTERPOLATED_CURVE_H
7 #include "../../libs/vector2/vector2.h"
16 template <
typename ContextType>
30 const std::vector<Vector2>& points)
31 :
Curve<ContextType>{childEvaluator}, points(points)
41 std::vector<Vector2>&& points)
42 :
Curve<ContextType>{childEvaluator}, points(std::move(points))
46 virtual float Sample(
float argument)
const override final
48 assert(points.size() > 1);
50 if(argument < points.front().x)
52 return points.front().y;
54 else if(argument > points.back().x)
56 return points.back().y;
59 auto iter = std::find_if(points.begin(),
61 [argument](
const Vector2& point) { return point.x > argument; });
62 size_t secondPointIndex = std::distance(points.begin(), iter);
63 Vector2 firstPoint = points[secondPointIndex - 1];
64 Vector2 secondPoint = points[secondPointIndex];
66 float deltaY = secondPoint.y - firstPoint.y;
67 float deltaX = secondPoint.x - firstPoint.x;
68 return firstPoint.y + deltaY * (argument - firstPoint.x) / deltaX;
71 void SetPointY(std::size_t pointIndex,
float y) { points[pointIndex].y = y; }
77 std::vector<Vector2>&
GetPoints() {
return points; }
82 const std::vector<Vector2>&
GetPoints()
const {
return points; }
84 virtual data::EvaluatorType GetEvaluatorType() const override final
86 return data::EvaluatorType::CURVE_LINEAR_INTERPOLATED;
90 std::vector<Vector2> points{};
95 #endif // GRAIL_LINEARLY_INTERPOLATED_CURVE_H