1 /*
2  * Copyright (c) 2021 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 POINT_H
17 #define POINT_H
18 
19 #include <cfloat>
20 #include <string>
21 #include "utils/drawing_macros.h"
22 #include "utils/scalar.h"
23 
24 namespace OHOS {
25 namespace Rosen {
26 namespace Drawing {
27 class PointF;
28 
29 typedef PointF Point;
30 
31 class DRAWING_API PointF {
32 public:
33     inline PointF() noexcept;
34     inline PointF(const PointF& p) noexcept;
35     inline PointF(scalar x, scalar y) noexcept;
36 
~PointF()37     inline ~PointF() {}
38 
39     inline scalar GetX() const;
40     inline scalar GetY() const;
41 
42     inline void SetX(scalar x);
43     inline void SetY(scalar y);
44     inline void Set(scalar x, scalar y);
45 
46     inline bool IsZero() const;
47 
48     inline void Offset(scalar x, scalar y);
49 
50     inline PointF& operator+=(const PointF& p);
51     inline PointF& operator-=(const PointF& p);
52     inline PointF& operator*=(scalar scale);
53     inline PointF& operator/=(scalar divisor);
54 
55     friend inline const PointF operator+(const PointF& p1, const PointF& p2);
56     friend inline const PointF operator-(const PointF& p1, const PointF& p2);
57     friend inline const PointF operator*(scalar scale, const PointF& p);
58     friend inline const PointF operator*(const PointF& p, scalar scale);
59     friend inline const PointF operator/(const PointF& p, scalar divisor);
60     friend inline const PointF operator+(const PointF& p);
61     friend inline const PointF operator-(const PointF& p);
62     friend inline bool operator==(const PointF& p1, const PointF& p2);
63     friend inline bool operator!=(const PointF& p1, const PointF& p2);
64 
65     inline void Dump(std::string& out) const;
66 
67 private:
68     scalar x_;
69     scalar y_;
70 };
71 
72 inline PointF::PointF() noexcept : x_(0.0), y_(0.0) {}
73 
y_(p.GetY())74 inline PointF::PointF(const PointF& p) noexcept : x_(p.GetX()), y_(p.GetY()) {}
75 
y_(y)76 inline PointF::PointF(scalar x, scalar y) noexcept : x_(x), y_(y) {}
77 
GetX() const78 inline scalar PointF::GetX() const
79 {
80     return x_;
81 }
82 
GetY() const83 inline scalar PointF::GetY() const
84 {
85     return y_;
86 }
87 
SetX(scalar x)88 inline void PointF::SetX(scalar x)
89 {
90     x_ = x;
91 }
92 
SetY(scalar y)93 inline void PointF::SetY(scalar y)
94 {
95     y_ = y;
96 }
97 
Set(scalar x, scalar y)98 inline void PointF::Set(scalar x, scalar y)
99 {
100     x_ = x;
101     y_ = y;
102 }
103 
IsZero() const104 inline bool PointF::IsZero() const
105 {
106     return (0 == x_) && (0 == y_);
107 }
108 
Offset(scalar x, scalar y)109 inline void PointF::Offset(scalar x, scalar y)
110 {
111     x_ += x;
112     y_ += y;
113 }
114 
operator +=(const PointF& p)115 inline PointF& PointF::operator+=(const PointF& p)
116 {
117     x_ += p.x_;
118     y_ += p.y_;
119     return *this;
120 }
121 
operator -=(const PointF& p)122 inline PointF& PointF::operator-=(const PointF& p)
123 {
124     x_ -= p.x_;
125     y_ -= p.y_;
126     return *this;
127 }
128 
operator *=(scalar scale)129 inline PointF& PointF::operator*=(scalar scale)
130 {
131     x_ *= scale;
132     y_ *= scale;
133     return *this;
134 }
135 
operator /=(scalar divisor)136 inline PointF& PointF::operator/=(scalar divisor)
137 {
138     if (fabs(divisor) < FLT_EPSILON) {
139         return *this;
140     }
141     x_ /= divisor;
142     y_ /= divisor;
143     return *this;
144 }
145 
Dump(std::string& out) const146 inline void PointF::Dump(std::string& out) const
147 {
148     out += "[";
149     out += "x:" + std::to_string(x_);
150     out += " y:" + std::to_string(y_);
151     out += "]";
152 }
153 
operator +(const PointF& p1, const PointF& p2)154 inline const PointF operator+(const PointF& p1, const PointF& p2)
155 {
156     return PointF(p1.x_ + p2.x_, p1.y_ + p2.y_);
157 }
158 
operator -(const PointF& p1, const PointF& p2)159 inline const PointF operator-(const PointF& p1, const PointF& p2)
160 {
161     return PointF(p1.x_ - p2.x_, p1.y_ - p2.y_);
162 }
163 
operator *(scalar scale, const PointF& p)164 inline const PointF operator*(scalar scale, const PointF& p)
165 {
166     return PointF(scale * p.x_, scale * p.y_);
167 }
168 
operator *(const PointF& p, scalar scale)169 inline const PointF operator*(const PointF& p, scalar scale)
170 {
171     return PointF(p.x_ * scale, p.y_ * scale);
172 }
173 
operator /(const PointF& p, scalar divisor)174 inline const PointF operator/(const PointF& p, scalar divisor)
175 {
176     if (fabs(divisor) < FLT_EPSILON) {
177         return PointF(p.x_, p.y_);
178     }
179     return PointF(p.x_ / divisor, p.y_ / divisor);
180 }
181 
operator +(const PointF& p)182 inline const PointF operator+(const PointF& p)
183 {
184     return PointF(p.x_, p.y_);
185 }
186 
operator -(const PointF& p)187 inline const PointF operator-(const PointF& p)
188 {
189     return PointF(-p.x_, -p.y_);
190 }
191 
operator ==(const PointF& p1, const PointF& p2)192 inline bool operator==(const PointF& p1, const PointF& p2)
193 {
194     return IsScalarAlmostEqual(p1.x_, p2.x_) && IsScalarAlmostEqual(p1.y_, p2.y_);
195 }
196 
operator !=(const PointF& p1, const PointF& p2)197 inline bool operator!=(const PointF& p1, const PointF& p2)
198 {
199     return !IsScalarAlmostEqual(p1.x_, p2.x_) || !IsScalarAlmostEqual(p1.y_, p2.y_);
200 }
201 
202 class DRAWING_API PointI {
203 public:
204     inline PointI() noexcept;
205     inline PointI(const PointI& p) noexcept;
206     inline PointI(int x, int y) noexcept;
207 
~PointI()208     inline ~PointI() {}
209 
210     inline int GetX() const;
211     inline int GetY() const;
212 
213     inline void SetX(int x);
214     inline void SetY(int y);
215     inline void Set(int x, int y);
216 
217     inline PointI& operator+=(const PointI& p);
218     inline PointI& operator-=(const PointI& p);
219     inline PointI& operator*=(scalar scale);
220     inline PointI& operator/=(scalar divisor);
221 
222     friend inline const PointI operator+(const PointI& p1, const PointI& p2);
223     friend inline const PointI operator-(const PointI& p1, const PointI& p2);
224     friend inline const PointI operator*(scalar scale, const PointI& p);
225     friend inline const PointI operator*(const PointI& p, scalar scale);
226     friend inline const PointI operator/(const PointI& p, scalar divisor);
227     friend inline const PointI operator+(const PointI& p);
228     friend inline const PointI operator-(const PointI& p);
229     friend inline bool operator==(const PointI& p1, const PointI& p2);
230     friend inline bool operator!=(const PointI& p1, const PointI& p2);
231 
232 private:
233     int x_;
234     int y_;
235 };
236 
237 inline PointI::PointI() noexcept : x_(0), y_(0) {}
238 
y_(p.GetY())239 inline PointI::PointI(const PointI& p) noexcept : x_(p.GetX()), y_(p.GetY()) {}
240 
y_(y)241 inline PointI::PointI(int x, int y) noexcept : x_(x), y_(y) {}
242 
GetX() const243 inline int PointI::GetX() const
244 {
245     return x_;
246 }
247 
GetY() const248 inline int PointI::GetY() const
249 {
250     return y_;
251 }
252 
SetX(int x)253 inline void PointI::SetX(int x)
254 {
255     x_ = x;
256 }
257 
SetY(int y)258 inline void PointI::SetY(int y)
259 {
260     y_ = y;
261 }
262 
Set(int x, int y)263 inline void PointI::Set(int x, int y)
264 {
265     x_ = x;
266     y_ = y;
267 }
268 
operator +=(const PointI& p)269 inline PointI& PointI::operator+=(const PointI& p)
270 {
271     x_ += p.x_;
272     y_ += p.y_;
273     return *this;
274 }
275 
operator -=(const PointI& p)276 inline PointI& PointI::operator-=(const PointI& p)
277 {
278     x_ -= p.x_;
279     y_ -= p.y_;
280     return *this;
281 }
282 
operator *=(scalar scale)283 inline PointI& PointI::operator*=(scalar scale)
284 {
285     x_ = static_cast<int64_t>(x_ * scale);
286     y_ = static_cast<int64_t>(y_ * scale);
287     return *this;
288 }
289 
operator /=(scalar divisor)290 inline PointI& PointI::operator/=(scalar divisor)
291 {
292     if (divisor == 0) {
293         return *this;
294     }
295     x_ = static_cast<int>(x_ / divisor);
296     y_ = static_cast<int>(y_ / divisor);
297     return *this;
298 }
299 
operator +(const PointI& p1, const PointI& p2)300 inline const PointI operator+(const PointI& p1, const PointI& p2)
301 {
302     return PointI(p1.x_ + p2.x_, p1.y_ + p2.y_);
303 }
304 
operator -(const PointI& p1, const PointI& p2)305 inline const PointI operator-(const PointI& p1, const PointI& p2)
306 {
307     return PointI(p1.x_ - p2.x_, p1.y_ - p2.y_);
308 }
309 
operator *(scalar scale, const PointI& p)310 inline const PointI operator*(scalar scale, const PointI& p)
311 {
312     return PointI(static_cast<int64_t>(scale * p.x_), static_cast<int64_t>(scale * p.y_));
313 }
314 
operator *(const PointI& p, scalar scale)315 inline const PointI operator*(const PointI& p, scalar scale)
316 {
317     return PointI(static_cast<int64_t>(p.x_ * scale), static_cast<int64_t>(p.y_ * scale));
318 }
319 
operator /(const PointI& p, scalar divisor)320 inline const PointI operator/(const PointI& p, scalar divisor)
321 {
322     if (divisor == 0) {
323         return PointI(p.x_, p.y_);
324     }
325     return PointI(static_cast<int>(p.x_ / divisor), static_cast<int>(p.y_ / divisor));
326 }
327 
operator +(const PointI& p)328 inline const PointI operator+(const PointI& p)
329 {
330     return PointI(p.x_, p.y_);
331 }
332 
operator -(const PointI& p)333 inline const PointI operator-(const PointI& p)
334 {
335     return PointI(-p.x_, -p.y_);
336 }
337 
operator ==(const PointI& p1, const PointI& p2)338 inline bool operator==(const PointI& p1, const PointI& p2)
339 {
340     return p1.x_ == p2.x_ && p1.y_ == p2.y_;
341 }
342 
operator !=(const PointI& p1, const PointI& p2)343 inline bool operator!=(const PointI& p1, const PointI& p2)
344 {
345     return p1.x_ != p2.x_ || p1.y_ != p2.y_;
346 }
347 } // namespace Drawing
348 } // namespace Rosen
349 } // namespace OHOS
350 #endif
351