qwt_double_rect.cpp

00001 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
00002  * Qwt Widget Library
00003  * Copyright (C) 1997   Josef Wilgen
00004  * Copyright (C) 2002   Uwe Rathmann
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the Qwt License, Version 1.0
00008  *****************************************************************************/
00009 
00010 #include <qglobal.h>
00011 
00012 #if QT_VERSION < 0x040000
00013 
00014 #include "qwt_math.h"
00015 #include "qwt_double_rect.h"
00016 
00022 QwtDoublePoint::QwtDoublePoint():
00023     d_x(0.0),
00024     d_y(0.0)
00025 {
00026 }
00027 
00029 QwtDoublePoint::QwtDoublePoint(double x, double y ):
00030     d_x(x),
00031     d_y(y)
00032 {
00033 }
00034 
00040 QwtDoublePoint::QwtDoublePoint(const QPoint &p):
00041     d_x(double(p.x())),
00042     d_y(double(p.y()))
00043 {
00044 }
00045 
00052 bool QwtDoublePoint::operator==(const QwtDoublePoint &other) const
00053 {
00054     return (d_x == other.d_x) && (d_y == other.d_y);
00055 }
00056 
00058 bool QwtDoublePoint::operator!=(const QwtDoublePoint &other) const
00059 {
00060     return !operator==(other);
00061 }
00062 
00067 const QwtDoublePoint QwtDoublePoint::operator-() const
00068 {
00069     return QwtDoublePoint(-d_x, -d_y);
00070 }
00071 
00077 const QwtDoublePoint QwtDoublePoint::operator+(
00078     const QwtDoublePoint &other) const
00079 {
00080     return QwtDoublePoint(d_x + other.d_x, d_y + other.d_y);
00081 }
00082 
00088 const QwtDoublePoint QwtDoublePoint::operator-(
00089     const QwtDoublePoint &other) const
00090 {
00091     return QwtDoublePoint(d_x - other.d_x, d_y - other.d_y);
00092 }
00093 
00099 const QwtDoublePoint QwtDoublePoint::operator*(double factor) const
00100 {
00101     return QwtDoublePoint(d_x * factor, d_y * factor);
00102 }
00103 
00109 const QwtDoublePoint QwtDoublePoint::operator/(double factor) const
00110 {
00111     return QwtDoublePoint(d_x / factor, d_y / factor);
00112 }
00113 
00119 QwtDoublePoint &QwtDoublePoint::operator+=(const QwtDoublePoint &other)
00120 {
00121     d_x += other.d_x;
00122     d_y += other.d_y;
00123     return *this;
00124 }
00125 
00131 QwtDoublePoint &QwtDoublePoint::operator-=(const QwtDoublePoint &other)
00132 {
00133     d_x -= other.d_x;
00134     d_y -= other.d_y;
00135     return *this;
00136 }
00137 
00143 QwtDoublePoint &QwtDoublePoint::operator*=(double factor)
00144 {
00145     d_x *= factor;
00146     d_y *= factor;
00147     return *this;
00148 }
00149 
00155 QwtDoublePoint &QwtDoublePoint::operator/=(double factor)
00156 {
00157     d_x /= factor;
00158     d_y /= factor;
00159     return *this;
00160 }
00161 
00163 QwtDoubleSize::QwtDoubleSize():
00164     d_width(-1.0),
00165     d_height(-1.0)
00166 {   
00167 }   
00168 
00170 QwtDoubleSize::QwtDoubleSize( double width, double height ):
00171     d_width(width),
00172     d_height(height)
00173 {   
00174 }   
00175 
00177 QwtDoubleSize::QwtDoubleSize(const QSize &sz):
00178     d_width(double(sz.width())),
00179     d_height(double(sz.height()))
00180 {   
00181 }   
00182 
00184 void QwtDoubleSize::transpose()
00185 {   
00186     double tmp = d_width;
00187     d_width = d_height;
00188     d_height = tmp;
00189 }
00190 
00195 QwtDoubleSize QwtDoubleSize::expandedTo(
00196     const QwtDoubleSize &other) const
00197 {   
00198     return QwtDoubleSize(
00199         qwtMax(d_width, other.d_width),
00200         qwtMax(d_height, other.d_height)
00201     );  
00202 }   
00203 
00207 QwtDoubleSize QwtDoubleSize::boundedTo(
00208     const QwtDoubleSize &other) const
00209 {   
00210     return QwtDoubleSize(
00211         qwtMin(d_width, other.d_width),
00212         qwtMin(d_height, other.d_height)
00213     );  
00214 }   
00215 
00217 bool QwtDoubleSize::operator==(const QwtDoubleSize &other) const
00218 { 
00219     return d_width == other.d_width && d_height == other.d_height;
00220 }   
00221 
00223 bool QwtDoubleSize::operator!=(const QwtDoubleSize &other) const
00224 { 
00225     return !operator==(other);
00226 }   
00227 
00232 const QwtDoubleSize QwtDoubleSize::operator+(
00233     const QwtDoubleSize &other) const
00234 {   
00235     return QwtDoubleSize(d_width + other.d_width,
00236         d_height + other.d_height); 
00237 }       
00238 
00243 const QwtDoubleSize QwtDoubleSize::operator-(
00244     const QwtDoubleSize &other) const
00245 {   
00246     return QwtDoubleSize(d_width - other.d_width,
00247         d_height - other.d_height); 
00248 }       
00249 
00251 const QwtDoubleSize QwtDoubleSize::operator*(double c) const
00252 { 
00253     return QwtDoubleSize(d_width * c, d_height * c);
00254 }   
00255 
00257 const QwtDoubleSize QwtDoubleSize::operator/(double c) const
00258 { 
00259     return QwtDoubleSize(d_width / c, d_height / c);
00260 }   
00261 
00263 QwtDoubleSize &QwtDoubleSize::operator+=(const QwtDoubleSize &other)
00264 {   
00265     d_width += other.d_width; 
00266     d_height += other.d_height;
00267     return *this;
00268 }
00269 
00271 QwtDoubleSize &QwtDoubleSize::operator-=(const QwtDoubleSize &other)
00272 {   
00273     d_width -= other.d_width; 
00274     d_height -= other.d_height;
00275     return *this;
00276 }
00277 
00278 /* 
00279   Multiplies this size's width and height by c, 
00280   and returns a reference to this size.
00281 */
00282 QwtDoubleSize &QwtDoubleSize::operator*=(double c)
00283 {   
00284     d_width *= c; 
00285     d_height *= c;
00286     return *this;
00287 }
00288 
00289 /* 
00290   Devides this size's width and height by c, 
00291   and returns a reference to this size.
00292 */
00293 QwtDoubleSize &QwtDoubleSize::operator/=(double c)
00294 {
00295     d_width /= c;
00296     d_height /= c;
00297     return *this;
00298 }   
00299 
00301 QwtDoubleRect::QwtDoubleRect():
00302     d_left(0.0),
00303     d_right(0.0),
00304     d_top(0.0),
00305     d_bottom(0.0)
00306 {
00307 }
00308 
00313 QwtDoubleRect::QwtDoubleRect(double left, double top,
00314         double width, double height):
00315     d_left(left),
00316     d_right(left + width),
00317     d_top(top),
00318     d_bottom(top + height)
00319 {
00320 }
00321 
00326 QwtDoubleRect::QwtDoubleRect(
00327         const QwtDoublePoint &p, const QwtDoubleSize &size):
00328     d_left(p.x()),
00329     d_right(p.x() + size.width()),
00330     d_top(p.y()),
00331     d_bottom(p.y() + size.height())
00332 {
00333 }
00334 
00338 void QwtDoubleRect::setRect(double left, double top, 
00339     double width, double height)
00340 {
00341     d_left = left;
00342     d_right = left + width;
00343     d_top = top;
00344     d_bottom = top + height;
00345 }
00346 
00351 void QwtDoubleRect::setSize(const QwtDoubleSize &size)
00352 {
00353     setWidth(size.width());
00354     setHeight(size.height());
00355 }
00356 
00363 QwtDoubleRect QwtDoubleRect::normalized() const
00364 {
00365     QwtDoubleRect r;
00366     if ( d_right < d_left ) 
00367     {
00368         r.d_left = d_right;
00369         r.d_right = d_left;
00370     } 
00371     else 
00372     {
00373         r.d_left = d_left;
00374         r.d_right = d_right; 
00375     }
00376     if ( d_bottom < d_top ) 
00377     { 
00378         r.d_top = d_bottom; 
00379         r.d_bottom = d_top;
00380     } 
00381     else 
00382     {
00383         r.d_top = d_top;
00384         r.d_bottom = d_bottom;
00385     }
00386     return r;
00387 }
00388 
00393 QwtDoubleRect QwtDoubleRect::unite(const QwtDoubleRect &other) const
00394 {
00395     return *this | other;
00396 }
00397 
00402 QwtDoubleRect QwtDoubleRect::intersect(const QwtDoubleRect &other) const
00403 {
00404     return *this & other;
00405 }
00406 
00411 bool QwtDoubleRect::intersects(const QwtDoubleRect &other) const
00412 {
00413     return ( qwtMax(d_left, other.d_left) <= qwtMin(d_right, other.d_right) ) &&
00414          ( qwtMax(d_top, other.d_top ) <= qwtMin(d_bottom, other.d_bottom) );
00415 }
00416 
00418 bool QwtDoubleRect::operator==(const QwtDoubleRect &other) const
00419 {
00420     return d_left == other.d_left && d_right == other.d_right && 
00421         d_top == other.d_top && d_bottom == other.d_bottom;
00422 }
00423 
00425 bool QwtDoubleRect::operator!=(const QwtDoubleRect &other) const
00426 {
00427     return !operator==(other);
00428 }
00429 
00435 QwtDoubleRect QwtDoubleRect::operator|(const QwtDoubleRect &other) const
00436 {
00437     if ( isEmpty() ) 
00438         return other;
00439 
00440     if ( other.isEmpty() ) 
00441         return *this;
00442         
00443     const double minX = qwtMin(d_left, other.d_left);
00444     const double maxX = qwtMax(d_right, other.d_right);
00445     const double minY = qwtMin(d_top, other.d_top);
00446     const double maxY = qwtMax(d_bottom, other.d_bottom);
00447 
00448     return QwtDoubleRect(minX, minY, maxX - minX, maxY - minY);
00449 }
00450 
00455 QwtDoubleRect QwtDoubleRect::operator&(const QwtDoubleRect &other) const
00456 {
00457     if (isNull() || other.isNull())
00458         return QwtDoubleRect();
00459 
00460     const QwtDoubleRect r1 = normalized();
00461     const QwtDoubleRect r2 = other.normalized();
00462 
00463     const double minX = qwtMax(r1.left(), r2.left());
00464     const double maxX = qwtMin(r1.right(), r2.right());
00465     const double minY = qwtMax(r1.top(), r2.top());
00466     const double maxY = qwtMin(r1.bottom(), r2.bottom());
00467 
00468     return QwtDoubleRect(minX, minY, maxX - minX, maxY - minY);
00469 }
00470 
00472 QwtDoubleRect &QwtDoubleRect::operator|=(const QwtDoubleRect &other)
00473 {
00474     *this = *this | other;
00475     return *this;
00476 }
00477 
00479 QwtDoubleRect &QwtDoubleRect::operator&=(const QwtDoubleRect &other)
00480 {
00481     *this = *this & other;
00482     return *this;
00483 }
00484 
00486 QwtDoublePoint QwtDoubleRect::center() const
00487 {
00488     return QwtDoublePoint(d_left + (d_right - d_left) / 2.0, 
00489         d_top + (d_bottom - d_top) / 2.0);
00490 }
00491 
00499 bool QwtDoubleRect::contains(double x, double y, bool proper) const
00500 {
00501     if ( proper )
00502         return x > d_left && x < d_right && y > d_top && y < d_bottom;
00503     else
00504         return x >= d_left && x <= d_right && y >= d_top && y <= d_bottom;
00505 }
00506 
00514 bool QwtDoubleRect::contains(const QwtDoublePoint &p, bool proper) const
00515 {
00516     return contains(p.x(), p.y(), proper);
00517 }
00518 
00526 bool QwtDoubleRect::contains(const QwtDoubleRect &other, bool proper) const
00527 {
00528     return contains(other.d_left, other.d_top, proper) && 
00529         contains(other.d_right, other.d_bottom, proper);
00530 }
00531 
00533 void QwtDoubleRect::moveLeft(double x)
00534 {
00535     const double w = width();
00536     d_left = x;
00537     d_right = d_left + w;
00538 }
00539 
00541 void QwtDoubleRect::moveRight(double x)
00542 {
00543     const double w = width();
00544     d_right = x;
00545     d_left = d_right - w;
00546 }
00547 
00549 void QwtDoubleRect::moveTop(double y)
00550 {
00551     const double h = height();
00552     d_top = y;
00553     d_bottom = d_top + h;
00554 }
00555 
00557 void QwtDoubleRect::moveBottom(double y)
00558 {
00559     const double h = height();
00560     d_bottom = y;
00561     d_top = d_bottom - h;
00562 }
00563 
00565 void QwtDoubleRect::moveTo(double x, double y)
00566 {
00567     moveLeft(x);
00568     moveTop(y);
00569 }
00570 
00572 void QwtDoubleRect::moveBy(double dx, double dy)
00573 {
00574     d_left += dx;
00575     d_right += dx;
00576     d_top += dy;
00577     d_bottom += dy;
00578 }
00579 
00581 void QwtDoubleRect::moveCenter(const QwtDoublePoint &pos)
00582 {
00583     moveCenter(pos.x(), pos.y());
00584 }
00585 
00587 void QwtDoubleRect::moveCenter(double x, double y)
00588 {
00589     moveTo(x - width() / 2.0, y - height() / 2.0);
00590 }
00591 
00592 #endif // QT_VERSION < 0x040000

Generated on Mon Feb 26 21:22:36 2007 for Qwt User's Guide by  doxygen 1.4.6