lib

KoRect.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001 David Faure <faure@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2, as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 * Boston, MA 02110-1301, USA.
17*/
18
19#include "KoRect.h"
20
21KoRect KoRect::normalize() const
22{
23 KoRect r;
24 if ( right() < left() ) { // swap bad x values
25 r.m_tl.setX( right() );
26 r.m_br.setX( left() );
27 } else {
28 r.m_tl.setX( left() );
29 r.m_br.setX( right() );
30 }
31 if ( bottom() < top() ) { // swap bad y values
32 r.m_tl.setY( bottom() );
33 r.m_br.setY( top() );
34 } else {
35 r.m_tl.setY( top() );
36 r.m_br.setY( bottom() );
37 }
38 return r;
39}
40
41void KoRect::setTopLeft(const KoPoint &topleft)
42{
43 m_tl = topleft;
44}
45
46void KoRect::setBottomRight(const KoPoint &bottomright)
47{
48 m_br = bottomright;
49}
50
51void KoRect::setTopRight(const KoPoint &topright)
52{
53 m_br.rx() = topright.x();
54 m_tl.ry() = topright.y();
55}
56
57void KoRect::setBottomLeft(const KoPoint &bottomleft)
58{
59 m_tl.rx() = bottomleft.x();
60 m_br.ry() = bottomleft.y();
61}
62
63
64KoPoint KoRect::center() const
65{
66 return KoPoint((left() + right()) / 2, (top() + bottom()) / 2);
67}
68
69void KoRect::moveTopLeft(const KoPoint &topleft)
70{
71 m_br.rx() += topleft.x() - m_tl.x();
72 m_br.ry() += topleft.y() - m_tl.y();
73 m_tl = topleft;
74}
75
76void KoRect::moveBottomRight(const KoPoint &bottomright)
77{
78 m_tl.rx() += bottomright.x() - m_br.x();
79 m_tl.ry() += bottomright.y() - m_br.y();
80 m_br = bottomright;
81}
82
83void KoRect::moveTopRight(const KoPoint &topright)
84{
85 m_tl.rx() += topright.x() - m_br.x();
86 m_br.ry() += topright.y() - m_tl.y();
87 m_br.rx() = topright.x();
88 m_tl.ry() = topright.y();
89}
90
91void KoRect::moveBottomLeft(const KoPoint &bottomleft)
92{
93 m_br.rx() += bottomleft.x() - m_tl.x();
94 m_tl.ry() += bottomleft.y() - m_br.y();
95 m_tl.rx() = bottomleft.x();
96 m_br.ry() = bottomleft.y();
97}
98
99void KoRect::moveBy(const double &dx, const double &dy)
100{
101 m_tl.rx() += dx;
102 m_tl.ry() += dy;
103 m_br.rx() += dx;
104 m_br.ry() += dy;
105}
106
107void KoRect::setRect(const double &x, const double &y, const double &width, const double &height)
108{
109 m_tl.setCoords( x, y );
110 m_br.setCoords( x + width, y + height );
111}
112
113void KoRect::setRect(const KoRect &rect)
114{
115 m_tl = rect.m_tl;
116 m_br = rect.m_br;
117}
118
119void KoRect::setCoords(const double &x1, const double &y1, const double &x2, const double &y2)
120{
121 m_tl.setCoords( x1, y1 );
122 m_br.setCoords( x2, y2 );
123}
124
125void KoRect::setSize(const KoSize &size)
126{
127 setWidth(size.width());
128 setHeight(size.height());
129}
130
131KoSize KoRect::size() const
132{
133 return KoSize(width(), height());
134}
135
136KoRect &KoRect::operator|=(const KoRect &rhs) {
137
138 if(rhs.isEmpty())
139 return *this;
140 if(isEmpty())
141 {
142 *this = rhs;
143 return *this;
144 }
145 if(m_tl.x() > rhs.left())
146 m_tl.setX(rhs.left());
147 if(m_tl.y() > rhs.top())
148 m_tl.setY(rhs.top());
149 if(m_br.x() < rhs.right())
150 m_br.setX(rhs.right());
151 if(m_br.y() < rhs.bottom())
152 m_br.setY(rhs.bottom());
153 return *this;
154}
155
156KoRect &KoRect::operator&=(const KoRect &rhs) {
157
158 if(m_tl.x() < rhs.left())
159 m_tl.setX(rhs.left());
160 if(m_tl.y() < rhs.top())
161 m_tl.setY(rhs.top());
162 if(m_br.x() > rhs.right())
163 m_br.setX(rhs.right());
164 if(m_br.y() > rhs.bottom())
165 m_br.setY(rhs.bottom());
166 return *this;
167}
168
169bool KoRect::contains(const KoPoint &p) const {
170 return (p.x() >= m_tl.x() && p.x() <= m_br.x() && p.y() >= m_tl.y() && p.y() <= m_br.y());
171}
172
173bool KoRect::contains(const double &x, const double &y) const {
174 return (x >= m_tl.x() && x <= m_br.x() && y >= m_tl.y() && y <= m_br.y());
175}
176
177bool KoRect::contains(const KoRect &r) const {
178 return (r.left() >= m_tl.x() && r.right() <= m_br.x() && r.top() >= m_tl.y() && r.bottom() <= m_br.y());
179}
180
181
182KoRect KoRect::unite(const KoRect &r) const {
183 return *this | r;
184}
185
186KoRect KoRect::intersect(const KoRect &r) const {
187 return *this & r;
188}
189
190bool KoRect::intersects(const KoRect &r) const {
191 return ( TQMAX(m_tl.x(), r.left()) <= TQMIN(m_br.x(), r.right()) &&
192 TQMAX(m_tl.y(), r.top()) <= TQMIN(m_br.y(), r.bottom()) );
193}
194
195KoRect operator|(const KoRect &lhs, const KoRect &rhs) {
196
197 if(lhs.isEmpty())
198 return rhs;
199 if(rhs.isEmpty())
200 return lhs;
201 KoRect tmp;
202 tmp.setCoords( (lhs.left() < rhs.left() ? lhs.left() : rhs.left()),
203 (lhs.top() < rhs.top() ? lhs.top() : rhs.top()),
204 (lhs.right() > rhs.right() ? lhs.right() : rhs.right()),
205 (lhs.bottom() > rhs.bottom() ? lhs.bottom() : rhs.bottom()) );
206 return tmp;
207}
208
209KoRect operator&(const KoRect &lhs, const KoRect &rhs) {
210
211 KoRect tmp;
212 tmp.setCoords( (lhs.left() > rhs.left() ? lhs.left() : rhs.left()),
213 (lhs.top() > rhs.top() ? lhs.top() : rhs.top()),
214 (lhs.right() < rhs.right() ? lhs.right() : rhs.right()),
215 (lhs.bottom() < rhs.bottom() ? lhs.bottom() : rhs.bottom()) );
216 return tmp;
217}
218
219bool operator==(const KoRect &lhs, const KoRect &rhs) {
220 return ( lhs.topLeft()==rhs.topLeft() &&
221 lhs.bottomRight()==rhs.bottomRight() );
222}
223
224bool operator!=(const KoRect &lhs, const KoRect &rhs) {
225 return ( lhs.topLeft()!=rhs.topLeft() ||
226 lhs.bottomRight()!=rhs.bottomRight() );
227}
228
229KoRect KoRect::transform(const TQWMatrix &m) const
230{
231 KoRect result;
232 if(m.m12() == 0.0F && m.m21() == 0.0F)
233 {
234 result = KoRect(topLeft().transform(m), bottomRight().transform(m));
235 }
236 else
237 {
238 int i;
239 KoPoint p[4] = { KoPoint(m_tl.x(), m_tl.y()), KoPoint(m_tl.x(), m_br.x()),
240 KoPoint(m_br.x(), m_br.x()), KoPoint(m_br.x(), m_tl.y()) };
241 for(i = 0; i < 4; i++)
242 p[i] = p[i].transform(m);
243
244 result.setLeft(p[0].x());
245 result.setTop(p[0].y());
246 result.setRight(p[0].x());
247 result.setBottom(p[0].y());
248
249 for(int i = 1; i < 4; i++)
250 {
251 result.setLeft(TQMIN(p[i].x(), result.left()));
252 result.setTop(TQMIN(p[i].y(), result.top()));
253 result.setRight(TQMAX(p[i].x(), result.right()));
254 result.setBottom(TQMAX(p[i].y(), result.bottom()));
255 }
256 }
257 return result;
258}
259
260KoRect KoRect::translate(double dx, double dy) const
261{
262 return KoRect(left() + dx, top() + dy, width(), height());
263}
264
265TQRect KoRect::toTQRect() const
266{
267 return TQRect( tqRound( left() ), tqRound( top() ), tqRound( width() ), tqRound( height() ) );
268}
269
270//static
271KoRect KoRect::fromTQRect( const TQRect &rect )
272{
273 return KoRect( rect.left(), rect.top(), rect.width(), rect.height() );
274}
A point whose coordinates are floating-point values ( "double"s ).
Definition KoPoint.h:30
A rect whose coordinates are floating-point values ( "double"s ).
Definition KoRect.h:31
bool contains(const KoPoint &p) const
Returns if the point is contained in the rect.
Definition KoRect.cpp:169
A size whose coordinates are floating-point values ( "double"s ).
Definition KoSize.h:34