1 /* 2 This file is part of Kig, a KDE program for Interactive Geometry... 3 SPDX-FileCopyrightText: 2002 Dominique Devriese <devriese@kde.org> 4 5 SPDX-License-Identifier: GPL-2.0-or-later 6 */ 7 8 9 #ifndef RECT_H 10 #define RECT_H 11 12 #include "coordinate.h" 13 14 #include <QRect> 15 #include <QDebug> 16 17 /** 18 * like Coordinate is a QPoint replacement with doubles, this is a 19 * QRect replacement with doubles... 20 */ 21 class Rect 22 { 23 public: 24 /** 25 * constructors... 26 */ 27 Rect( const Coordinate &bottomLeft, const Coordinate &topRight ); 28 Rect( const Coordinate &bottomLeft, const double width, const double height ); 29 Rect( double xa, double ya, double width, double height ); 30 Rect( const Rect& r ); 31 Rect(); 32 static Rect invalidRect(); 33 34 35 bool valid(); 36 37 void setBottomLeft( const Coordinate &p ); 38 void setTopLeft( const Coordinate &p ); 39 void setTopRight( const Coordinate &p ); 40 void setBottomRight( const Coordinate &p ); 41 void setCenter( const Coordinate &p ); 42 void setLeft( const double p); 43 void setRight( const double p); 44 void setTop( const double p ); 45 void setBottom( const double p ); 46 void setWidth( const double w ); 47 void setHeight( const double h ); 48 /** 49 * this makes sure width and height are > 0 ... 50 */ 51 void normalize(); 52 /** 53 * this makes sure p is in the rect, extending it if necessary... 54 */ 55 void setContains( const Coordinate &p ); 56 /** 57 * Assignment operator. 58 */ 59 Rect& operator=( const Rect& other ); 60 /** 61 * moves the rect while keeping the size constant... 62 */ 63 void moveBy( const Coordinate &p ); 64 /** 65 * synonym for moveBy... 66 */ 67 Rect& operator+=( const Coordinate &p ) { moveBy(p); return *this; } 68 /** 69 * scale: only the size changes, topLeft is kept where it is... 70 */ 71 void scale( const double r ); 72 /** 73 * synonym for scale... 74 */ 75 Rect& operator*=( const double r ) { scale(r); return *this; } 76 Rect& operator/=( const double r ) { scale(1/r); return *this; } 77 78 /** 79 * This expands the rect so that it contains r. It has friends 80 * '|=' and '|' below... 81 */ 82 void eat( const Rect& r ); 83 84 /** 85 * synonym for eat. 86 */ 87 Rect& operator|=( const Rect& rhs ) { eat( rhs ); return *this; } 88 89 /** 90 * return a rect which is a copy of this rect, but has an aspect 91 * ratio equal to rhs's one.. if \p shrink is true, the rect will be 92 * shrunk, otherwise extended.. The center of the new rect is the 93 * same as this rect's center.. 94 */ 95 Rect matchShape( const Rect& rhs, bool shrink = false ) const; 96 97 QRect toQRect() const; 98 Coordinate bottomLeft() const; 99 Coordinate bottomRight() const; 100 Coordinate topLeft() const; 101 Coordinate topRight() const; 102 Coordinate center() const; 103 double left() const; 104 double right() const; 105 double bottom() const; 106 double top() const; 107 double width() const; 108 double height() const; 109 bool contains( const Coordinate& p ) const; 110 bool contains( const Coordinate& p, double allowed_miss ) const; 111 bool intersects( const Rect& p ) const; 112 Rect normalized() const; 113 friend QDebug& operator<<( QDebug& s, const Rect& t ); 114 115 static Rect fromQRect( const QRect& ); 116 protected: 117 Coordinate mBottomLeft; 118 double mwidth; 119 double mheight; 120 }; 121 122 bool operator==( const Rect& r, const Rect& s ); 123 QDebug& operator<<( QDebug& s, const Rect& t ); 124 /** 125 * this operator returns a Rect that contains both the given 126 * rects. 127 */ 128 Rect operator|( const Rect& lhs, const Rect& rhs ); 129 130 #endif 131 132