1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_CUBIC_CURVE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_CUBIC_CURVE_H
18 
19 #include "core/animation/curve.h"
20 
21 namespace OHOS::Ace {
22 class NativeCurveHelper;
23 
24 // Third-order bezier curve. Formula as follows:
25 // B(m) = (1-m)^3*P0 + 3m(1-m)^2*P1 + 3m^2*P2 + m^3*P3,
26 // where P0 = (0,0), P1 = (x0_, y0_), P2 = (x1_, y1_),  P3 = (1,1)
27 // so Bx(m) = 3m(1-m)^2*x0_ + 3m^2*x1_ + m^3
28 //    By(m) = 3m(1-m)^2*y0_ + 3m^2*y1_ + m^3
29 // in function MoveInternal, assume time as Bx(m), we let Bx(m) approaching time, and we can get m and the output By(m)
30 class ACE_EXPORT CubicCurve : public Curve {
31     DECLARE_ACE_TYPE(CubicCurve, Curve);
32 
33 public:
34     CubicCurve(float x0, float y0, float x1, float y1);
35     ~CubicCurve() override = default;
36 
37     float MoveInternal(float time) override;
38     const std::string ToString() override;
39 
GetX0()40     float GetX0() const
41     {
42         return x0_;
43     }
44 
GetY0()45     float GetY0() const
46     {
47         return y0_;
48     }
49 
GetX1()50     float GetX1() const
51     {
52         return x1_;
53     }
54 
GetY1()55     float GetY1() const
56     {
57         return y1_;
58     }
59 
60     bool IsEqual(const RefPtr<Curve>& curve) const override;
61 
62 private:
63     // Bx(m) or By(m) = 3m(1-m)^2*a + 3m^2*b + m^3, where a = x0_ ,b = x1_ or a = y0_ ,b = y1_
64     static float CalculateCubic(float a, float b, float m);
65 
66     float cubicErrorBound_ = 0.001f; // Control curve accuracy
67     float x0_;                       // X-axis of the first point (P1)
68     float y0_;                       // Y-axis of the first point (P1)
69     float x1_;                       // X-axis of the second point (P2)
70     float y1_;                       // Y-axis of the second point (P2)
71 
72     friend class NativeCurveHelper;
73 };
74 
75 } // namespace OHOS::Ace
76 
77 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_CUBIC_CURVE_H
78