My Project
ParaRect.h
1 #pragma once
2 
3 #include "ParaMargins.h"
4 #include "ParaSize.h"
5 #include "ParaPoint.h"
6 
7 namespace ParaEngine
8 {
9  class QRectF;
10 
16  class QRect
17  {
18  public:
19  QRect() : x1(0), y1(0), x2(-1), y2(-1) {}
20  QRect(const QPoint &topleft, const QPoint &bottomright);
21  QRect(const QPoint &topleft, const QSize &size);
22  QRect(int32 left, int32 top, int32 width, int32 height);
23  QRect(const RECT& r) :x1(r.left), y1(r.top), x2(r.right-1), y2(r.bottom-1) {}
24  explicit QRect(const QRectF &rect);
25 
26  inline operator RECT() const{
27  RECT r = { x1, y1, x1 + width(), y1 + height() };
28  return r;
29  }
30 
31  inline bool isNull() const;
32  inline bool isEmpty() const;
33  inline bool isValid() const;
34 
35  inline int32 left() const;
36  inline int32 top() const;
37  inline int32 right() const;
38  inline int32 bottom() const;
39  QRect normalized() const;
40 
41  inline int32 x() const;
42  inline int32 y() const;
43  inline void setLeft(int32 pos);
44  inline void setTop(int32 pos);
45  inline void setRight(int32 pos);
46  inline void setBottom(int32 pos);
47  inline void setX(int32 x);
48  inline void setY(int32 y);
49 
50  inline void setTopLeft(const QPoint &p);
51  inline void setBottomRight(const QPoint &p);
52  inline void setTopRight(const QPoint &p);
53  inline void setBottomLeft(const QPoint &p);
54 
55  inline QPoint topLeft() const;
56  inline QPoint bottomRight() const;
57  inline QPoint topRight() const;
58  inline QPoint bottomLeft() const;
59  inline QPoint center() const;
60 
61  inline void moveLeft(int32 pos);
62  inline void moveTop(int32 pos);
63  inline void moveRight(int32 pos);
64  inline void moveBottom(int32 pos);
65  inline void moveTopLeft(const QPoint &p);
66  inline void moveBottomRight(const QPoint &p);
67  inline void moveTopRight(const QPoint &p);
68  inline void moveBottomLeft(const QPoint &p);
69  inline void moveCenter(const QPoint &p);
70 
71  inline void translate(int32 dx, int32 dy);
72  inline void translate(const QPoint &p);
73  inline QRect translated(int32 dx, int32 dy) const;
74  inline QRect translated(const QPoint &p) const;
75 
76  inline void moveTo(int32 x, int32 t);
77  inline void moveTo(const QPoint &p);
78 
79  inline void setRect(int32 x, int32 y, int32 w, int32 h);
80  inline void getRect(int32 *x, int32 *y, int32 *w, int32 *h) const;
81 
82  inline void setCoords(int32 x1, int32 y1, int32 x2, int32 y2);
83  inline void getCoords(int32 *x1, int32 *y1, int32 *x2, int32 *y2) const;
84 
85  inline void adjust(int32 x1, int32 y1, int32 x2, int32 y2);
86  inline QRect adjusted(int32 x1, int32 y1, int32 x2, int32 y2) const;
87 
88  inline QSize size() const;
89  inline int32 width() const;
90  inline int32 height() const;
91  inline void setWidth(int32 w);
92  inline void setHeight(int32 h);
93  inline void setSize(const QSize &s);
94 
95  QRect operator|(const QRect &r) const;
96  QRect operator&(const QRect &r) const;
97  inline QRect& operator|=(const QRect &r);
98  inline QRect& operator&=(const QRect &r);
99 
100  bool contains(const QRect &r, bool proper = false) const;
101  bool contains(const QPoint &p, bool proper = false) const;
102  inline bool contains(int32 x, int32 y) const;
103  inline bool contains(int32 x, int32 y, bool proper) const;
104  inline QRect united(const QRect &other) const;
105  inline QRect intersected(const QRect &other) const;
106  bool intersects(const QRect &r) const;
107 
108  inline QRect marginsAdded(const QMargins &margins) const;
109  inline QRect marginsRemoved(const QMargins &margins) const;
110  inline QRect &operator+=(const QMargins &margins);
111  inline QRect &operator-=(const QMargins &margins);
112 
113 
114  friend inline bool operator==(const QRect &, const QRect &);
115  friend inline bool operator!=(const QRect &, const QRect &);
116 
117  private:
118  int32 x1;
119  int32 y1;
120  int32 x2;
121  int32 y2;
122  };
123  inline bool operator==(const QRect &, const QRect &);
124  inline bool operator!=(const QRect &, const QRect &);
125 
126  /*****************************************************************************
127  QRect inline member functions
128  *****************************************************************************/
129 
130  inline QRect::QRect(int32 aleft, int32 atop, int32 awidth, int32 aheight)
131  : x1(aleft), y1(atop), x2(aleft + awidth - 1), y2(atop + aheight - 1) {}
132 
133  inline QRect::QRect(const QPoint &atopLeft, const QPoint &abottomRight)
134  : x1(atopLeft.x()), y1(atopLeft.y()), x2(abottomRight.x()), y2(abottomRight.y()) {}
135 
136  inline QRect::QRect(const QPoint &atopLeft, const QSize &asize)
137  : x1(atopLeft.x()), y1(atopLeft.y()), x2(atopLeft.x() + asize.width() - 1), y2(atopLeft.y() + asize.height() - 1) {}
138 
139  inline bool QRect::isNull() const
140  {
141  return x2 == x1 - 1 && y2 == y1 - 1;
142  }
143 
144  inline bool QRect::isEmpty() const
145  {
146  return x1 > x2 || y1 > y2;
147  }
148 
149  inline bool QRect::isValid() const
150  {
151  return x1 <= x2 && y1 <= y2;
152  }
153 
154  inline int32 QRect::left() const
155  {
156  return x1;
157  }
158 
159  inline int32 QRect::top() const
160  {
161  return y1;
162  }
163 
164  inline int32 QRect::right() const
165  {
166  return x2;
167  }
168 
169  inline int32 QRect::bottom() const
170  {
171  return y2;
172  }
173 
174  inline int32 QRect::x() const
175  {
176  return x1;
177  }
178 
179  inline int32 QRect::y() const
180  {
181  return y1;
182  }
183 
184  inline void QRect::setLeft(int32 pos)
185  {
186  x1 = pos;
187  }
188 
189  inline void QRect::setTop(int32 pos)
190  {
191  y1 = pos;
192  }
193 
194  inline void QRect::setRight(int32 pos)
195  {
196  x2 = pos;
197  }
198 
199  inline void QRect::setBottom(int32 pos)
200  {
201  y2 = pos;
202  }
203 
204  inline void QRect::setTopLeft(const QPoint &p)
205  {
206  x1 = p.x(); y1 = p.y();
207  }
208 
209  inline void QRect::setBottomRight(const QPoint &p)
210  {
211  x2 = p.x(); y2 = p.y();
212  }
213 
214  inline void QRect::setTopRight(const QPoint &p)
215  {
216  x2 = p.x(); y1 = p.y();
217  }
218 
219  inline void QRect::setBottomLeft(const QPoint &p)
220  {
221  x1 = p.x(); y2 = p.y();
222  }
223 
224  inline void QRect::setX(int32 ax)
225  {
226  x1 = ax;
227  }
228 
229  inline void QRect::setY(int32 ay)
230  {
231  y1 = ay;
232  }
233 
234  inline QPoint QRect::topLeft() const
235  {
236  return QPoint(x1, y1);
237  }
238 
239  inline QPoint QRect::bottomRight() const
240  {
241  return QPoint(x2, y2);
242  }
243 
244  inline QPoint QRect::topRight() const
245  {
246  return QPoint(x2, y1);
247  }
248 
249  inline QPoint QRect::bottomLeft() const
250  {
251  return QPoint(x1, y2);
252  }
253 
254  inline QPoint QRect::center() const
255  {
256  return QPoint((x1 + x2) / 2, (y1 + y2) / 2);
257  }
258 
259  inline int32 QRect::width() const
260  {
261  return x2 - x1 + 1;
262  }
263 
264  inline int32 QRect::height() const
265  {
266  return y2 - y1 + 1;
267  }
268 
269  inline QSize QRect::size() const
270  {
271  return QSize(width(), height());
272  }
273 
274  inline void QRect::translate(int32 dx, int32 dy)
275  {
276  x1 += dx;
277  y1 += dy;
278  x2 += dx;
279  y2 += dy;
280  }
281 
282  inline void QRect::translate(const QPoint &p)
283  {
284  x1 += p.x();
285  y1 += p.y();
286  x2 += p.x();
287  y2 += p.y();
288  }
289 
290  inline QRect QRect::translated(int32 dx, int32 dy) const
291  {
292  return QRect(QPoint(x1 + dx, y1 + dy), QPoint(x2 + dx, y2 + dy));
293  }
294 
295  inline QRect QRect::translated(const QPoint &p) const
296  {
297  return QRect(QPoint(x1 + p.x(), y1 + p.y()), QPoint(x2 + p.x(), y2 + p.y()));
298  }
299 
300  inline void QRect::moveTo(int32 ax, int32 ay)
301  {
302  x2 += ax - x1;
303  y2 += ay - y1;
304  x1 = ax;
305  y1 = ay;
306  }
307 
308  inline void QRect::moveTo(const QPoint &p)
309  {
310  x2 += p.x() - x1;
311  y2 += p.y() - y1;
312  x1 = p.x();
313  y1 = p.y();
314  }
315 
316  inline void QRect::moveLeft(int32 pos)
317  {
318  x2 += (pos - x1); x1 = pos;
319  }
320 
321  inline void QRect::moveTop(int32 pos)
322  {
323  y2 += (pos - y1); y1 = pos;
324  }
325 
326  inline void QRect::moveRight(int32 pos)
327  {
328  x1 += (pos - x2);
329  x2 = pos;
330  }
331 
332  inline void QRect::moveBottom(int32 pos)
333  {
334  y1 += (pos - y2);
335  y2 = pos;
336  }
337 
338  inline void QRect::moveTopLeft(const QPoint &p)
339  {
340  moveLeft(p.x());
341  moveTop(p.y());
342  }
343 
344  inline void QRect::moveBottomRight(const QPoint &p)
345  {
346  moveRight(p.x());
347  moveBottom(p.y());
348  }
349 
350  inline void QRect::moveTopRight(const QPoint &p)
351  {
352  moveRight(p.x());
353  moveTop(p.y());
354  }
355 
356  inline void QRect::moveBottomLeft(const QPoint &p)
357  {
358  moveLeft(p.x());
359  moveBottom(p.y());
360  }
361 
362  inline void QRect::moveCenter(const QPoint &p)
363  {
364  int32 w = x2 - x1;
365  int32 h = y2 - y1;
366  x1 = p.x() - w / 2;
367  y1 = p.y() - h / 2;
368  x2 = x1 + w;
369  y2 = y1 + h;
370  }
371 
372  inline void QRect::getRect(int32 *ax, int32 *ay, int32 *aw, int32 *ah) const
373  {
374  *ax = x1;
375  *ay = y1;
376  *aw = x2 - x1 + 1;
377  *ah = y2 - y1 + 1;
378  }
379 
380  inline void QRect::setRect(int32 ax, int32 ay, int32 aw, int32 ah)
381  {
382  x1 = ax;
383  y1 = ay;
384  x2 = (ax + aw - 1);
385  y2 = (ay + ah - 1);
386  }
387 
388  inline void QRect::getCoords(int32 *xp1, int32 *yp1, int32 *xp2, int32 *yp2) const
389  {
390  *xp1 = x1;
391  *yp1 = y1;
392  *xp2 = x2;
393  *yp2 = y2;
394  }
395 
396  inline void QRect::setCoords(int32 xp1, int32 yp1, int32 xp2, int32 yp2)
397  {
398  x1 = xp1;
399  y1 = yp1;
400  x2 = xp2;
401  y2 = yp2;
402  }
403 
404  inline QRect QRect::adjusted(int32 xp1, int32 yp1, int32 xp2, int32 yp2) const
405  {
406  return QRect(QPoint(x1 + xp1, y1 + yp1), QPoint(x2 + xp2, y2 + yp2));
407  }
408 
409  inline void QRect::adjust(int32 dx1, int32 dy1, int32 dx2, int32 dy2)
410  {
411  x1 += dx1;
412  y1 += dy1;
413  x2 += dx2;
414  y2 += dy2;
415  }
416 
417  inline void QRect::setWidth(int32 w)
418  {
419  x2 = (x1 + w - 1);
420  }
421 
422  inline void QRect::setHeight(int32 h)
423  {
424  y2 = (y1 + h - 1);
425  }
426 
427  inline void QRect::setSize(const QSize &s)
428  {
429  x2 = (s.width() + x1 - 1);
430  y2 = (s.height() + y1 - 1);
431  }
432 
433  inline bool QRect::contains(int32 ax, int32 ay, bool aproper) const
434  {
435  return contains(QPoint(ax, ay), aproper);
436  }
437 
438  inline bool QRect::contains(int32 ax, int32 ay) const
439  {
440  return contains(QPoint(ax, ay), false);
441  }
442 
443  inline QRect& QRect::operator|=(const QRect &r)
444  {
445  *this = *this | r;
446  return *this;
447  }
448 
449  inline QRect& QRect::operator&=(const QRect &r)
450  {
451  *this = *this & r;
452  return *this;
453  }
454 
455  inline QRect QRect::intersected(const QRect &other) const
456  {
457  return *this & other;
458  }
459 
460  inline QRect QRect::united(const QRect &r) const
461  {
462  return *this | r;
463  }
464 
465  inline bool operator==(const QRect &r1, const QRect &r2)
466  {
467  return r1.x1 == r2.x1 && r1.x2 == r2.x2 && r1.y1 == r2.y1 && r1.y2 == r2.y2;
468  }
469 
470  inline bool operator!=(const QRect &r1, const QRect &r2)
471  {
472  return r1.x1 != r2.x1 || r1.x2 != r2.x2 || r1.y1 != r2.y1 || r1.y2 != r2.y2;
473  }
474 
475  inline QRect operator+(const QRect &rectangle, const QMargins &margins)
476  {
477  return QRect(QPoint(rectangle.left() - margins.left(), rectangle.top() - margins.top()),
478  QPoint(rectangle.right() + margins.right(), rectangle.bottom() + margins.bottom()));
479  }
480 
481  inline QRect operator+(const QMargins &margins, const QRect &rectangle)
482  {
483  return QRect(QPoint(rectangle.left() - margins.left(), rectangle.top() - margins.top()),
484  QPoint(rectangle.right() + margins.right(), rectangle.bottom() + margins.bottom()));
485  }
486 
487  inline QRect operator-(const QRect &lhs, const QMargins &rhs)
488  {
489  return QRect(QPoint(lhs.left() + rhs.left(), lhs.top() + rhs.top()),
490  QPoint(lhs.right() - rhs.right(), lhs.bottom() - rhs.bottom()));
491  }
492 
493  inline QRect QRect::marginsAdded(const QMargins &margins) const
494  {
495  return QRect(QPoint(x1 - margins.left(), y1 - margins.top()),
496  QPoint(x2 + margins.right(), y2 + margins.bottom()));
497  }
498 
499  inline QRect QRect::marginsRemoved(const QMargins &margins) const
500  {
501  return QRect(QPoint(x1 + margins.left(), y1 + margins.top()),
502  QPoint(x2 - margins.right(), y2 - margins.bottom()));
503  }
504 
505  inline QRect &QRect::operator+=(const QMargins &margins)
506  {
507  *this = marginsAdded(margins);
508  return *this;
509  }
510 
511  inline QRect &QRect::operator-=(const QMargins &margins)
512  {
513  *this = marginsRemoved(margins);
514  return *this;
515  }
516 
517  class QRectF
518  {
519  public:
520  QRectF() : xp(0.), yp(0.), w(0.), h(0.) {}
521  QRectF(const QPointF &topleft, const QSizeF &size);
522  QRectF(const QPointF &topleft, const QPointF &bottomRight);
523  QRectF(float left, float top, float width, float height);
524  QRectF(const QRect &rect);
525  inline explicit QRectF(const RECT& r) :xp((float)r.left), yp((float)r.top), w((float)(r.right - r.left)), h((float)(r.bottom - r.top)) {}
526 
527  inline operator RECT() const{
528  RECT r = { (int)x(), (int)y(), (int)right(), (int)bottom() };
529  return r;
530  }
531 
532  inline bool isNull() const;
533  inline bool isEmpty() const;
534  inline bool isValid() const;
535  QRectF normalized() const;
536 
537  inline float left() const { return xp; }
538  inline float top() const { return yp; }
539  inline float right() const { return xp + w; }
540  inline float bottom() const { return yp + h; }
541 
542  inline float x() const;
543  inline float y() const;
544  inline void setLeft(float pos);
545  inline void setTop(float pos);
546  inline void setRight(float pos);
547  inline void setBottom(float pos);
548  inline void setX(float pos) { setLeft(pos); }
549  inline void setY(float pos) { setTop(pos); }
550 
551  inline QPointF topLeft() const { return QPointF(xp, yp); }
552  inline QPointF bottomRight() const { return QPointF(xp + w, yp + h); }
553  inline QPointF topRight() const { return QPointF(xp + w, yp); }
554  inline QPointF bottomLeft() const { return QPointF(xp, yp + h); }
555  inline QPointF center() const;
556 
557  inline void setTopLeft(const QPointF &p);
558  inline void setBottomRight(const QPointF &p);
559  inline void setTopRight(const QPointF &p);
560  inline void setBottomLeft(const QPointF &p);
561 
562  inline void moveLeft(float pos);
563  inline void moveTop(float pos);
564  inline void moveRight(float pos);
565  inline void moveBottom(float pos);
566  inline void moveTopLeft(const QPointF &p);
567  inline void moveBottomRight(const QPointF &p);
568  inline void moveTopRight(const QPointF &p);
569  inline void moveBottomLeft(const QPointF &p);
570  inline void moveCenter(const QPointF &p);
571 
572  inline void translate(float dx, float dy);
573  inline void translate(const QPointF &p);
574 
575  inline QRectF translated(float dx, float dy) const;
576  inline QRectF translated(const QPointF &p) const;
577 
578  inline void moveTo(float x, float y);
579  inline void moveTo(const QPointF &p);
580 
581  inline void setRect(float x, float y, float w, float h);
582  inline void getRect(float *x, float *y, float *w, float *h) const;
583 
584  inline void setCoords(float x1, float y1, float x2, float y2);
585  inline void getCoords(float *x1, float *y1, float *x2, float *y2) const;
586 
587  inline void adjust(float x1, float y1, float x2, float y2);
588  inline QRectF adjusted(float x1, float y1, float x2, float y2) const;
589 
590  inline QSizeF size() const;
591  inline float width() const;
592  inline float height() const;
593  inline void setWidth(float w);
594  inline void setHeight(float h);
595  inline void setSize(const QSizeF &s);
596 
597  QRectF operator|(const QRectF &r) const;
598  QRectF operator&(const QRectF &r) const;
599  inline QRectF& operator|=(const QRectF &r);
600  inline QRectF& operator&=(const QRectF &r);
601 
602  bool contains(const QRectF &r) const;
603  bool contains(const QPointF &p) const;
604  inline bool contains(float x, float y) const;
605  inline QRectF united(const QRectF &other) const;
606  inline QRectF intersected(const QRectF &other) const;
607  bool intersects(const QRectF &r) const;
608 
609  inline QRectF marginsAdded(const QMarginsF &margins) const;
610  inline QRectF marginsRemoved(const QMarginsF &margins) const;
611  inline QRectF &operator+=(const QMarginsF &margins);
612  inline QRectF &operator-=(const QMarginsF &margins);
613 
614  friend inline bool operator==(const QRectF &, const QRectF &);
615  friend inline bool operator!=(const QRectF &, const QRectF &);
616 
617  inline QRect toRect() const;
618  QRect toAlignedRect() const;
619 
620  private:
621  float xp;
622  float yp;
623  float w;
624  float h;
625  };
626 
627  inline bool operator==(const QRectF &, const QRectF &);
628  inline bool operator!=(const QRectF &, const QRectF &);
629 
630  /*****************************************************************************
631  QRectF inline member functions
632  *****************************************************************************/
633 
634  inline QRectF::QRectF(float aleft, float atop, float awidth, float aheight)
635  : xp(aleft), yp(atop), w(awidth), h(aheight)
636  {
637  }
638 
639  inline QRectF::QRectF(const QPointF &atopLeft, const QSizeF &asize)
640  : xp(atopLeft.x()), yp(atopLeft.y()), w(asize.width()), h(asize.height())
641  {
642  }
643 
644 
645  inline QRectF::QRectF(const QPointF &atopLeft, const QPointF &abottomRight)
646  : xp(atopLeft.x()), yp(atopLeft.y()), w(abottomRight.x() - atopLeft.x()), h(abottomRight.y() - atopLeft.y())
647  {
648  }
649 
650  inline QRectF::QRectF(const QRect &r)
651  : xp((float)r.x()), yp((float)r.y()), w((float)r.width()), h((float)r.height())
652  {
653  }
654 
655  inline bool QRectF::isNull() const
656  {
657  return w == 0. && h == 0.;
658  }
659 
660  inline bool QRectF::isEmpty() const
661  {
662  return w <= 0. || h <= 0.;
663  }
664 
665  inline bool QRectF::isValid() const
666  {
667  return w > 0. && h > 0.;
668  }
669 
670  inline float QRectF::x() const
671  {
672  return xp;
673  }
674 
675  inline float QRectF::y() const
676  {
677  return yp;
678  }
679 
680  inline void QRectF::setLeft(float pos) { float diff = pos - xp; xp += diff; w -= diff; }
681 
682  inline void QRectF::setRight(float pos) { w = pos - xp; }
683 
684  inline void QRectF::setTop(float pos) { float diff = pos - yp; yp += diff; h -= diff; }
685 
686  inline void QRectF::setBottom(float pos) { h = pos - yp; }
687 
688  inline void QRectF::setTopLeft(const QPointF &p) { setLeft(p.x()); setTop(p.y()); }
689 
690  inline void QRectF::setTopRight(const QPointF &p) { setRight(p.x()); setTop(p.y()); }
691 
692  inline void QRectF::setBottomLeft(const QPointF &p) { setLeft(p.x()); setBottom(p.y()); }
693 
694  inline void QRectF::setBottomRight(const QPointF &p) { setRight(p.x()); setBottom(p.y()); }
695 
696  inline QPointF QRectF::center() const
697  {
698  return QPointF(xp + w / 2, yp + h / 2);
699  }
700 
701  inline void QRectF::moveLeft(float pos) { xp = pos; }
702 
703  inline void QRectF::moveTop(float pos) { yp = pos; }
704 
705  inline void QRectF::moveRight(float pos) { xp = pos - w; }
706 
707  inline void QRectF::moveBottom(float pos) { yp = pos - h; }
708 
709  inline void QRectF::moveTopLeft(const QPointF &p) { moveLeft(p.x()); moveTop(p.y()); }
710 
711  inline void QRectF::moveTopRight(const QPointF &p) { moveRight(p.x()); moveTop(p.y()); }
712 
713  inline void QRectF::moveBottomLeft(const QPointF &p) { moveLeft(p.x()); moveBottom(p.y()); }
714 
715  inline void QRectF::moveBottomRight(const QPointF &p) { moveRight(p.x()); moveBottom(p.y()); }
716 
717  inline void QRectF::moveCenter(const QPointF &p) { xp = p.x() - w / 2; yp = p.y() - h / 2; }
718 
719  inline float QRectF::width() const
720  {
721  return w;
722  }
723 
724  inline float QRectF::height() const
725  {
726  return h;
727  }
728 
729  inline QSizeF QRectF::size() const
730  {
731  return QSizeF(w, h);
732  }
733 
734  inline void QRectF::translate(float dx, float dy)
735  {
736  xp += dx;
737  yp += dy;
738  }
739 
740  inline void QRectF::translate(const QPointF &p)
741  {
742  xp += p.x();
743  yp += p.y();
744  }
745 
746  inline void QRectF::moveTo(float ax, float ay)
747  {
748  xp = ax;
749  yp = ay;
750  }
751 
752  inline void QRectF::moveTo(const QPointF &p)
753  {
754  xp = p.x();
755  yp = p.y();
756  }
757 
758  inline QRectF QRectF::translated(float dx, float dy) const
759  {
760  return QRectF(xp + dx, yp + dy, w, h);
761  }
762 
763  inline QRectF QRectF::translated(const QPointF &p) const
764  {
765  return QRectF(xp + p.x(), yp + p.y(), w, h);
766  }
767 
768  inline void QRectF::getRect(float *ax, float *ay, float *aaw, float *aah) const
769  {
770  *ax = this->xp;
771  *ay = this->yp;
772  *aaw = this->w;
773  *aah = this->h;
774  }
775 
776  inline void QRectF::setRect(float ax, float ay, float aaw, float aah)
777  {
778  this->xp = ax;
779  this->yp = ay;
780  this->w = aaw;
781  this->h = aah;
782  }
783 
784  inline void QRectF::getCoords(float *xp1, float *yp1, float *xp2, float *yp2) const
785  {
786  *xp1 = xp;
787  *yp1 = yp;
788  *xp2 = xp + w;
789  *yp2 = yp + h;
790  }
791 
792  inline void QRectF::setCoords(float xp1, float yp1, float xp2, float yp2)
793  {
794  xp = xp1;
795  yp = yp1;
796  w = xp2 - xp1;
797  h = yp2 - yp1;
798  }
799 
800  inline void QRectF::adjust(float xp1, float yp1, float xp2, float yp2)
801  {
802  xp += xp1; yp += yp1; w += xp2 - xp1; h += yp2 - yp1;
803  }
804 
805  inline QRectF QRectF::adjusted(float xp1, float yp1, float xp2, float yp2) const
806  {
807  return QRectF(xp + xp1, yp + yp1, w + xp2 - xp1, h + yp2 - yp1);
808  }
809 
810  inline void QRectF::setWidth(float aw)
811  {
812  this->w = aw;
813  }
814 
815  inline void QRectF::setHeight(float ah)
816  {
817  this->h = ah;
818  }
819 
820  inline void QRectF::setSize(const QSizeF &s)
821  {
822  w = s.width();
823  h = s.height();
824  }
825 
826  inline bool QRectF::contains(float ax, float ay) const
827  {
828  return contains(QPointF(ax, ay));
829  }
830 
831  inline QRectF& QRectF::operator|=(const QRectF &r)
832  {
833  *this = *this | r;
834  return *this;
835  }
836 
837  inline QRectF& QRectF::operator&=(const QRectF &r)
838  {
839  *this = *this & r;
840  return *this;
841  }
842 
843  inline QRectF QRectF::intersected(const QRectF &r) const
844  {
845  return *this & r;
846  }
847 
848  inline QRectF QRectF::united(const QRectF &r) const
849  {
850  return *this | r;
851  }
852 
853  inline bool operator==(const QRectF &r1, const QRectF &r2)
854  {
855  return Math::FuzzyCompare(r1.xp, r2.xp) && Math::FuzzyCompare(r1.yp, r2.yp)
856  && Math::FuzzyCompare(r1.w, r2.w) && Math::FuzzyCompare(r1.h, r2.h);
857  }
858 
859  inline bool operator!=(const QRectF &r1, const QRectF &r2)
860  {
861  return !Math::FuzzyCompare(r1.xp, r2.xp) || !Math::FuzzyCompare(r1.yp, r2.yp)
862  || !Math::FuzzyCompare(r1.w, r2.w) || !Math::FuzzyCompare(r1.h, r2.h);
863  }
864 
865  inline QRect QRectF::toRect() const
866  {
867  return QRect(Math::Round(xp), Math::Round(yp), Math::Round(w), Math::Round(h));
868  }
869 
870  inline QRectF operator+(const QRectF &lhs, const QMarginsF &rhs)
871  {
872  return QRectF(QPointF(lhs.left() - rhs.left(), lhs.top() - rhs.top()),
873  QSizeF(lhs.width() + rhs.left() + rhs.right(), lhs.height() + rhs.top() + rhs.bottom()));
874  }
875 
876  inline QRectF operator+(const QMarginsF &lhs, const QRectF &rhs)
877  {
878  return QRectF(QPointF(rhs.left() - lhs.left(), rhs.top() - lhs.top()),
879  QSizeF(rhs.width() + lhs.left() + lhs.right(), rhs.height() + lhs.top() + lhs.bottom()));
880  }
881 
882  inline QRectF operator-(const QRectF &lhs, const QMarginsF &rhs)
883  {
884  return QRectF(QPointF(lhs.left() + rhs.left(), lhs.top() + rhs.top()),
885  QSizeF(lhs.width() - rhs.left() - rhs.right(), lhs.height() - rhs.top() - rhs.bottom()));
886  }
887 
888  inline QRectF QRectF::marginsAdded(const QMarginsF &margins) const
889  {
890  return QRectF(QPointF(xp - margins.left(), yp - margins.top()),
891  QSizeF(w + margins.left() + margins.right(), h + margins.top() + margins.bottom()));
892  }
893 
894  inline QRectF QRectF::marginsRemoved(const QMarginsF &margins) const
895  {
896  return QRectF(QPointF(xp + margins.left(), yp + margins.top()),
897  QSizeF(w - margins.left() - margins.right(), h - margins.top() - margins.bottom()));
898  }
899 
900  inline QRectF &QRectF::operator+=(const QMarginsF &margins)
901  {
902  *this = marginsAdded(margins);
903  return *this;
904  }
905 
906  inline QRectF &QRectF::operator-=(const QMarginsF &margins)
907  {
908  *this = marginsRemoved(margins);
909  return *this;
910  }
911 
912 }
static int Round(float fValue)
get the closest integer near the specified float number.
Definition: ParaMath.h:609
Definition: ParaPoint.h:203
Definition: ParaSize.h:195
different physics engine has different winding order.
Definition: EventBinding.h:32
Definition: ManagedDef.h:18
Definition: ParaRect.h:517
when concerting QRect with QRectF and RECT, we ensure that their left, top and width, height are the same.
Definition: ParaRect.h:16
Definition: ParaMargins.h:5
Definition: other.hpp:41
Definition: ParaMargins.h:242
Definition: ParaPoint.h:5
bool contains(const QRectF &r) const
Definition: ParaRect.cpp:360
QRect operator|(const QRect &r) const
Definition: ParaRect.cpp:139
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: ParaSize.h:9