1 /*
2  * Copyright (c) 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_BASE_GEOMETRY_NG_OFFSET_T_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_NG_OFFSET_T_H
18 
19 #include <cstdint>
20 #include <iomanip>
21 #include <sstream>
22 #include <string>
23 
24 #include "base/geometry/axis.h"
25 #include "base/utils/utils.h"
26 
27 namespace OHOS::Ace::NG {
28 template<typename T>
29 class OffsetT {
30 public:
31     OffsetT() = default;
32     ~OffsetT() = default;
OffsetT(T x,T y)33     OffsetT(T x, T y) : x_(x), y_(y) {}
34 
OffsetT(T crossOffset,T mainOffset,Axis axis)35     OffsetT(T crossOffset, T mainOffset, Axis axis)
36     {
37         if (axis == Axis::HORIZONTAL) {
38             x_ = mainOffset;
39             y_ = crossOffset;
40         } else { // Axis::VERTICAL and others
41             x_ = crossOffset;
42             y_ = mainOffset;
43         }
44     }
45 
Reset()46     void Reset()
47     {
48         x_ = 0;
49         y_ = 0;
50     }
51 
GetX()52     T GetX() const
53     {
54         return x_;
55     }
56 
GetY()57     T GetY() const
58     {
59         return y_;
60     }
61 
GetMainOffset(Axis axis)62     T GetMainOffset(Axis axis) const
63     {
64         return axis == Axis::HORIZONTAL ? x_ : y_;
65     }
66 
GetCrossOffset(Axis axis)67     T GetCrossOffset(Axis axis) const
68     {
69         return axis == Axis::HORIZONTAL ? y_ : x_;
70     }
71 
GetDistance(const OffsetT & offset)72     T GetDistance(const OffsetT& offset) const
73     {
74         T dx = x_ - offset.x_;
75         T dy = y_ - offset.y_;
76         return std::sqrt(dx * dx + dy * dy);
77     }
78 
SetX(T x)79     void SetX(T x)
80     {
81         x_ = x;
82     }
83 
SetY(T y)84     void SetY(T y)
85     {
86         y_ = y;
87     }
88 
AddX(T x)89     void AddX(T x)
90     {
91         x_ += x;
92     }
93 
AddY(T y)94     void AddY(T y)
95     {
96         y_ += y;
97     }
98 
99     OffsetT operator+(const OffsetT& offset) const
100     {
101         return OffsetT(x_ + offset.x_, y_ + offset.y_);
102     }
103 
104     OffsetT operator-(const OffsetT& offset) const
105     {
106         return OffsetT(x_ - offset.x_, y_ - offset.y_);
107     }
108 
109     OffsetT operator*(double value) const
110     {
111         return OffsetT(x_ * value, y_ * value);
112     }
113 
114     OffsetT operator/(double value) const
115     {
116         if (NearZero(value)) {
117             return {};
118         }
119         return OffsetT(x_ / value, y_ / value);
120     }
121 
122     OffsetT& operator+=(const OffsetT& offset)
123     {
124         x_ += offset.x_;
125         y_ += offset.y_;
126         return *this;
127     }
128 
129     OffsetT& operator-=(const OffsetT& offset)
130     {
131         x_ -= offset.x_;
132         y_ -= offset.y_;
133         return *this;
134     }
135 
136     bool operator==(const OffsetT& offset) const
137     {
138         return NearEqual(x_, offset.x_) && NearEqual(y_, offset.y_);
139     }
140 
141     bool operator!=(const OffsetT& offset) const
142     {
143         return !operator==(offset);
144     }
145 
NonNegative()146     bool NonNegative() const
147     {
148         return GreatOrEqual(x_, 0) && GreatOrEqual(y_, 0);
149     }
150 
IsNegative()151     bool IsNegative() const
152     {
153         return LessNotEqual(x_, 0) && LessNotEqual(y_, 0);
154     }
155 
NonOffset()156     bool NonOffset() const
157     {
158         return NearZero(x_) && NearZero(y_);
159     }
160 
ToString()161     std::string ToString() const
162     {
163         static const int32_t precision = 2;
164         std::stringstream ss;
165         ss << "Offset (" << std::fixed << std::setprecision(precision) << x_ << ", " << y_ << ")";
166         std::string output = ss.str();
167         return output;
168     }
169 
170 private:
171     T x_ { 0 };
172     T y_ { 0 };
173 };
174 
175 using OffsetF = OffsetT<float>;
176 } // namespace OHOS::Ace::NG
177 
178 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_NG_OFFSET_T_H
179