• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 RECT_H
17 #define RECT_H
18 
19 #include "utils/scalar.h"
20 
21 namespace OHOS {
22 namespace Rosen {
23 namespace Drawing {
24 class RectF;
25 
26 typedef RectF Rect;
27 
28 class RectF {
29 public:
30     inline RectF() noexcept;
31     inline RectF(const RectF& r) noexcept;
32     inline RectF(const scalar l, const scalar t, const scalar r, const scalar b) noexcept;
33 
~RectF()34     ~RectF() {}
35 
36     inline bool IsValid() const;
37 
38     inline scalar GetLeft() const;
39     inline scalar GetTop() const;
40     inline scalar GetRight() const;
41     inline scalar GetBottom() const;
42 
43     inline scalar GetWidth() const;
44     inline scalar GetHeight() const;
45 
46     inline void SetLeft(scalar pos);
47     inline void SetTop(scalar pos);
48     inline void SetRight(scalar pos);
49     inline void SetBottom(scalar pos);
50 
51     inline void Offset(scalar dx, scalar dy);
52 
53     /*
54      * @brief        If RectF intersects other, sets RectF to intersection.
55      * @param other  limit of result.
56      * @return       true if other and RectF have area in common.
57      */
58     inline bool Intersect(const RectF& other);
59 
60     /*
61      * @brief        If other is valid, sets RectF to the union of itself and other.
62      * @param other  expansion RectF.
63      * @return       true if other is valid.
64      */
65     inline bool Join(const RectF& other);
66 
67     friend inline bool operator==(const RectF& r1, const RectF& r2);
68     friend inline bool operator!=(const RectF& r1, const RectF& r2);
69 
70 private:
71     scalar left_;
72     scalar right_;
73     scalar top_;
74     scalar bottom_;
75 };
76 
RectF()77 inline RectF::RectF() noexcept : left_(0.0), right_(0.0), top_(0.0), bottom_(0.0) {}
78 
RectF(const RectF & r)79 inline RectF::RectF(const RectF& r) noexcept
80     : left_(r.GetLeft()), right_(r.GetRight()), top_(r.GetTop()), bottom_(r.GetBottom())
81 {}
82 
RectF(const scalar l,const scalar t,const scalar r,const scalar b)83 inline RectF::RectF(const scalar l, const scalar t, const scalar r, const scalar b) noexcept
84     : left_(l), right_(r), top_(t), bottom_(b)
85 {}
86 
IsValid()87 inline bool RectF::IsValid() const
88 {
89     return left_ < right_ && top_ < bottom_;
90 }
91 
GetLeft()92 inline scalar RectF::GetLeft() const
93 {
94     return left_;
95 }
96 
GetTop()97 inline scalar RectF::GetTop() const
98 {
99     return top_;
100 }
101 
GetRight()102 inline scalar RectF::GetRight() const
103 {
104     return right_;
105 }
106 
GetBottom()107 inline scalar RectF::GetBottom() const
108 {
109     return bottom_;
110 }
111 
GetWidth()112 inline scalar RectF::GetWidth() const
113 {
114     return right_ - left_;
115 }
116 
GetHeight()117 inline scalar RectF::GetHeight() const
118 {
119     return bottom_ - top_;
120 }
121 
SetLeft(scalar pos)122 inline void RectF::SetLeft(scalar pos)
123 {
124     left_ = pos;
125 }
126 
SetTop(scalar pos)127 inline void RectF::SetTop(scalar pos)
128 {
129     top_ = pos;
130 }
131 
SetRight(scalar pos)132 inline void RectF::SetRight(scalar pos)
133 {
134     right_ = pos;
135 }
136 
SetBottom(scalar pos)137 inline void RectF::SetBottom(scalar pos)
138 {
139     bottom_ = pos;
140 }
141 
Offset(scalar dx,scalar dy)142 inline void RectF::Offset(scalar dx, scalar dy)
143 {
144     left_ += dx;
145     right_ += dx;
146     top_ += dy;
147     bottom_ += dy;
148 }
149 
Intersect(const RectF & other)150 inline bool RectF::Intersect(const RectF& other)
151 {
152     RectF rectF(left_ > other.left_ ? left_ : other.left_, top_ > other.top_ ? top_ : other.top_,
153                 right_ < other.right_ ? right_ : other.right_, bottom_ < other.bottom_ ? bottom_ : other.bottom_);
154     if (!rectF.IsValid()) {
155         return false;
156     }
157     *this = rectF;
158     return true;
159 }
160 
Join(const RectF & other)161 inline bool RectF::Join(const RectF& other)
162 {
163     if (!other.IsValid()) {
164         return false;
165     }
166     if (!IsValid()) {
167         *this = other;
168     } else {
169         *this = RectF(left_ < other.left_ ? left_ : other.left_, top_ < other.top_ ? top_ : other.top_,
170             right_ > other.right_ ? right_ : other.right_, bottom_ > other.bottom_ ? bottom_ : other.bottom_);
171     }
172     return true;
173 }
174 
175 inline bool operator==(const RectF& r1, const RectF& r2)
176 {
177     return IsScalarAlmostEqual(r1.left_, r2.left_) && IsScalarAlmostEqual(r1.right_, r2.right_) &&
178         IsScalarAlmostEqual(r1.top_, r2.top_) && IsScalarAlmostEqual(r1.bottom_, r2.bottom_);
179 }
180 
181 inline bool operator!=(const RectF& r1, const RectF& r2)
182 {
183     return !IsScalarAlmostEqual(r1.left_, r2.left_) || !IsScalarAlmostEqual(r1.right_, r2.right_) ||
184         !IsScalarAlmostEqual(r1.top_, r2.top_) || !IsScalarAlmostEqual(r1.bottom_, r2.bottom_);
185 }
186 
187 class RectI {
188 public:
189     inline RectI() noexcept;
190     inline RectI(const RectI& r) noexcept;
191     inline RectI(const int l, const int t, const int r, const int b) noexcept;
192 
~RectI()193     ~RectI() {}
194 
195     inline bool IsValid() const;
196 
197     inline int GetLeft() const;
198     inline int GetTop() const;
199     inline int GetRight() const;
200     inline int GetBottom() const;
201 
202     inline int GetWidth() const;
203     inline int GetHeight() const;
204 
205     inline void SetLeft(int pos);
206     inline void SetTop(int pos);
207     inline void SetRight(int pos);
208     inline void SetBottom(int pos);
209 
210     inline void Offset(int dx, int dy);
211 
212     /*
213      * @brief        If RectI intersects other, sets RectI to intersection.
214      * @param other  limit of result.
215      * @return       true if other and RectI have area in common.
216      */
217     inline bool Intersect(const RectI& other);
218 
219     /*
220      * @brief        If other is valid, sets RectI to the union of itself and other.
221      * @param other  expansion RectI.
222      * @return       true if other is valid.
223      */
224     inline bool Join(const RectI& other);
225 
226     friend inline bool operator==(const RectI& r1, const RectI& r2);
227     friend inline bool operator!=(const RectI& r1, const RectI& r2);
228 
229 private:
230     int left_;
231     int right_;
232     int top_;
233     int bottom_;
234 };
235 
RectI()236 inline RectI::RectI() noexcept : left_(0), right_(0), top_(0), bottom_(0) {}
237 
RectI(const RectI & r)238 inline RectI::RectI(const RectI& r) noexcept
239     : left_(r.GetLeft()), right_(r.GetRight()), top_(r.GetTop()), bottom_(r.GetBottom())
240 {}
241 
RectI(const int l,const int t,const int r,const int b)242 inline RectI::RectI(const int l, const int t, const int r, const int b) noexcept
243     : left_(l), right_(r), top_(t), bottom_(b)
244 {}
245 
IsValid()246 inline bool RectI::IsValid() const
247 {
248     return left_ <= right_ && top_ <= bottom_;
249 }
250 
GetLeft()251 inline int RectI::GetLeft() const
252 {
253     return left_;
254 }
255 
GetTop()256 inline int RectI::GetTop() const
257 {
258     return top_;
259 }
260 
GetRight()261 inline int RectI::GetRight() const
262 {
263     return right_;
264 }
265 
GetBottom()266 inline int RectI::GetBottom() const
267 {
268     return bottom_;
269 }
270 
GetWidth()271 inline int RectI::GetWidth() const
272 {
273     return right_ - left_;
274 }
275 
GetHeight()276 inline int RectI::GetHeight() const
277 {
278     return bottom_ - top_;
279 }
280 
SetLeft(int pos)281 inline void RectI::SetLeft(int pos)
282 {
283     left_ = pos;
284 }
285 
SetTop(int pos)286 inline void RectI::SetTop(int pos)
287 {
288     top_ = pos;
289 }
290 
SetRight(int pos)291 inline void RectI::SetRight(int pos)
292 {
293     right_ = pos;
294 }
295 
SetBottom(int pos)296 inline void RectI::SetBottom(int pos)
297 {
298     bottom_ = pos;
299 }
300 
Offset(int dx,int dy)301 inline void RectI::Offset(int dx, int dy)
302 {
303     left_ += dx;
304     right_ += dx;
305     top_ += dy;
306     bottom_ += dy;
307 }
308 
Intersect(const RectI & other)309 inline bool RectI::Intersect(const RectI& other)
310 {
311     RectI rectI(left_ > other.left_ ? left_ : other.left_, top_ > other.top_ ? top_ : other.top_,
312         right_ < other.right_ ? right_ : other.right_, bottom_ < other.bottom_ ? bottom_ : other.bottom_);
313     if (!rectI.IsValid()) {
314         return false;
315     }
316     *this = rectI;
317     return true;
318 }
319 
Join(const RectI & other)320 inline bool RectI::Join(const RectI& other)
321 {
322     if (!other.IsValid()) return false;
323     if (!IsValid()) {
324         *this = other;
325     } else {
326         *this = RectI(left_ < other.left_ ? left_ : other.left_, top_ < other.top_ ? top_ : other.top_,
327             right_ > other.right_ ? right_ : other.right_, bottom_ > other.bottom_ ? bottom_ : other.bottom_);
328     }
329     return true;
330 }
331 
332 inline bool operator==(const RectI& r1, const RectI& r2)
333 {
334     return r1.left_ == r2.left_ && r1.right_ == r2.right_ && r1.top_ == r2.top_ && r1.bottom_ == r2.bottom_;
335 }
336 
337 inline bool operator!=(const RectI& r1, const RectI& r2)
338 {
339     return r1.left_ != r2.left_ || r1.right_ != r2.right_ || r1.top_ != r2.top_ || r1.bottom_ != r2.bottom_;
340 }
341 } // namespace Drawing
342 } // namespace Rosen
343 } // namespace OHOS
344 #endif
345