1 /****************************************************************************
2 ** $Id: frect.cpp 23700 2020-05-05 20:27:20Z craig $
3 **
4 ** Implementation of FRect class
5 **
6 ** Created : 931028
7 **
8 ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
9 **
10 ** This file is part of the kernel module of the Qt GUI Toolkit.
11 **
12 ** This file may be distributed under the terms of the Q Public License
13 ** as defined by Trolltech AS of Norway and appearing in the file
14 ** LICENSE.QPL included in the packaging of this file.
15 **
16 ** This file may be distributed and/or modified under the terms of the
17 ** GNU General Public License version 2 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.GPL included in the
19 ** packaging of this file.
20 **
21 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
22 ** licenses may use this file in accordance with the Qt Commercial License
23 ** Agreement provided with the Software.
24 **
25 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 **
28 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
29 **   information about Qt Commercial License Agreements.
30 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
31 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
32 **
33 ** Contact info@trolltech.com if any conditions of this licensing are
34 ** not clear to you.
35 **
36 **********************************************************************/
37 
38 #define	 QRECT_C
39 #include "frect.h"
40 //#include "qdatastream.h"
41 
42 /*!
43 	\class FRect
44 	\brief The FRect class defines a rectangle in the plane.
45 
46 	\ingroup images
47 	\ingroup graphics
48 	\mainclass
49 
50 	A rectangle is internally represented as an upper-left corner and
51 	a bottom-right corner, but it is normally expressed as an
52 	upper-left corner and a size.
53 
54 	The coordinate type is qreal (defined in \c qwindowdefs.h as \c
55 	int). The minimum value of qreal is qreal_MIN (-2147483648) and
56 	the maximum value is  qreal_MAX (2147483647).
57 
58 	Note that the size (width and height) of a rectangle might be
59 	different from what you are used to. If the top-left corner and
60 	the bottom-right corner are the same, the height and the width of
61 	the rectangle will both be 1.
62 
63 	Generally, \e{width = right - left + 1} and \e{height = bottom -
64 	top + 1}. We designed it this way to make it correspond to
65 	rectangular spaces used by drawing functions in which the width
66 	and height denote a number of pixels. For example, drawing a
67 	rectangle with width and height 1 draws a single pixel.
68 
69 	The default coordinate system has origin (0, 0) in the top-left
70 	corner. The positive direction of the y axis is down, and the
71 	positive x axis is from left to right.
72 
73 	A FRect can be constructed with a set of left, top, width and
74 	height integers, from two FPoints or from a FPoint and a FSize.
75 	After creation the dimensions can be changed, e.g. with setLeft(),
76 	setRight(), setTop() and setBottom(), or by setting sizes, e.g.
77 	setWidth(), setHeight() and setSize(). The dimensions can also be
78 	changed with the move functions, e.g. moveBy(), moveCenter(),
79 	moveBottomRight(), etc. You can also add coordinates to a
80 	rectangle with addCoords().
81 
82 	You can test to see if a FRect contains a specific point with
83 	contains(). You can also test to see if two FRects intersect with
84 	intersects() (see also intersect()). To get the bounding rectangle
85 	of two FRects use unite().
86 
87 	\sa FPoint, FSize
88 */
89 
90 
91 /*****************************************************************************
92   FRect member functions
93  *****************************************************************************/
94 
95 /*!
96 	\fn FRect::FRect()
97 
98 	Constructs an invalid rectangle.
99 */
100 
101 /*!
102 	Constructs a rectangle with \a topLeft as the top-left corner and
103 	\a bottomRight as the bottom-right corner.
104 */
105 
FRect(FPoint & topLeft,FPoint & bottomRight)106 FRect::FRect( FPoint &topLeft, FPoint &bottomRight )
107 {
108 	x1 = (qreal)topLeft.x();
109 	y1 = (qreal)topLeft.y();
110 	x2 = (qreal)bottomRight.x();
111 	y2 = (qreal)bottomRight.y();
112 }
113 
114 /*!
115 	Constructs a rectangle with \a topLeft as the top-left corner and
116 	\a size as the rectangle size.
117 */
118 
FRect(FPoint & topLeft,FSize & size)119 FRect::FRect( FPoint &topLeft, FSize &size )
120 {
121 	x1 = (qreal)topLeft.x();
122 	y1 = (qreal)topLeft.y();
123 	x2 = (qreal)(x1+size.width());
124 	y2 = (qreal)(y1+size.height());
125 }
126 
127 /*!
128 	\fn FRect::FRect( qreal left, qreal top, qreal width, qreal height )
129 
130 	Constructs a rectangle with the \a top, \a left corner and \a
131 	width and \a height.
132 
133 	Example (creates three identical rectangles):
134 	\code
135 	FRect r1( FPoint(100,200), FPoint(110,215) );
136 	FRect r2( FPoint(100,200), FSize(11,16) );
137 	FRect r3( 100, 200, 11, 16 );
138 	\endcode
139 */
140 
141 
142 /*!
143 	\fn bool FRect::isNull() const
144 
145 	Returns TRUE if the rectangle is a null rectangle; otherwise
146 	returns FALSE.
147 
148 	A null rectangle has both the width and the height set to 0, that
149 	is right() == left() - 1 and bottom() == top() - 1.
150 
151 	Note that if right() == left() and bottom() == top(), then the
152 	rectangle has width 1 and height 1.
153 
154 	A null rectangle is also empty.
155 
156 	A null rectangle is not valid.
157 
158 	\sa isEmpty(), isValid()
159 */
160 
161 /*!
162 	\fn bool FRect::isEmpty() const
163 
164 	Returns TRUE if the rectangle is empty; otherwise returns FALSE.
165 
166 	An empty rectangle has a left() \> right() or top() \> bottom().
167 
168 	An empty rectangle is not valid. \c{isEmpty() == !isValid()}
169 
170 	\sa isNull(), isValid(), normalize()
171 */
172 
173 /*!
174 	\fn bool FRect::isValid() const
175 
176 	Returns TRUE if the rectangle is valid; otherwise returns FALSE.
177 
178 	A valid rectangle has a left() \<= right() and top() \<= bottom().
179 
180 	Note that non-trivial operations like intersections are not defined
181 	for invalid rectangles.
182 
183 	\c{isValid() == !isEmpty()}
184 
185 	\sa isNull(), isEmpty(), normalize()
186 */
187 
188 
189 /*!
190 	Returns a normalized rectangle, i.e. a rectangle that has a
191 	non-negative width and height.
192 
193 	It swaps left and right if left() \> right(), and swaps top and
194 	bottom if top() \> bottom().
195 
196 	\sa isValid()
197 */
198 
normalize() const199 FRect FRect::normalize() const
200 {
201 	FRect r;
202 	if ( x2 < x1 ) {				// swap bad x values
203 		r.x1 = x2;
204 		r.x2 = x1;
205 	} else {
206 		r.x1 = x1;
207 		r.x2 = x2;
208 	}
209 	if ( y2 < y1 ) {				// swap bad y values
210 		r.y1 = y2;
211 		r.y2 = y1;
212 	} else {
213 		r.y1 = y1;
214 		r.y2 = y2;
215 	}
216 	return r;
217 }
218 
219 
220 /*!
221 	\fn qreal FRect::left() const
222 
223 	Returns the left coordinate of the rectangle. Identical to x().
224 
225 	\sa setLeft(), right(), topLeft(), bottomLeft()
226 */
227 
228 /*!
229 	\fn qreal FRect::top() const
230 
231 	Returns the top coordinate of the rectangle. Identical to y().
232 
233 	\sa setTop(), bottom(), topLeft(), topRight()
234 */
235 
236 /*!
237 	\fn qreal FRect::right() const
238 
239 	Returns the right coordinate of the rectangle.
240 
241 	\sa setRight(), left(), topRight(), bottomRight()
242 */
243 
244 /*!
245 	\fn qreal FRect::bottom() const
246 
247 	Returns the bottom coordinate of the rectangle.
248 
249 	\sa setBottom(), top(), bottomLeft(), bottomRight()
250 */
251 
252 /*!
253 	\fn qreal &FRect::rLeft()
254 
255 	Returns a reference to the left coordinate of the rectangle.
256 
257 	\sa rTop(), rRight(), rBottom()
258 */
259 
260 /*!
261 	\fn qreal &FRect::rTop()
262 
263 	Returns a reference to the top coordinate of the rectangle.
264 
265 	\sa rLeft(),  rRight(), rBottom()
266 */
267 
268 /*!
269 	\fn qreal &FRect::rRight()
270 
271 	Returns a reference to the right coordinate of the rectangle.
272 
273 	\sa rLeft(), rTop(), rBottom()
274 */
275 
276 /*!
277 	\fn qreal &FRect::rBottom()
278 
279 	Returns a reference to the bottom coordinate of the rectangle.
280 
281 	\sa rLeft(), rTop(), rRight()
282 */
283 
284 /*!
285 	\fn qreal FRect::x() const
286 
287 	Returns the left coordinate of the rectangle. Identical to left().
288 
289 	\sa left(), y(), setX()
290 */
291 
292 /*!
293 	\fn qreal FRect::y() const
294 
295 	Returns the top coordinate of the rectangle. Identical to top().
296 
297 	\sa top(), x(), setY()
298 */
299 
300 /*!
301 	\fn void FRect::setLeft( qreal pos )
302 
303 	Sets the left edge of the rectangle to \a pos. May change the
304 	width, but will never change the right edge of the rectangle.
305 
306 	Identical to setX().
307 
308 	\sa left(), setTop(), setWidth()
309 */
310 
311 /*!
312 	\fn void FRect::setTop( qreal pos )
313 
314 	Sets the top edge of the rectangle to \a pos. May change the
315 	height, but will never change the bottom edge of the rectangle.
316 
317 	Identical to setY().
318 
319 	\sa top(), setBottom(), setHeight()
320 */
321 
322 /*!
323 	\fn void FRect::setRight( qreal pos )
324 
325 	Sets the right edge of the rectangle to \a pos. May change the
326 	width, but will never change the left edge of the rectangle.
327 
328 	\sa right(), setLeft(), setWidth()
329 */
330 
331 /*!
332 	\fn void FRect::setBottom( qreal pos )
333 
334 	Sets the bottom edge of the rectangle to \a pos. May change the
335 	height, but will never change the top edge of the rectangle.
336 
337 	\sa bottom(), setTop(), setHeight()
338 */
339 
340 /*!
341 	\fn void FRect::setX( qreal x )
342 
343 	Sets the x position of the rectangle (its left end) to \a x. May
344 	change the width, but will never change the right edge of the
345 	rectangle.
346 
347 	Identical to setLeft().
348 
349 	\sa x(), setY()
350 */
351 
352 /*!
353 	\fn void FRect::setY( qreal y )
354 
355 	Sets the y position of the rectangle (its top) to \a y. May change
356 	the height, but will never change the bottom edge of the
357 	rectangle.
358 
359 	Identical to setTop().
360 
361 	\sa y(), setX()
362 */
363 
364 /*!
365 	Set the top-left corner of the rectangle to \a p. May change
366 	the size, but will the never change the bottom-right corner of
367 	the rectangle.
368 
369 	\sa topLeft(), moveTopLeft(), setBottomRight(), setTopRight(), setBottomLeft()
370 */
setTopLeft(FPoint & p)371 void FRect::setTopLeft( FPoint &p )
372 {
373 	setLeft( p.x() );
374 	setTop( p.y() );
375 }
376 
377 /*!
378 	Set the bottom-right corner of the rectangle to \a p. May change
379 	the size, but will the never change the top-left corner of
380 	the rectangle.
381 
382 	\sa bottomRight(), moveBottomRight(), setTopLeft(), setTopRight(), setBottomLeft()
383 */
setBottomRight(FPoint & p)384 void FRect::setBottomRight( FPoint &p )
385 {
386 	setRight( p.x() );
387 	setBottom( p.y() );
388 }
389 
390 /*!
391 	Set the top-right corner of the rectangle to \a p. May change
392 	the size, but will the never change the bottom-left corner of
393 	the rectangle.
394 
395 	\sa topRight(), moveTopRight(), setTopLeft(), setBottomRight(), setBottomLeft()
396 */
setTopRight(FPoint & p)397 void FRect::setTopRight( FPoint &p )
398 {
399 	setRight( p.x() );
400 	setTop( p.y() );
401 }
402 
403 /*!
404 	Set the bottom-left corner of the rectangle to \a p. May change
405 	the size, but will the never change the top-right corner of
406 	the rectangle.
407 
408 	\sa bottomLeft(), moveBottomLeft(), setTopLeft(), setBottomRight(), setTopRight()
409 */
setBottomLeft(FPoint & p)410 void FRect::setBottomLeft( FPoint &p )
411 {
412 	setLeft( p.x() );
413 	setBottom( p.y() );
414 }
415 
416 /*!
417 	\fn FPoint FRect::topLeft() const
418 
419 	Returns the top-left position of the rectangle.
420 
421 	\sa setTopLeft(), moveTopLeft(), bottomRight(), left(), top()
422 */
423 
424 /*!
425 	\fn FPoint FRect::bottomRight() const
426 
427 	Returns the bottom-right position of the rectangle.
428 
429 	\sa setBottomRight(), moveBottomRight(), topLeft(), right(), bottom()
430 */
431 
432 /*!
433 	\fn FPoint FRect::topRight() const
434 
435 	Returns the top-right position of the rectangle.
436 
437 	\sa setTopRight(), moveTopRight(), bottomLeft(), top(), right()
438 */
439 
440 /*!
441 	\fn FPoint FRect::bottomLeft() const
442 
443 	Returns the bottom-left position of the rectangle.
444 
445 	\sa setBottomLeft(), moveBottomLeft(), topRight(), bottom(), left()
446 */
447 
448 /*!
449 	\fn FPoint FRect::center() const
450 
451 	Returns the center point of the rectangle.
452 
453 	\sa moveCenter(), topLeft(), bottomRight(), topRight(), bottomLeft()
454 */
455 
456 
457 /*!
458 	Extracts the rectangle parameters as the position \a *x, \a *y and
459 	width \a *w and height \a *h.
460 
461 	\sa setRect(), coords()
462 */
463 
rect(qreal * x,qreal * y,qreal * w,qreal * h) const464 void FRect::rect( qreal *x, qreal *y, qreal *w, qreal *h ) const
465 {
466 	*x = x1;
467 	*y = y1;
468 	*w = x2-x1;
469 	*h = y2-y1;
470 }
471 
472 /*!
473 	Extracts the rectangle parameters as the top-left point \a *xp1,
474 	\a *yp1 and the bottom-right point \a *xp2, \a *yp2.
475 
476 	\sa setCoords(), rect()
477 */
478 
coords(qreal * xp1,qreal * yp1,qreal * xp2,qreal * yp2) const479 void FRect::coords( qreal *xp1, qreal *yp1, qreal *xp2, qreal *yp2 ) const
480 {
481 	*xp1 = x1;
482 	*yp1 = y1;
483 	*xp2 = x2;
484 	*yp2 = y2;
485 }
486 
487 
488 /*!
489 	Sets the left position of the rectangle to \a pos, leaving the
490 	size unchanged.
491 
492 	\sa left(), setLeft(), moveTop(), moveRight(), moveBottom()
493 */
moveLeft(qreal pos)494 void FRect::moveLeft( qreal pos )
495 {
496 	x2 += (qreal)(pos - x1);
497 	x1 = (qreal)pos;
498 }
499 
500 /*!
501 	Sets the top position of the rectangle to \a pos, leaving the
502 	size unchanged.
503 
504 	\sa top(), setTop(), moveLeft(), moveRight(), moveBottom()
505 */
506 
moveTop(qreal pos)507 void FRect::moveTop( qreal pos )
508 {
509 	y2 += (qreal)(pos - y1);
510 	y1 = (qreal)pos;
511 }
512 
513 /*!
514 	Sets the right position of the rectangle to \a pos, leaving the
515 	size unchanged.
516 
517 	\sa right(), setRight(), moveLeft(), moveTop(), moveBottom()
518 */
519 
moveRight(qreal pos)520 void FRect::moveRight( qreal pos )
521 {
522 	x1 += (qreal)(pos - x2);
523 	x2 = (qreal)pos;
524 }
525 
526 /*!
527 	Sets the bottom position of the rectangle to \a pos, leaving the
528 	size unchanged.
529 
530 	\sa bottom(), setBottom(), moveLeft(), moveTop(), moveRight()
531 */
532 
moveBottom(qreal pos)533 void FRect::moveBottom( qreal pos )
534 {
535 	y1 += (qreal)(pos - y2);
536 	y2 = (qreal)pos;
537 }
538 
539 /*!
540 	Sets the top-left position of the rectangle to \a p, leaving the
541 	size unchanged.
542 
543 	\sa topLeft(), setTopLeft(), moveBottomRight(), moveTopRight(), moveBottomLeft()
544 */
545 
moveTopLeft(FPoint & p)546 void FRect::moveTopLeft( FPoint &p )
547 {
548 	moveLeft( p.x() );
549 	moveTop( p.y() );
550 }
551 
552 /*!
553 	Sets the bottom-right position of the rectangle to \a p, leaving
554 	the size unchanged.
555 
556 	\sa bottomRight(), setBottomRight(), moveTopLeft(), moveTopRight(), moveBottomLeft()
557 */
558 
moveBottomRight(FPoint & p)559 void FRect::moveBottomRight( FPoint &p )
560 {
561 	moveRight( p.x() );
562 	moveBottom( p.y() );
563 }
564 
565 /*!
566 	Sets the top-right position of the rectangle to \a p, leaving the
567 	size unchanged.
568 
569 	\sa topRight(), setTopRight(), moveTopLeft(), moveBottomRight(), moveBottomLeft()
570 */
571 
moveTopRight(FPoint & p)572 void FRect::moveTopRight( FPoint &p )
573 {
574 	moveRight( p.x() );
575 	moveTop( p.y() );
576 }
577 
578 /*!
579 	Sets the bottom-left position of the rectangle to \a p, leaving
580 	the size unchanged.
581 
582 	\sa bottomLeft(), setBottomLeft(), moveTopLeft(), moveBottomRight(), moveTopRight()
583 */
584 
moveBottomLeft(FPoint & p)585 void FRect::moveBottomLeft( FPoint &p )
586 {
587 	moveLeft( p.x() );
588 	moveBottom( p.y() );
589 }
590 
591 
592 /*!
593 	Sets the center point of the rectangle to \a p, leaving the size
594 	unchanged.
595 
596 	\sa center(), moveTopLeft(), moveBottomRight(), moveTopRight(), moveBottomLeft()
597 */
598 
moveCenter(FPoint & p)599 void FRect::moveCenter( FPoint &p )
600 {
601 	qreal w = x2 - x1;
602 	qreal h = y2 - y1;
603 	x1 = (qreal)(p.x() - w/2);
604 	y1 = (qreal)(p.y() - h/2);
605 	x2 = x1 + w;
606 	y2 = y1 + h;
607 }
608 
609 
610 /*!
611 	Moves the rectangle \a dx along the x axis and \a dy along the y
612 	axis, relative to the current position. Positive values move the
613 	rectangle to the right and down.
614 
615 	\sa moveTopLeft()
616 */
617 
moveBy(qreal dx,qreal dy)618 void FRect::moveBy( qreal dx, qreal dy )
619 {
620 	x1 += (qreal)dx;
621 	y1 -= (qreal)dy;
622 	x2 += (qreal)dx;
623 	y2 -= (qreal)dy;
624 }
625 
626 /*!
627 	Sets the coordinates of the rectangle's top-left corner to \a (x,
628 	y), and its size to \a (w, h).
629 
630 	\sa rect(), setCoords()
631 */
632 
setRect(qreal x,qreal y,qreal w,qreal h)633 void FRect::setRect( qreal x, qreal y, qreal w, qreal h )
634 {
635 	x1 = (qreal)x;
636 	y1 = (qreal)y;
637 	x2 = (qreal)(x+w);
638 	y2 = (qreal)(y+h);
639 }
640 
641 /*!
642 	Sets the coordinates of the rectangle's top-left corner to \a
643 	(xp1, yp1), and the coordinates of its bottom-right corner to \a
644 	(xp2, yp2).
645 
646 	\sa coords(), setRect()
647 */
648 
setCoords(qreal xp1,qreal yp1,qreal xp2,qreal yp2)649 void FRect::setCoords(qreal xp1, qreal yp1, qreal xp2, qreal yp2 )
650 {
651 	x1 = (qreal)xp1;
652 	y1 = (qreal)yp1;
653 	x2 = (qreal)xp2;
654 	y2 = (qreal)yp2;
655 }
656 
657 /*!
658 	Adds \a xp1, \a yp1, \a xp2 and \a yp2 respectively to the
659 	existing coordinates of the rectangle.
660 */
661 
addCoords(qreal xp1,qreal yp1,qreal xp2,qreal yp2)662 void FRect::addCoords( qreal xp1, qreal yp1, qreal xp2, qreal yp2 )
663 {
664 	x1 += (qreal)xp1;
665 	y1 += (qreal)yp1;
666 	x2 += (qreal)xp2;
667 	y2 += (qreal)yp2;
668 }
669 
670 /*!
671 	\fn FSize FRect::size() const
672 
673 	Returns the size of the rectangle.
674 
675 	\sa width(), height()
676 */
677 
678 /*!
679 	\fn qreal FRect::width() const
680 
681 	Returns the width of the rectangle. The width includes both the
682 	left and right edges, i.e. width = right - left + 1.
683 
684 	\sa height(), size(), setHeight()
685 */
686 
687 /*!
688 	\fn qreal FRect::height() const
689 
690 	Returns the height of the rectangle. The height includes both the
691 	top and bottom edges, i.e. height = bottom - top + 1.
692 
693 	\sa width(), size(), setHeight()
694 */
695 
696 /*!
697 	Sets the width of the rectangle to \a w. The right edge is
698 	changed, but not the left edge.
699 
700 	\sa width(), setLeft(), setRight(), setSize()
701 */
702 
setWidth(qreal w)703 void FRect::setWidth( qreal w )
704 {
705 	x2 = (qreal)(x1 + w);
706 }
707 
708 /*!
709 	Sets the height of the rectangle to \a h. The top edge is not
710 	moved, but the bottom edge may be moved.
711 
712 	\sa height(), setTop(), setBottom(), setSize()
713 */
714 
setHeight(qreal h)715 void FRect::setHeight( qreal h )
716 {
717 	y2 = (qreal)(y1 + h);
718 }
719 
720 /*!
721 	Sets the size of the rectangle to \a s. The top-left corner is not
722 	moved.
723 
724 	\sa size(), setWidth(), setHeight()
725 */
726 
setSize(const FSize & s)727 void FRect::setSize( const FSize &s )
728 {
729 	x2 = (qreal)(s.width() +x1);
730 	y2 = (qreal)(s.height()+y1);
731 }
732 
733 /*!
734 	Returns TRUE if the point \a p is inside or on the edge of the
735 	rectangle; otherwise returns FALSE.
736 
737 	If \a proper is TRUE, this function returns TRUE only if \a p is
738 	inside (not on the edge).
739 */
740 
contains(FPoint & p,bool proper) const741 bool FRect::contains( FPoint &p, bool proper ) const
742 {
743 	if (proper)
744 		return p.x() > x1 && p.x() < x2 &&
745 				p.y() > y1 && p.y() < y2;
746 	return p.x() >= x1 && p.x() <= x2 &&
747 			p.y() >= y1 && p.y() <= y2;
748 }
749 
750 /*!
751 	\overload bool FRect::contains( qreal x, qreal y, bool proper ) const
752 
753 	Returns TRUE if the point \a x, \a y is inside this rectangle;
754 	otherwise returns FALSE.
755 
756 	If \a proper is TRUE, this function returns TRUE only if the point
757 	is entirely inside (not on the edge).
758 */
759 
760 /*!
761 	\overload bool FRect::contains( qreal x, qreal y ) const
762 
763 	Returns TRUE if the point \a x, \a y is inside this rectangle;
764 	otherwise returns FALSE.
765 */
766 
767 /*!
768 	\overload
769 
770 	Returns TRUE if the rectangle \a r is inside this rectangle;
771 	otherwise returns FALSE.
772 
773 	If \a proper is TRUE, this function returns TRUE only if \a r is
774 	entirely inside (not on the edge).
775 
776 	\sa unite(), intersect(), intersects()
777 */
778 
contains(const FRect & r,bool proper) const779 bool FRect::contains( const FRect &r, bool proper ) const
780 {
781 	if (proper)
782 		return r.x1 > x1 && r.x2 < x2 && r.y1 > y1 && r.y2 < y2;
783 	return r.x1 >= x1 && r.x2 <= x2 && r.y1 >= y1 && r.y2 <= y2;
784 }
785 
786 /*!
787 	Unites this rectangle with rectangle \a r.
788 */
operator |=(const FRect & r)789 FRect& FRect::operator|=(const FRect &r)
790 {
791 	*this = *this | r;
792 	return *this;
793 }
794 
795 /*!
796 	Intersects this rectangle with rectangle \a r.
797 */
operator &=(const FRect & r)798 FRect& FRect::operator&=(const FRect &r)
799 {
800 	*this = *this & r;
801 	return *this;
802 }
803 
804 
805 /*!
806 	Returns the bounding rectangle of this rectangle and rectangle \a
807 	r.
808 
809 	The bounding rectangle of a nonempty rectangle and an empty or
810 	invalid rectangle is defined to be the nonempty rectangle.
811 
812 	\sa operator|=(), operator&(), intersects(), contains()
813 */
814 
operator |(const FRect & r) const815 FRect FRect::operator|(const FRect &r) const
816 {
817 	if (!isValid())
818 		return r;
819 	if (r.isValid())
820 	{
821 		FRect tmp;
822 		tmp.setLeft(   qMin( x1, r.x1 ) );
823 		tmp.setRight(  qMax( x2, r.x2 ) );
824 		tmp.setTop(	   qMin( y1, r.y1 ) );
825 		tmp.setBottom( qMax( y2, r.y2 ) );
826 		return tmp;
827 	}
828 	return *this;
829 }
830 
831 /*!
832 	Returns the bounding rectangle of this rectangle and rectangle \a
833 	r. \c{r.unite(s)} is equivalent to \c{r|s}.
834 */
unite(const FRect & r) const835 FRect FRect::unite( const FRect &r ) const
836 {
837 	return *this | r;
838 }
839 
840 
841 /*!
842 	Returns the intersection of this rectangle and rectangle \a r.
843 
844 	Returns an empty rectangle if there is no intersection.
845 
846 	\sa operator&=(), operator|(), isEmpty(), intersects(), contains()
847 */
848 
operator &(const FRect & r) const849 FRect FRect::operator&( const FRect &r ) const
850 {
851 	FRect tmp;
852 	tmp.x1 = qMax( x1, r.x1 );
853 	tmp.x2 = qMin( x2, r.x2 );
854 	tmp.y1 = qMax( y1, r.y1 );
855 	tmp.y2 = qMin( y2, r.y2 );
856 	return tmp;
857 }
858 
859 /*!
860 	Returns the intersection of this rectangle and rectangle \a r.
861 	\c{r.intersect(s)} is equivalent to \c{r&s}.
862 */
intersect(const FRect & r) const863 FRect FRect::intersect( const FRect &r ) const
864 {
865 	return *this & r;
866 }
867 
868 /*!
869 	Returns TRUE if this rectangle intersects with rectangle \a r
870 	(there is at least one pixel that is within both rectangles);
871 	otherwise returns FALSE.
872 
873 	\sa intersect(), contains()
874 */
875 
intersects(const FRect & r) const876 bool FRect::intersects( const FRect &r ) const
877 {
878 	return ( qMax( x1, r.x1 ) <= qMin( x2, r.x2 ) &&
879 			 qMax( y1, r.y1 ) <= qMin( y2, r.y2 ) );
880 }
881 
882 
883 /*!
884 	\relates FRect
885 
886 	Returns TRUE if \a r1 and \a r2 are equal; otherwise returns FALSE.
887 */
888 
operator ==(const FRect & r1,const FRect & r2)889 bool operator==( const FRect &r1, const FRect &r2 )
890 {
891 	return r1.x1==r2.x1 && r1.x2==r2.x2 && r1.y1==r2.y1 && r1.y2==r2.y2;
892 }
893 
894 /*!
895 	\relates FRect
896 
897 	Returns TRUE if \a r1 and \a r2 are different; otherwise returns FALSE.
898 */
899 
operator !=(const FRect & r1,const FRect & r2)900 bool operator!=( const FRect &r1, const FRect &r2 )
901 {
902 	return r1.x1!=r2.x1 || r1.x2!=r2.x2 || r1.y1!=r2.y1 || r1.y2!=r2.y2;
903 }
904 
905 
906 // /*****************************************************************************
907 //   FRect stream functions
908 //  *****************************************************************************/
909 // #ifndef QT_NO_DATASTREAM
910 // /*!
911 //     \relates FRect
912 //
913 //     Writes the FRect, \a r, to the stream \a s, and returns a
914 //     reference to the stream.
915 //
916 //     \sa \link datastreamformat.html Format of the QDataStream operators \endlink
917 // */
918 //
919 // QDataStream &operator<<( QDataStream &s, const FRect &r )
920 // {
921 //     if ( s.version() == 1 )
922 // 	s << (Q_INT16)r.left() << (Q_INT16)r.top()
923 // 	  << (Q_INT16)r.right() << (Q_INT16)r.bottom();
924 //     else
925 // 	s << (Q_INT32)r.left() << (Q_INT32)r.top()
926 // 	  << (Q_INT32)r.right() << (Q_INT32)r.bottom();
927 //     return s;
928 // }
929 //
930 // /*!
931 //     \relates FRect
932 //
933 //     Reads a FRect from the stream \a s into rect \a r and returns a
934 //     reference to the stream.
935 //
936 //     \sa \link datastreamformat.html Format of the QDataStream operators \endlink
937 // */
938 //
939 // QDataStream &operator>>( QDataStream &s, FRect &r )
940 // {
941 //     if ( s.version() == 1 ) {
942 // 	Q_INT16 x1, y1, x2, y2;
943 // 	s >> x1; s >> y1; s >> x2; s >> y2;
944 // 	r.setCoords( x1, y1, x2, y2 );
945 //     }
946 //     else {
947 // 	Q_INT32 x1, y1, x2, y2;
948 // 	s >> x1; s >> y1; s >> x2; s >> y2;
949 // 	r.setCoords( x1, y1, x2, y2 );
950 //     }
951 //     return s;
952 // }
953 // #endif // QT_NO_DATASTREAM
954