QtBase  v6.3.1
qrect.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qrect.h"
41 #include "qdatastream.h"
42 #include "qmath.h"
43 
44 #include <private/qdebug_p.h>
45 
47 
211 /*****************************************************************************
212  QRect member functions
213  *****************************************************************************/
214 
310 {
311  QRect r(*this);
312  if (x2 < x1) { // swap bad x values
313  r.x1 = x2 + 1;
314  r.x2 = x1 - 1;
315  }
316  if (y2 < y1) { // swap bad y values
317  r.y1 = y2 + 1;
318  r.y2 = y1 - 1;
319  }
320  return r;
321 }
322 
323 
823 bool QRect::contains(const QPoint &p, bool proper) const noexcept
824 {
825  int l, r;
826  if (x2 < x1 - 1) {
827  l = x2 + 1;
828  r = x1 - 1;
829  } else {
830  l = x1;
831  r = x2;
832  }
833  if (proper) {
834  if (p.x() <= l || p.x() >= r)
835  return false;
836  } else {
837  if (p.x() < l || p.x() > r)
838  return false;
839  }
840  int t, b;
841  if (y2 < y1 - 1) {
842  t = y2 + 1;
843  b = y1 - 1;
844  } else {
845  t = y1;
846  b = y2;
847  }
848  if (proper) {
849  if (p.y() <= t || p.y() >= b)
850  return false;
851  } else {
852  if (p.y() < t || p.y() > b)
853  return false;
854  }
855  return true;
856 }
857 
858 
887 bool QRect::contains(const QRect &r, bool proper) const noexcept
888 {
889  if (isNull() || r.isNull())
890  return false;
891 
892  int l1 = x1;
893  int r1 = x1 - 1;
894  if (x2 < x1 - 1)
895  l1 = x2 + 1;
896  else
897  r1 = x2;
898 
899  int l2 = r.x1;
900  int r2 = r.x1 - 1;
901  if (r.x2 < r.x1 - 1)
902  l2 = r.x2 + 1;
903  else
904  r2 = r.x2;
905 
906  if (proper) {
907  if (l2 <= l1 || r2 >= r1)
908  return false;
909  } else {
910  if (l2 < l1 || r2 > r1)
911  return false;
912  }
913 
914  int t1 = y1;
915  int b1 = y1 - 1;
916  if (y2 < y1 - 1)
917  t1 = y2 + 1;
918  else
919  b1 = y2;
920 
921  int t2 = r.y1;
922  int b2 = r.y1 - 1;
923  if (r.y2 < r.y1 - 1)
924  t2 = r.y2 + 1;
925  else
926  b2 = r.y2;
927 
928  if (proper) {
929  if (t2 <= t1 || b2 >= b1)
930  return false;
931  } else {
932  if (t2 < t1 || b2 > b1)
933  return false;
934  }
935 
936  return true;
937 }
938 
965 QRect QRect::operator|(const QRect &r) const noexcept
966 {
967  if (isNull())
968  return r;
969  if (r.isNull())
970  return *this;
971 
972  int l1 = x1;
973  int r1 = x1 - 1;
974  if (x2 < x1 - 1)
975  l1 = x2 + 1;
976  else
977  r1 = x2;
978 
979  int l2 = r.x1;
980  int r2 = r.x1 - 1;
981  if (r.x2 < r.x1 - 1)
982  l2 = r.x2 + 1;
983  else
984  r2 = r.x2;
985 
986  int t1 = y1;
987  int b1 = y1 - 1;
988  if (y2 < y1 - 1)
989  t1 = y2 + 1;
990  else
991  b1 = y2;
992 
993  int t2 = r.y1;
994  int b2 = r.y1 - 1;
995  if (r.y2 < r.y1 - 1)
996  t2 = r.y2 + 1;
997  else
998  b2 = r.y2;
999 
1000  QRect tmp;
1001  tmp.x1 = qMin(l1, l2);
1002  tmp.x2 = qMax(r1, r2);
1003  tmp.y1 = qMin(t1, t2);
1004  tmp.y2 = qMax(b1, b2);
1005  return tmp;
1006 }
1007 
1029 QRect QRect::operator&(const QRect &r) const noexcept
1030 {
1031  if (isNull() || r.isNull())
1032  return QRect();
1033 
1034  int l1 = x1;
1035  int r1 = x2;
1036  if (x2 < x1 - 1) {
1037  l1 = x2 + 1;
1038  r1 = x1 - 1;
1039  }
1040 
1041  int l2 = r.x1;
1042  int r2 = r.x2;
1043  if (r.x2 < r.x1 - 1) {
1044  l2 = r.x2 + 1;
1045  r2 = r.x1 - 1;
1046  }
1047 
1048  if (l1 > r2 || l2 > r1)
1049  return QRect();
1050 
1051  int t1 = y1;
1052  int b1 = y2;
1053  if (y2 < y1 - 1) {
1054  t1 = y2 + 1;
1055  b1 = y1 - 1;
1056  }
1057 
1058  int t2 = r.y1;
1059  int b2 = r.y2;
1060  if (r.y2 < r.y1 - 1) {
1061  t2 = r.y2 + 1;
1062  b2 = r.y1 - 1;
1063  }
1064 
1065  if (t1 > b2 || t2 > b1)
1066  return QRect();
1067 
1068  QRect tmp;
1069  tmp.x1 = qMax(l1, l2);
1070  tmp.x2 = qMin(r1, r2);
1071  tmp.y1 = qMax(t1, t2);
1072  tmp.y2 = qMin(b1, b2);
1073  return tmp;
1074 }
1075 
1101 bool QRect::intersects(const QRect &r) const noexcept
1102 {
1103  if (isNull() || r.isNull())
1104  return false;
1105 
1106  int l1 = x1;
1107  int r1 = x2;
1108  if (x2 < x1 - 1) {
1109  l1 = x2 + 1;
1110  r1 = x1 - 1;
1111  }
1112 
1113  int l2 = r.x1;
1114  int r2 = r.x2;
1115  if (r.x2 < r.x1 - 1) {
1116  l2 = r.x2 + 1;
1117  r2 = r.x1 - 1;
1118  }
1119 
1120  if (l1 > r2 || l2 > r1)
1121  return false;
1122 
1123  int t1 = y1;
1124  int b1 = y2;
1125  if (y2 < y1 - 1) {
1126  t1 = y2 + 1;
1127  b1 = y1 - 1;
1128  }
1129 
1130  int t2 = r.y1;
1131  int b2 = r.y2;
1132  if (r.y2 < r.y1 - 1) {
1133  t2 = r.y2 + 1;
1134  b2 = r.y1 - 1;
1135  }
1136 
1137  if (t1 > b2 || t2 > b1)
1138  return false;
1139 
1140  return true;
1141 }
1142 
1235 /*****************************************************************************
1236  QRect stream functions
1237  *****************************************************************************/
1238 #ifndef QT_NO_DATASTREAM
1250 {
1251  if (s.version() == 1)
1252  s << (qint16)r.left() << (qint16)r.top()
1253  << (qint16)r.right() << (qint16)r.bottom();
1254  else
1255  s << (qint32)r.left() << (qint32)r.top()
1256  << (qint32)r.right() << (qint32)r.bottom();
1257  return s;
1258 }
1259 
1271 {
1272  if (s.version() == 1) {
1273  qint16 x1, y1, x2, y2;
1274  s >> x1; s >> y1; s >> x2; s >> y2;
1275  r.setCoords(x1, y1, x2, y2);
1276  }
1277  else {
1278  qint32 x1, y1, x2, y2;
1279  s >> x1; s >> y1; s >> x2; s >> y2;
1280  r.setCoords(x1, y1, x2, y2);
1281  }
1282  return s;
1283 }
1284 
1285 #endif // QT_NO_DATASTREAM
1286 
1287 
1288 #ifndef QT_NO_DEBUG_STREAM
1290 {
1291  QDebugStateSaver saver(dbg);
1292  dbg.nospace();
1293  dbg << "QRect" << '(';
1294  QtDebugUtils::formatQRect(dbg, r);
1295  dbg << ')';
1296  return dbg;
1297 }
1298 #endif
1299 
1440 /*****************************************************************************
1441  QRectF member functions
1442  *****************************************************************************/
1443 
1536 {
1537  QRectF r = *this;
1538  if (r.w < 0) {
1539  r.xp += r.w;
1540  r.w = -r.w;
1541  }
1542  if (r.h < 0) {
1543  r.yp += r.h;
1544  r.h = -r.h;
1545  }
1546  return r;
1547 }
1548 
1960 bool QRectF::contains(const QPointF &p) const noexcept
1961 {
1962  qreal l = xp;
1963  qreal r = xp;
1964  if (w < 0)
1965  l += w;
1966  else
1967  r += w;
1968  if (l == r) // null rect
1969  return false;
1970 
1971  if (p.x() < l || p.x() > r)
1972  return false;
1973 
1974  qreal t = yp;
1975  qreal b = yp;
1976  if (h < 0)
1977  t += h;
1978  else
1979  b += h;
1980  if (t == b) // null rect
1981  return false;
1982 
1983  if (p.y() < t || p.y() > b)
1984  return false;
1985 
1986  return true;
1987 }
1988 
1989 
2006 bool QRectF::contains(const QRectF &r) const noexcept
2007 {
2008  qreal l1 = xp;
2009  qreal r1 = xp;
2010  if (w < 0)
2011  l1 += w;
2012  else
2013  r1 += w;
2014  if (l1 == r1) // null rect
2015  return false;
2016 
2017  qreal l2 = r.xp;
2018  qreal r2 = r.xp;
2019  if (r.w < 0)
2020  l2 += r.w;
2021  else
2022  r2 += r.w;
2023  if (l2 == r2) // null rect
2024  return false;
2025 
2026  if (l2 < l1 || r2 > r1)
2027  return false;
2028 
2029  qreal t1 = yp;
2030  qreal b1 = yp;
2031  if (h < 0)
2032  t1 += h;
2033  else
2034  b1 += h;
2035  if (t1 == b1) // null rect
2036  return false;
2037 
2038  qreal t2 = r.yp;
2039  qreal b2 = r.yp;
2040  if (r.h < 0)
2041  t2 += r.h;
2042  else
2043  b2 += r.h;
2044  if (t2 == b2) // null rect
2045  return false;
2046 
2047  if (t2 < t1 || b2 > b1)
2048  return false;
2049 
2050  return true;
2051 }
2052 
2144 QRectF QRectF::operator|(const QRectF &r) const noexcept
2145 {
2146  if (isNull())
2147  return r;
2148  if (r.isNull())
2149  return *this;
2150 
2151  qreal left = xp;
2152  qreal right = xp;
2153  if (w < 0)
2154  left += w;
2155  else
2156  right += w;
2157 
2158  if (r.w < 0) {
2159  left = qMin(left, r.xp + r.w);
2160  right = qMax(right, r.xp);
2161  } else {
2162  left = qMin(left, r.xp);
2163  right = qMax(right, r.xp + r.w);
2164  }
2165 
2166  qreal top = yp;
2167  qreal bottom = yp;
2168  if (h < 0)
2169  top += h;
2170  else
2171  bottom += h;
2172 
2173  if (r.h < 0) {
2174  top = qMin(top, r.yp + r.h);
2175  bottom = qMax(bottom, r.yp);
2176  } else {
2177  top = qMin(top, r.yp);
2178  bottom = qMax(bottom, r.yp + r.h);
2179  }
2180 
2181  return QRectF(left, top, right - left, bottom - top);
2182 }
2183 
2206 QRectF QRectF::operator&(const QRectF &r) const noexcept
2207 {
2208  qreal l1 = xp;
2209  qreal r1 = xp;
2210  if (w < 0)
2211  l1 += w;
2212  else
2213  r1 += w;
2214  if (l1 == r1) // null rect
2215  return QRectF();
2216 
2217  qreal l2 = r.xp;
2218  qreal r2 = r.xp;
2219  if (r.w < 0)
2220  l2 += r.w;
2221  else
2222  r2 += r.w;
2223  if (l2 == r2) // null rect
2224  return QRectF();
2225 
2226  if (l1 >= r2 || l2 >= r1)
2227  return QRectF();
2228 
2229  qreal t1 = yp;
2230  qreal b1 = yp;
2231  if (h < 0)
2232  t1 += h;
2233  else
2234  b1 += h;
2235  if (t1 == b1) // null rect
2236  return QRectF();
2237 
2238  qreal t2 = r.yp;
2239  qreal b2 = r.yp;
2240  if (r.h < 0)
2241  t2 += r.h;
2242  else
2243  b2 += r.h;
2244  if (t2 == b2) // null rect
2245  return QRectF();
2246 
2247  if (t1 >= b2 || t2 >= b1)
2248  return QRectF();
2249 
2250  QRectF tmp;
2251  tmp.xp = qMax(l1, l2);
2252  tmp.yp = qMax(t1, t2);
2253  tmp.w = qMin(r1, r2) - tmp.xp;
2254  tmp.h = qMin(b1, b2) - tmp.yp;
2255  return tmp;
2256 }
2257 
2284 bool QRectF::intersects(const QRectF &r) const noexcept
2285 {
2286  qreal l1 = xp;
2287  qreal r1 = xp;
2288  if (w < 0)
2289  l1 += w;
2290  else
2291  r1 += w;
2292  if (l1 == r1) // null rect
2293  return false;
2294 
2295  qreal l2 = r.xp;
2296  qreal r2 = r.xp;
2297  if (r.w < 0)
2298  l2 += r.w;
2299  else
2300  r2 += r.w;
2301  if (l2 == r2) // null rect
2302  return false;
2303 
2304  if (l1 >= r2 || l2 >= r1)
2305  return false;
2306 
2307  qreal t1 = yp;
2308  qreal b1 = yp;
2309  if (h < 0)
2310  t1 += h;
2311  else
2312  b1 += h;
2313  if (t1 == b1) // null rect
2314  return false;
2315 
2316  qreal t2 = r.yp;
2317  qreal b2 = r.yp;
2318  if (r.h < 0)
2319  t2 += r.h;
2320  else
2321  b2 += r.h;
2322  if (t2 == b2) // null rect
2323  return false;
2324 
2325  if (t1 >= b2 || t2 >= b1)
2326  return false;
2327 
2328  return true;
2329 }
2330 
2352 {
2353  int xmin = int(qFloor(xp));
2354  int xmax = int(qCeil(xp + w));
2355  int ymin = int(qFloor(yp));
2356  int ymax = int(qCeil(yp + h));
2357  return QRect(xmin, ymin, xmax - xmin, ymax - ymin);
2358 }
2359 
2453 /*****************************************************************************
2454  QRectF stream functions
2455  *****************************************************************************/
2456 #ifndef QT_NO_DATASTREAM
2469 {
2470  s << double(r.x()) << double(r.y()) << double(r.width()) << double(r.height());
2471  return s;
2472 }
2473 
2486 {
2487  double x, y, w, h;
2488  s >> x;
2489  s >> y;
2490  s >> w;
2491  s >> h;
2492  r.setRect(qreal(x), qreal(y), qreal(w), qreal(h));
2493  return s;
2494 }
2495 
2496 #endif // QT_NO_DATASTREAM
2497 
2498 
2499 #ifndef QT_NO_DEBUG_STREAM
2501 {
2502  QDebugStateSaver saver(dbg);
2503  dbg.nospace();
2504  dbg << "QRectF" << '(';
2505  QtDebugUtils::formatQRect(dbg, r);
2506  dbg << ')';
2507  return dbg;
2508 }
2509 #endif
2510 
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:66
operator>>(QDataStream &ds, qfloat16 &f)
Definition: qfloat16.cpp:344
operator<<(QDataStream &ds, qfloat16 f)
Definition: qfloat16.cpp:327
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:65
QDebug & nospace()
Definition: qdebug.h:113
Convenience class for custom QDebug operators.
Definition: qdebug.h:176
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:242
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:52
The QRectF class defines a finite rectangle in the plane using floating point precision.
Definition: qrect.h:511
QRectF operator|(const QRectF &r) const noexcept
Definition: qrect.cpp:2144
QRect toAlignedRect() const noexcept
Definition: qrect.cpp:2351
bool contains(const QRectF &r) const noexcept
Definition: qrect.cpp:2006
bool intersects(const QRectF &r) const noexcept
Definition: qrect.cpp:2284
QRectF normalized() const noexcept
Definition: qrect.cpp:1535
QRectF operator&(const QRectF &r) const noexcept
Definition: qrect.cpp:2206
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:59
bool intersects(const QRect &r) const noexcept
Definition: qrect.cpp:1101
QRect normalized() const noexcept
Definition: qrect.cpp:309
QRect operator&(const QRect &r) const noexcept
Definition: qrect.cpp:1029
bool contains(const QRect &r, bool proper=false) const noexcept
Definition: qrect.cpp:887
QRect operator|(const QRect &r) const noexcept
Definition: qrect.cpp:965
auto it unsigned count const
Definition: hb-iter.hh:848
short qint16
Definition: qglobal.h:285
int qint32
Definition: qglobal.h:287
QT_END_INCLUDE_NAMESPACE typedef double qreal
Definition: qglobal.h:341
int qFloor(T v)
Definition: qmath.h:78
int qCeil(T v)
Definition: qmath.h:72
GLboolean GLboolean GLboolean b
GLint GLint GLint GLint GLint x
[0]
GLboolean r
[2]
GLfloat GLfloat GLfloat w
[0]
GLuint GLfloat GLfloat GLfloat GLfloat y1
GLuint GLfloat GLfloat GLfloat x1
GLdouble GLdouble GLdouble GLdouble top
GLdouble GLdouble right
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
[4]
GLint left
GLint GLint bottom
GLint y
GLfloat GLfloat GLfloat GLfloat h
GLfixed GLfixed GLfixed y2
Definition: qopenglext.h:5231
GLfixed GLfixed x2
Definition: qopenglext.h:5231
GLdouble GLdouble t
[9]
Definition: qopenglext.h:243
GLdouble s
[6]
Definition: qopenglext.h:235
GLfloat GLfloat p
[1]
Definition: qopenglext.h:12698
QRect r1(100, 200, 11, 16)
[0]
QRect r2(QPoint(100, 200), QSize(11, 16))
QSize t2(10, 12)