OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/geometry/cubic_bezier.h" | 5 #include "ui/gfx/geometry/cubic_bezier.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 return t; | 64 return t; |
65 } | 65 } |
66 | 66 |
67 } // namespace | 67 } // namespace |
68 | 68 |
69 CubicBezier::CubicBezier(double x1, double y1, double x2, double y2) | 69 CubicBezier::CubicBezier(double x1, double y1, double x2, double y2) |
70 : x1_(x1), | 70 : x1_(x1), |
71 y1_(y1), | 71 y1_(y1), |
72 x2_(x2), | 72 x2_(x2), |
73 y2_(y2) { | 73 y2_(y2) { |
| 74 InitGradients(); |
74 } | 75 } |
75 | 76 |
76 CubicBezier::~CubicBezier() { | 77 CubicBezier::~CubicBezier() { |
77 } | 78 } |
78 | 79 |
| 80 void CubicBezier::InitGradients() { |
| 81 if (x1_ > 0) |
| 82 start_gradient_ = y1_ / x1_; |
| 83 else if (!y1_ && x2_ > 0) |
| 84 start_gradient_ = y2_ / x2_; |
| 85 else |
| 86 start_gradient_ = 0; |
| 87 |
| 88 if (x2_ < 1) |
| 89 end_gradient_ = (y2_ - 1) / (x2_ - 1); |
| 90 else if (x2_ == 1 && x1_ < 1) |
| 91 end_gradient_ = (y1_ - 1) / (x1_ - 1); |
| 92 else |
| 93 end_gradient_ = 0; |
| 94 } |
| 95 |
79 double CubicBezier::Solve(double x) const { | 96 double CubicBezier::Solve(double x) const { |
| 97 if (x < 0) |
| 98 return start_gradient_ * x; |
| 99 if (x > 1) |
| 100 return 1.0 + end_gradient_ * (x - 1.0); |
| 101 |
80 return eval_bezier(y1_, y2_, bezier_interp(x1_, x2_, x)); | 102 return eval_bezier(y1_, y2_, bezier_interp(x1_, x2_, x)); |
81 } | 103 } |
82 | 104 |
83 double CubicBezier::Slope(double x) const { | 105 double CubicBezier::Slope(double x) const { |
84 double t = bezier_interp(x1_, x2_, x); | 106 double t = bezier_interp(x1_, x2_, x); |
85 double dx_dt = eval_bezier_derivative(x1_, x2_, t); | 107 double dx_dt = eval_bezier_derivative(x1_, x2_, t); |
86 double dy_dt = eval_bezier_derivative(y1_, y2_, t); | 108 double dy_dt = eval_bezier_derivative(y1_, y2_, t); |
87 return dy_dt / dx_dt; | 109 return dy_dt / dx_dt; |
88 } | 110 } |
89 | 111 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 sol_1 = eval_bezier(y1_, y2_, t_1); | 152 sol_1 = eval_bezier(y1_, y2_, t_1); |
131 | 153 |
132 if (0 < t_2 && t_2 < 1) | 154 if (0 < t_2 && t_2 < 1) |
133 sol_2 = eval_bezier(y1_, y2_, t_2); | 155 sol_2 = eval_bezier(y1_, y2_, t_2); |
134 | 156 |
135 *min = std::min(std::min(*min, sol_1), sol_2); | 157 *min = std::min(std::min(*min, sol_1), sol_2); |
136 *max = std::max(std::max(*max, sol_1), sol_2); | 158 *max = std::max(std::max(*max, sol_1), sol_2); |
137 } | 159 } |
138 | 160 |
139 } // namespace gfx | 161 } // namespace gfx |
OLD | NEW |