• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef UI_GFX_GEOMETRY_CUBIC_BEZIER_H_
6 #define UI_GFX_GEOMETRY_CUBIC_BEZIER_H_
7 
8 #include "base/macros.h"
9 #include "ui/gfx/gfx_export.h"
10 
11 namespace gfx {
12 
13 class GFX_EXPORT CubicBezier {
14  public:
15   CubicBezier(double p1x, double p1y, double p2x, double p2y);
16   CubicBezier(const CubicBezier& other);
17 
SampleCurveX(double t)18   double SampleCurveX(double t) const {
19     // `ax t^3 + bx t^2 + cx t' expanded using Horner's rule.
20     return ((ax_ * t + bx_) * t + cx_) * t;
21   }
22 
SampleCurveY(double t)23   double SampleCurveY(double t) const {
24     return ((ay_ * t + by_) * t + cy_) * t;
25   }
26 
SampleCurveDerivativeX(double t)27   double SampleCurveDerivativeX(double t) const {
28     return (3.0 * ax_ * t + 2.0 * bx_) * t + cx_;
29   }
30 
SampleCurveDerivativeY(double t)31   double SampleCurveDerivativeY(double t) const {
32     return (3.0 * ay_ * t + 2.0 * by_) * t + cy_;
33   }
34 
35   static double GetDefaultEpsilon();
36 
37   // Given an x value, find a parametric value it came from.
38   // x must be in [0, 1] range. Doesn't use gradients.
39   double SolveCurveX(double x, double epsilon) const;
40 
41   // Evaluates y at the given x with default epsilon.
42   double Solve(double x) const;
43   // Evaluates y at the given x. The epsilon parameter provides a hint as to the
44   // required accuracy and is not guaranteed. Uses gradients if x is
45   // out of [0, 1] range.
SolveWithEpsilon(double x,double epsilon)46   double SolveWithEpsilon(double x, double epsilon) const {
47     if (x < 0.0)
48       return 0.0 + start_gradient_ * x;
49     if (x > 1.0)
50       return 1.0 + end_gradient_ * (x - 1.0);
51     return SampleCurveY(SolveCurveX(x, epsilon));
52   }
53 
54   // Returns an approximation of dy/dx at the given x with default epsilon.
55   double Slope(double x) const;
56   // Returns an approximation of dy/dx at the given x.
57   // Clamps x to range [0, 1].
58   double SlopeWithEpsilon(double x, double epsilon) const;
59 
60   // These getters are used rarely. We reverse compute them from coefficients.
61   // See CubicBezier::InitCoefficients. The speed has been traded for memory.
62   double GetX1() const;
63   double GetY1() const;
64   double GetX2() const;
65   double GetY2() const;
66 
67   // Gets the bezier's minimum y value in the interval [0, 1].
range_min()68   double range_min() const { return range_min_; }
69   // Gets the bezier's maximum y value in the interval [0, 1].
range_max()70   double range_max() const { return range_max_; }
71 
72  private:
73   void InitCoefficients(double p1x, double p1y, double p2x, double p2y);
74   void InitGradients(double p1x, double p1y, double p2x, double p2y);
75   void InitRange(double p1y, double p2y);
76 
77   double ax_;
78   double bx_;
79   double cx_;
80 
81   double ay_;
82   double by_;
83   double cy_;
84 
85   double start_gradient_;
86   double end_gradient_;
87 
88   double range_min_;
89   double range_max_;
90 
91   DISALLOW_ASSIGN(CubicBezier);
92 };
93 
94 }  // namespace gfx
95 
96 #endif  // UI_GFX_GEOMETRY_CUBIC_BEZIER_H_
97