(C++)  1.1.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
LinearFunction.hh
1 #ifndef GRAIL_LINEAR_FUNCTION_H
2 #define GRAIL_LINEAR_FUNCTION_H
3 
4 #include "Curve.hh"
5 
6 
7 namespace grail
8 {
9  namespace curves
10  {
11  template <typename ContextType>
16  class LinearFunction final : public Curve<ContextType>
17  {
18  public:
25  LinearFunction(std::shared_ptr<utility::Evaluator<ContextType>> childEvaluator,
26  float slope, float intercept)
27  : Curve<ContextType>{childEvaluator}, slope{slope}, intercept{intercept}
28  {
29  }
30 
37  LinearFunction(std::shared_ptr<utility::Evaluator<ContextType>> childEvaluator,
38  std::pair<float, float> firstPoint, std::pair<float, float> secondPoint)
39  : Curve<ContextType>{childEvaluator}
40  {
41  assert(firstPoint.first != secondPoint.first);
42  if (firstPoint.second == secondPoint.second)
43  {
44  slope = 0.0f;
45  intercept = firstPoint.second;
46  }
47  else
48  {
49  if (firstPoint.first > secondPoint.first)
50  {
51  float x = firstPoint.first;
52  float y = firstPoint.second;
53  firstPoint.first = secondPoint.first;
54  firstPoint.second = secondPoint.second;
55  secondPoint.first = x;
56  secondPoint.second = y;
57  }
58  slope = (secondPoint.second - firstPoint.second) / (secondPoint.first - firstPoint.first);
59  intercept = firstPoint.second - (slope * firstPoint.first);
60  }
61  }
62 
63  virtual float Sample(float argument) const override final
64  {
65  return slope * argument + intercept;
66  }
67 
72  float GetSlope() const { return slope; }
77  float GetIntercept() const { return intercept; }
78 
79  EvaluatorType GetEvaluatorType() const override final { return EvaluatorType::CURVE_LINEAR; }
80 
81  private:
82  float slope = 0.0f;
83  float intercept = 0.0f;
84 
85  };
86  }
87 }
88 
89 #endif // GRAIL_LINEAR_FUNCTION_H
The Curve class - Defines objects transforming one value into the other.
Definition: Curve.hh:21
The LinearFunction class - Linear Function.
Definition: LinearFunction.hh:17
LinearFunction(std::shared_ptr< utility::Evaluator< ContextType >> childEvaluator, std::pair< float, float > firstPoint, std::pair< float, float > secondPoint)
LinearFunction - Constructor.
Definition: LinearFunction.hh:37
LinearFunction(std::shared_ptr< utility::Evaluator< ContextType >> childEvaluator, float slope, float intercept)
LinearFunction - Constructor.
Definition: LinearFunction.hh:25
float GetIntercept() const
GetIntercept.
Definition: LinearFunction.hh:77
float GetSlope() const
GetSlope.
Definition: LinearFunction.hh:72
virtual float Sample(float argument) const override final
Sample - Transforms argument into output value depending on the type of Curve.
Definition: LinearFunction.hh:63
The Evaluator class - base class being able to evaluate given context and output the result.
Definition: Evaluator.hh:21