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 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 friend inline bool operator==(const RectF& r1, const RectF& r2);
54 friend inline bool operator!=(const RectF& r1, const RectF& r2);
55
56 private:
57 scalar left_;
58 scalar right_;
59 scalar top_;
60 scalar bottom_;
61 };
62
RectF()63 inline RectF::RectF() noexcept : left_(0.0), right_(0.0), top_(0.0), bottom_(0.0) {}
64
RectF(const RectF & r)65 inline RectF::RectF(const RectF& r) noexcept
66 : left_(r.GetLeft()), right_(r.GetRight()), top_(r.GetTop()), bottom_(r.GetBottom())
67 {}
68
RectF(const scalar l,const scalar t,const scalar r,const scalar b)69 inline RectF::RectF(const scalar l, const scalar t, const scalar r, const scalar b) noexcept
70 : left_(l), right_(r), top_(t), bottom_(b)
71 {}
72
IsValid()73 inline bool RectF::IsValid() const
74 {
75 return left_ < right_ && top_ < bottom_;
76 }
77
GetLeft()78 inline scalar RectF::GetLeft() const
79 {
80 return left_;
81 }
82
GetTop()83 inline scalar RectF::GetTop() const
84 {
85 return top_;
86 }
87
GetRight()88 inline scalar RectF::GetRight() const
89 {
90 return right_;
91 }
92
GetBottom()93 inline scalar RectF::GetBottom() const
94 {
95 return bottom_;
96 }
97
GetWidth()98 inline scalar RectF::GetWidth() const
99 {
100 return right_ - left_;
101 }
102
GetHeight()103 inline scalar RectF::GetHeight() const
104 {
105 return bottom_ - top_;
106 }
107
SetLeft(scalar pos)108 inline void RectF::SetLeft(scalar pos)
109 {
110 left_ = pos;
111 }
112
SetTop(scalar pos)113 inline void RectF::SetTop(scalar pos)
114 {
115 top_ = pos;
116 }
117
SetRight(scalar pos)118 inline void RectF::SetRight(scalar pos)
119 {
120 right_ = pos;
121 }
122
SetBottom(scalar pos)123 inline void RectF::SetBottom(scalar pos)
124 {
125 bottom_ = pos;
126 }
127
Offset(scalar dx,scalar dy)128 inline void RectF::Offset(scalar dx, scalar dy)
129 {
130 left_ += dx;
131 right_ += dx;
132 top_ += dy;
133 bottom_ += dy;
134 }
135
136 inline bool operator==(const RectF& r1, const RectF& r2)
137 {
138 return IsScalarAlmostEqual(r1.left_, r2.left_) && IsScalarAlmostEqual(r1.right_, r2.right_) &&
139 IsScalarAlmostEqual(r1.top_, r2.top_) && IsScalarAlmostEqual(r1.bottom_, r2.bottom_);
140 }
141
142 inline bool operator!=(const RectF& r1, const RectF& r2)
143 {
144 return !IsScalarAlmostEqual(r1.left_, r2.left_) || !IsScalarAlmostEqual(r1.right_, r2.right_) ||
145 !IsScalarAlmostEqual(r1.top_, r2.top_) || !IsScalarAlmostEqual(r1.bottom_, r2.bottom_);
146 }
147
148 class RectI {
149 public:
150 inline RectI() noexcept;
151 inline RectI(const RectI& r) noexcept;
152 inline RectI(const int l, const int t, const int r, const int b) noexcept;
153
~RectI()154 ~RectI() {}
155
156 inline bool IsValid() const;
157
158 inline int GetLeft() const;
159 inline int GetTop() const;
160 inline int GetRight() const;
161 inline int GetBottom() const;
162
163 inline int GetWidth() const;
164 inline int GetHeight() const;
165
166 inline void SetLeft(int pos);
167 inline void SetTop(int pos);
168 inline void SetRight(int pos);
169 inline void SetBottom(int pos);
170
171 inline void Offset(int dx, int dy);
172
173 friend inline bool operator==(const RectI& r1, const RectI& r2);
174 friend inline bool operator!=(const RectI& r1, const RectI& r2);
175
176 private:
177 int left_;
178 int right_;
179 int top_;
180 int bottom_;
181 };
182
RectI()183 inline RectI::RectI() noexcept : left_(0), right_(0), top_(0), bottom_(0) {}
184
RectI(const RectI & r)185 inline RectI::RectI(const RectI& r) noexcept
186 : left_(r.GetLeft()), right_(r.GetRight()), top_(r.GetTop()), bottom_(r.GetBottom())
187 {}
188
RectI(const int l,const int t,const int r,const int b)189 inline RectI::RectI(const int l, const int t, const int r, const int b) noexcept
190 : left_(l), right_(r), top_(t), bottom_(b)
191 {}
192
IsValid()193 inline bool RectI::IsValid() const
194 {
195 return left_ <= right_ && top_ <= bottom_;
196 }
197
GetLeft()198 inline int RectI::GetLeft() const
199 {
200 return left_;
201 }
202
GetTop()203 inline int RectI::GetTop() const
204 {
205 return top_;
206 }
207
GetRight()208 inline int RectI::GetRight() const
209 {
210 return right_;
211 }
212
GetBottom()213 inline int RectI::GetBottom() const
214 {
215 return bottom_;
216 }
217
GetWidth()218 inline int RectI::GetWidth() const
219 {
220 return right_ - left_;
221 }
222
GetHeight()223 inline int RectI::GetHeight() const
224 {
225 return bottom_ - top_;
226 }
227
SetLeft(int pos)228 inline void RectI::SetLeft(int pos)
229 {
230 left_ = pos;
231 }
232
SetTop(int pos)233 inline void RectI::SetTop(int pos)
234 {
235 top_ = pos;
236 }
237
SetRight(int pos)238 inline void RectI::SetRight(int pos)
239 {
240 right_ = pos;
241 }
242
SetBottom(int pos)243 inline void RectI::SetBottom(int pos)
244 {
245 bottom_ = pos;
246 }
247
Offset(int dx,int dy)248 inline void RectI::Offset(int dx, int dy)
249 {
250 left_ += dx;
251 right_ += dx;
252 top_ += dy;
253 bottom_ += dy;
254 }
255
256 inline bool operator==(const RectI& r1, const RectI& r2)
257 {
258 return r1.left_ == r2.left_ && r1.right_ == r2.right_ && r1.top_ == r2.top_ && r1.bottom_ == r2.bottom_;
259 }
260
261 inline bool operator!=(const RectI& r1, const RectI& r2)
262 {
263 return r1.left_ != r2.left_ || r1.right_ != r2.right_ || r1.top_ != r2.top_ || r1.bottom_ != r2.bottom_;
264 }
265 } // namespace Drawing
266 } // namespace Rosen
267 } // namespace OHOS
268 #endif
269