• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kig

  • sources
  • kde-4.12
  • kdeedu
  • kig
  • misc
rect.cc
Go to the documentation of this file.
1 // Copyright (C) 2002 Dominique Devriese <devriese@kde.org>
2 
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License
5 // as published by the Free Software Foundation; either version 2
6 // of the License, or (at your option) any later version.
7 
8 // This program 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
11 // GNU General Public License for more details.
12 
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 // 02110-1301, USA.
17 
18 #include "rect.h"
19 #include "common.h"
20 
21 bool operator==( const Rect& r, const Rect& s )
22 {
23  return ( r.bottomLeft() == s.bottomLeft()
24  && r.width() == s.width()
25  && r.height() == s.height() );
26 }
27 
28 kdbgstream& operator<<( kdbgstream& s, const Rect& t )
29 {
30  s << "left: " << t.left()
31  << "bottom: " << t.bottom()
32  << "right: " << t.right()
33  << "top: " << t.top()
34  << endl;
35  return s;
36 }
37 
38 Rect::Rect( const Coordinate bottomLeft, const Coordinate topRight )
39  : mBottomLeft(bottomLeft)
40 {
41  mwidth = topRight.x - bottomLeft.x;
42  mheight = topRight.y - bottomLeft.y;
43  normalize();
44 }
45 
46 Rect::Rect( const Coordinate p, const double width, const double height )
47  : mBottomLeft(p),
48  mwidth(width),
49  mheight(height)
50 {
51  normalize();
52 }
53 
54 Rect::Rect( double xa, double ya, double width, double height )
55  : mBottomLeft( xa, ya ),
56  mwidth( width ),
57  mheight( height )
58 {
59  normalize();
60 }
61 
62 Rect::Rect( const Rect& r )
63  : mBottomLeft (r.mBottomLeft),
64  mwidth(r.mwidth),
65  mheight(r.mheight)
66 {
67  normalize();
68 }
69 
70 Rect::Rect()
71  : mwidth(0),
72  mheight(0)
73 {
74 }
75 
76 void Rect::setBottomLeft( const Coordinate p )
77 {
78  mBottomLeft = p;
79 }
80 
81 void Rect::setBottomRight( const Coordinate p )
82 {
83  mBottomLeft = p - Coordinate(mwidth,0);
84 }
85 
86 void Rect::setTopRight( const Coordinate p )
87 {
88  mBottomLeft = p - Coordinate(mwidth, mheight);
89 }
90 
91 void Rect::setCenter( const Coordinate p )
92 {
93  mBottomLeft = p - Coordinate(mwidth, mheight)/2;
94 }
95 
96 void Rect::setLeft( const double p )
97 {
98  double r = right();
99  mBottomLeft.x = p;
100  setRight( r );
101 }
102 
103 void Rect::setRight( const double p )
104 {
105  mwidth = p - left();
106 }
107 
108 void Rect::setBottom( const double p )
109 {
110  double t = top();
111  mBottomLeft.y = p;
112  setTop( t );
113 }
114 
115 void Rect::setTop( const double p )
116 {
117  mheight = p - bottom();
118 }
119 
120 void Rect::setWidth( const double w )
121 {
122  mwidth = w;
123 }
124 
125 void Rect::setHeight( const double h )
126 {
127  mheight = h;
128 }
129 
130 void Rect::normalize()
131 {
132  if ( mwidth < 0 )
133  {
134  mBottomLeft.x += mwidth;
135  mwidth = -mwidth;
136  };
137  if ( mheight < 0 )
138  {
139  mBottomLeft.y += mheight;
140  mheight = -mheight;
141  };
142 }
143 
144 void Rect::moveBy( const Coordinate p )
145 {
146  mBottomLeft += p;
147 }
148 
149 void Rect::scale( const double r )
150 {
151  mwidth *= r;
152  mheight *= r;
153 }
154 
155 
156 QRect Rect::toQRect() const
157 {
158  return QRect(mBottomLeft.toQPoint(), topRight().toQPoint());
159 }
160 
161 Coordinate Rect::bottomLeft() const
162 {
163  return mBottomLeft;
164 }
165 
166 Coordinate Rect::bottomRight() const
167 {
168  return mBottomLeft + Coordinate(mwidth, 0);
169 }
170 
171 Coordinate Rect::topLeft() const
172 {
173  return mBottomLeft + Coordinate(0, mheight);
174 }
175 
176 Coordinate Rect::topRight() const
177 {
178  return mBottomLeft + Coordinate(mwidth, mheight);
179 }
180 
181 Coordinate Rect::center() const
182 {
183  return mBottomLeft + Coordinate(mwidth, mheight)/2;
184 }
185 
186 double Rect::left() const
187 {
188  return mBottomLeft.x;
189 }
190 double Rect::right() const
191 {
192  return left() + mwidth;
193 }
194 double Rect::bottom() const
195 {
196  return mBottomLeft.y;
197 }
198 
199 double Rect::top() const
200 {
201  return bottom() + mheight;
202 }
203 
204 double Rect::width() const
205 {
206  return mwidth;
207 }
208 
209 double Rect::height() const
210 {
211  return mheight;
212 }
213 
214 bool Rect::contains( const Coordinate& p, double allowed_miss ) const
215 {
216  return p.x - left() >= - allowed_miss &&
217  p.y - bottom() >= - allowed_miss &&
218  p.x - left() - width() <= allowed_miss &&
219  p.y - bottom() - height() <= allowed_miss;
220 }
221 
222 bool Rect::contains( const Coordinate& p ) const
223 {
224  return p.x >= left() &&
225  p.y >= bottom() &&
226  p.x - left() <= width() &&
227  p.y - bottom() <= height();
228 }
229 
230 bool Rect::intersects( const Rect& p ) const
231 {
232  // never thought it was this simple :)
233  if( p.left() < left() && p.right() < left()) return false;
234  if( p.left() > right() && p.right() > right()) return false;
235  if( p.bottom() < bottom() && p.top() < bottom()) return false;
236  if( p.bottom() > top() && p.top() > top()) return false;
237  return true;
238 }
239 
240 void Rect::setContains( Coordinate p )
241 {
242  normalize();
243  if( p.x < left() ) setLeft( p.x );
244  if( p.x > right() ) setRight(p.x);
245  if( p.y < bottom() ) setBottom( p.y );
246  if( p.y > top() ) setTop( p.y );
247 }
248 
249 Rect Rect::normalized() const
250 {
251  Rect t = *this;
252  (void) t.normalize();
253  return t;
254 }
255 
256 Rect Rect::fromQRect( const QRect& r )
257 {
258  return Rect( r.left(), r.top(), r.right(), r.bottom() );
259 }
260 
261 void Rect::setTopLeft( const Coordinate p )
262 {
263  Coordinate bl = Coordinate( p.x, p.y - mheight );
264  setBottomLeft( bl );
265 }
266 
267 Rect operator|( const Rect& lhs, const Rect& rhs )
268 {
269  Rect r( lhs );
270  r |= rhs;
271  return r;
272 }
273 
274 void Rect::eat( const Rect& r )
275 {
276  setLeft( kigMin( left(), r.left() ) );
277  setRight( kigMax( right(), r.right() ) );
278  setBottom( kigMin( bottom(), r.bottom() ) );
279  setTop( kigMax( top(), r.top() ) );
280 }
281 
282 Rect Rect::matchShape( const Rect& rhs, bool shrink ) const
283 {
284  Rect ret = *this;
285  Coordinate c = center();
286  double v = width()/height(); // current ratio
287  double w = rhs.width()/rhs.height(); // wanted ratio
288 
289  // we don't show less than r, if the dimensions don't match, we
290  // extend r into some dimension...
291  if( ( v > w ) ^ shrink )
292  ret.setHeight( ret.width() / w );
293  else
294  ret.setWidth( ret.height() * w );
295 
296  ret.setCenter(c);
297  return ret.normalized();
298 }
299 
300 bool Rect::valid()
301 {
302  return mBottomLeft.valid() && mwidth != double_inf && mheight != double_inf;
303 }
304 
305 Rect Rect::invalidRect()
306 {
307  return Rect( Coordinate::invalidCoord(), double_inf, double_inf );
308 }
Rect::setTopLeft
void setTopLeft(const Coordinate p)
Definition: rect.cc:261
Rect::moveBy
void moveBy(const Coordinate p)
moves the rect while keeping the size constant...
Definition: rect.cc:144
Coordinate::toQPoint
QPoint toQPoint() const
Definition: coordinate.cpp:165
Rect::normalize
void normalize()
this makes sure width and height are > 0 ...
Definition: rect.cc:130
Rect::mheight
double mheight
Definition: rect.h:128
Rect::setRight
void setRight(const double p)
Definition: rect.cc:103
Rect::width
double width() const
Definition: rect.cc:204
Rect::valid
bool valid()
Definition: rect.cc:300
Rect::bottomLeft
Coordinate bottomLeft() const
Definition: rect.cc:161
Rect::Rect
Rect()
Definition: rect.cc:70
Rect::left
double left() const
Definition: rect.cc:186
Rect
This file is part of Kig, a KDE program for Interactive Geometry...
Definition: rect.h:34
Rect::setBottom
void setBottom(const double p)
Definition: rect.cc:108
operator==
bool operator==(const Rect &r, const Rect &s)
Definition: rect.cc:21
Rect::eat
void eat(const Rect &r)
This expands the rect so that it contains r.
Definition: rect.cc:274
Rect::right
double right() const
Definition: rect.cc:190
Rect::intersects
bool intersects(const Rect &p) const
Definition: rect.cc:230
Rect::invalidRect
static Rect invalidRect()
Definition: rect.cc:305
Rect::setHeight
void setHeight(const double h)
Definition: rect.cc:125
Coordinate
The Coordinate class is the basic class representing a 2D location by its x and y components...
Definition: coordinate.h:33
rect.h
Rect::fromQRect
static Rect fromQRect(const QRect &)
Definition: rect.cc:256
Rect::contains
bool contains(const Coordinate &p) const
Definition: rect.cc:222
Rect::topLeft
Coordinate topLeft() const
Definition: rect.cc:171
kigMax
T kigMax(const T &a, const T &b)
Definition: misc/common.h:261
Rect::bottom
double bottom() const
Definition: rect.cc:194
Rect::setCenter
void setCenter(const Coordinate p)
Definition: rect.cc:91
Rect::center
Coordinate center() const
Definition: rect.cc:181
Rect::mwidth
double mwidth
Definition: rect.h:127
Rect::top
double top() const
Definition: rect.cc:199
Rect::matchShape
Rect matchShape(const Rect &rhs, bool shrink=false) const
return a rect which is a copy of this rect, but has an aspect ratio equal to rhs's one...
Definition: rect.cc:282
Rect::setContains
void setContains(Coordinate p)
this makes sure p is in the rect, extending it if necessary...
Definition: rect.cc:240
Rect::setWidth
void setWidth(const double w)
Definition: rect.cc:120
Rect::setTopRight
void setTopRight(const Coordinate p)
Definition: rect.cc:86
kigMin
T kigMin(const T &a, const T &b)
Definition: misc/common.h:255
common.h
Rect::topRight
Coordinate topRight() const
Definition: rect.cc:176
Rect::scale
void scale(const double r)
scale: only the size changes, topLeft is kept where it is...
Definition: rect.cc:149
Coordinate::invalidCoord
static Coordinate invalidCoord()
Create an invalid Coordinate.
Definition: coordinate.cpp:171
double_inf
const double double_inf
Definition: common.cpp:509
Rect::normalized
Rect normalized() const
Definition: rect.cc:249
Rect::setTop
void setTop(const double p)
Definition: rect.cc:115
Rect::height
double height() const
Definition: rect.cc:209
Rect::setLeft
void setLeft(const double p)
Definition: rect.cc:96
operator<<
kdbgstream & operator<<(kdbgstream &s, const Rect &t)
Definition: rect.cc:28
Coordinate::x
double x
X Component.
Definition: coordinate.h:126
operator|
Rect operator|(const Rect &lhs, const Rect &rhs)
this operator returns a Rect that contains both the given rects.
Definition: rect.cc:267
Coordinate::y
double y
Y Component.
Definition: coordinate.h:129
Rect::bottomRight
Coordinate bottomRight() const
Definition: rect.cc:166
Rect::toQRect
QRect toQRect() const
Definition: rect.cc:156
Rect::mBottomLeft
Coordinate mBottomLeft
Definition: rect.h:126
Rect::setBottomRight
void setBottomRight(const Coordinate p)
Definition: rect.cc:81
Rect::setBottomLeft
void setBottomLeft(const Coordinate p)
Definition: rect.cc:76
Coordinate::valid
bool valid() const
Return whether this is a valid Coordinate.
Definition: coordinate.cpp:176
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:35:40 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kig

Skip menu "kig"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal