# 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
    00335 QwtDoubleRect::QwtDoubleRect(const QRect &rect):
    00336     d_left(rect.left()),
    00337     d_right(rect.right()),
    00338     d_top(rect.top()),
    00339     d_bottom(rect.bottom())
    00340 {
    00341 }
    00342
    00343 QRect QwtDoubleRect::toRect() const
    00344 {
    00345     return QRect(qRound(x()), qRound(y()), qRound(width()), qRound(height()));
    00346 }
    00347
    00351 void QwtDoubleRect::setRect(double left, double top,
    00352     double width, double height)
    00353 {
    00354     d_left = left;
    00355     d_right = left + width;
    00356     d_top = top;
    00357     d_bottom = top + height;
    00358 }
    00359
    00364 void QwtDoubleRect::setSize(const QwtDoubleSize &size)
    00365 {
    00366     setWidth(size.width());
    00367     setHeight(size.height());
    00368 }
    00369
    00376 QwtDoubleRect QwtDoubleRect::normalized() const
    00377 {
    00378     QwtDoubleRect r;
    00379     if ( d_right < d_left )
    00380     {
    00381         r.d_left = d_right;
    00382         r.d_right = d_left;
    00383     }
    00384     else
    00385     {
    00386         r.d_left = d_left;
    00387         r.d_right = d_right;
    00388     }
    00389     if ( d_bottom < d_top )
    00390     {
    00391         r.d_top = d_bottom;
    00392         r.d_bottom = d_top;
    00393     }
    00394     else
    00395     {
    00396         r.d_top = d_top;
    00397         r.d_bottom = d_bottom;
    00398     }
    00399     return r;
    00400 }
    00401
    00406 QwtDoubleRect QwtDoubleRect::unite(const QwtDoubleRect &other) const
    00407 {
    00408     return *this | other;
    00409 }
    00410
    00415 QwtDoubleRect QwtDoubleRect::intersect(const QwtDoubleRect &other) const
    00416 {
    00417     return *this & other;
    00418 }
    00419
    00424 bool QwtDoubleRect::intersects(const QwtDoubleRect &other) const
    00425 {
    00426     return ( qwtMax(d_left, other.d_left) <= qwtMin(d_right, other.d_right) ) &&
    00427          ( qwtMax(d_top, other.d_top ) <= qwtMin(d_bottom, other.d_bottom) );
    00428 }
    00429
    00431 bool QwtDoubleRect::operator==(const QwtDoubleRect &other) const
    00432 {
    00433     return d_left == other.d_left && d_right == other.d_right &&
    00434         d_top == other.d_top && d_bottom == other.d_bottom;
    00435 }
    00436
    00438 bool QwtDoubleRect::operator!=(const QwtDoubleRect &other) const
    00439 {
    00440     return !operator==(other);
    00441 }
    00442
    00448 QwtDoubleRect QwtDoubleRect::operator|(const QwtDoubleRect &other) const
    00449 {
    00450     if ( isEmpty() )
    00451         return other;
    00452
    00453     if ( other.isEmpty() )
    00454         return *this;
    00455
    00456     const double minX = qwtMin(d_left, other.d_left);
    00457     const double maxX = qwtMax(d_right, other.d_right);
    00458     const double minY = qwtMin(d_top, other.d_top);
    00459     const double maxY = qwtMax(d_bottom, other.d_bottom);
    00460
    00461     return QwtDoubleRect(minX, minY, maxX - minX, maxY - minY);
    00462 }
    00463
    00468 QwtDoubleRect QwtDoubleRect::operator&(const QwtDoubleRect &other) const
    00469 {
    00470     if (isNull() || other.isNull())
    00471         return QwtDoubleRect();
    00472
    00473     const QwtDoubleRect r1 = normalized();
    00474     const QwtDoubleRect r2 = other.normalized();
    00475
    00476     const double minX = qwtMax(r1.left(), r2.left());
    00477     const double maxX = qwtMin(r1.right(), r2.right());
    00478     const double minY = qwtMax(r1.top(), r2.top());
    00479     const double maxY = qwtMin(r1.bottom(), r2.bottom());
    00480
    00481     return QwtDoubleRect(minX, minY, maxX - minX, maxY - minY);
    00482 }
    00483
    00485 QwtDoubleRect &QwtDoubleRect::operator|=(const QwtDoubleRect &other)
    00486 {
    00487     *this = *this | other;
    00488     return *this;
    00489 }
    00490
    00492 QwtDoubleRect &QwtDoubleRect::operator&=(const QwtDoubleRect &other)
    00493 {
    00494     *this = *this & other;
    00495     return *this;
    00496 }
    00497
    00499 QwtDoublePoint QwtDoubleRect::center() const
    00500 {
    00501     return QwtDoublePoint(d_left + (d_right - d_left) / 2.0,
    00502         d_top + (d_bottom - d_top) / 2.0);
    00503 }
    00504
    00512 bool QwtDoubleRect::contains(double x, double y, bool proper) const
    00513 {
    00514     if ( proper )
    00515         return x > d_left && x < d_right && y > d_top && y < d_bottom;
    00516     else
    00517         return x >= d_left && x <= d_right && y >= d_top && y <= d_bottom;
    00518 }
    00519
    00527 bool QwtDoubleRect::contains(const QwtDoublePoint &p, bool proper) const
    00528 {
    00529     return contains(p.x(), p.y(), proper);
    00530 }
    00531
    00539 bool QwtDoubleRect::contains(const QwtDoubleRect &other, bool proper) const
    00540 {
    00541     return contains(other.d_left, other.d_top, proper) &&
    00542         contains(other.d_right, other.d_bottom, proper);
    00543 }
    00544
    00546 void QwtDoubleRect::moveLeft(double x)
    00547 {
    00548     const double w = width();
    00549     d_left = x;
    00550     d_right = d_left + w;
    00551 }
    00552
    00554 void QwtDoubleRect::moveRight(double x)
    00555 {
    00556     const double w = width();
    00557     d_right = x;
    00558     d_left = d_right - w;
    00559 }
    00560
    00562 void QwtDoubleRect::moveTop(double y)
    00563 {
    00564     const double h = height();
    00565     d_top = y;
    00566     d_bottom = d_top + h;
    00567 }
    00568
    00570 void QwtDoubleRect::moveBottom(double y)
    00571 {
    00572     const double h = height();
    00573     d_bottom = y;
    00574     d_top = d_bottom - h;
    00575 }
    00576
    00578 void QwtDoubleRect::moveTo(double x, double y)
    00579 {
    00580     moveLeft(x);
    00581     moveTop(y);
    00582 }
    00583
    00585 void QwtDoubleRect::moveBy(double dx, double dy)
    00586 {
    00587     d_left += dx;
    00588     d_right += dx;
    00589     d_top += dy;
    00590     d_bottom += dy;
    00591 }
    00592
    00594 void QwtDoubleRect::moveCenter(const QwtDoublePoint &pos)
    00595 {
    00596     moveCenter(pos.x(), pos.y());
    00597 }
    00598
    00600 void QwtDoubleRect::moveCenter(double x, double y)
    00601 {
    00602     moveTo(x - width() / 2.0, y - height() / 2.0);
    00603 }
    00604
    00605 #endif // QT_VERSION < 0x040000