Grail (C++)  1.1.1
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 namespace grail
7 {
8  namespace curves
9  {
10  template <typename ContextType>
15  class LinearFunction final : public Curve<ContextType>
16  {
17  public:
24  LinearFunction(std::shared_ptr<utility::Evaluator<ContextType>> childEvaluator,
25  float slope,
26  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,
39  std::pair<float, float> secondPoint)
40  : Curve<ContextType>{childEvaluator}
41  {
42  assert(firstPoint.first != secondPoint.first);
43  if(firstPoint.second == secondPoint.second)
44  {
45  slope = 0.0f;
46  intercept = firstPoint.second;
47  }
48  else
49  {
50  if(firstPoint.first > secondPoint.first)
51  {
52  float x = firstPoint.first;
53  float y = firstPoint.second;
54  firstPoint.first = secondPoint.first;
55  firstPoint.second = secondPoint.second;
56  secondPoint.first = x;
57  secondPoint.second = y;
58  }
59  slope = (secondPoint.second - firstPoint.second) / (secondPoint.first - firstPoint.first);
60  intercept = firstPoint.second - (slope * firstPoint.first);
61  }
62  }
63 
64  virtual float Sample(float argument) const override final
65  {
66  return slope * argument + intercept;
67  }
68 
73  float GetSlope() const { return slope; }
78  float GetIntercept() const { return intercept; }
79 
80  virtual EvaluatorType GetEvaluatorType() const override final { return EvaluatorType::CURVE_LINEAR; }
81 
82  private:
83  float slope = 0.0f;
84  float intercept = 0.0f;
85  };
86  }
87 }
88 
89 #endif // GRAIL_LINEAR_FUNCTION_H
grail::curves::Curve
The Curve class - Defines objects transforming one value into the other.
Definition: Curve.hh:19
grail::curves::LinearFunction::LinearFunction
LinearFunction(std::shared_ptr< utility::Evaluator< ContextType >> childEvaluator, float slope, float intercept)
LinearFunction - Constructor.
Definition: LinearFunction.hh:24
grail::curves::LinearFunction::Sample
virtual float Sample(float argument) const override final
Sample - Transforms argument into output value depending on the type of Curve.
Definition: LinearFunction.hh:64
grail::curves::LinearFunction
The LinearFunction class - Linear Function.
Definition: LinearFunction.hh:15
grail::utility::Evaluator
The Evaluator class - base class being able to evaluate given context and output the result.
Definition: Evaluator.hh:20
grail::curves::LinearFunction::LinearFunction
LinearFunction(std::shared_ptr< utility::Evaluator< ContextType >> childEvaluator, std::pair< float, float > firstPoint, std::pair< float, float > secondPoint)
LinearFunction - Constructor.
Definition: LinearFunction.hh:37
grail::curves::LinearFunction::GetSlope
float GetSlope() const
GetSlope.
Definition: LinearFunction.hh:73
grail::curves::LinearFunction::GetIntercept
float GetIntercept() const
GetIntercept.
Definition: LinearFunction.hh:78