qwt_double_rect.cpp 15.1 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#include <qglobal.h>

#if QT_VERSION < 0x040000

#include "qwt_math.h"
#include "qwt_double_rect.h"

/*! 
    Constructs a null point.

    \sa QwtDoublePoint::isNull
*/
QwtDoublePoint::QwtDoublePoint():
    d_x(0.0),
    d_y(0.0)
{
}

//! Constructs a point with coordinates specified by x and y.
QwtDoublePoint::QwtDoublePoint(double x, double y ):
    d_x(x),
    d_y(y)
{
}

/*! 
    Copy constructor. 

    Constructs a point using the values of the point specified.
*/
QwtDoublePoint::QwtDoublePoint(const QPoint &p):
    d_x(double(p.x())),
    d_y(double(p.y()))
{
}

/*! 
    Returns true if point1 is equal to point2; otherwise returns false.

    Two points are equal to each other if both x-coordinates and 
    both y-coordinates are the same.
*/
bool QwtDoublePoint::operator==(const QwtDoublePoint &other) const
{
    return (d_x == other.d_x) && (d_y == other.d_y);
}

//! Returns true if point1 is not equal to point2; otherwise returns false.
bool QwtDoublePoint::operator!=(const QwtDoublePoint &other) const
{
    return !operator==(other);
}

/*! 
    Negates the coordinates of the point, and returns a point with the 
    new coordinates. (Inversion).
*/
const QwtDoublePoint QwtDoublePoint::operator-() const
{
    return QwtDoublePoint(-d_x, -d_y);
}

/*! 
    Adds the coordinates of the point to the corresponding coordinates of 
    the other point, and returns a point with the new coordinates. 
    (Vector addition.)
*/
const QwtDoublePoint QwtDoublePoint::operator+(
    const QwtDoublePoint &other) const
{
    return QwtDoublePoint(d_x + other.d_x, d_y + other.d_y);
}

/*! 
    Subtracts the coordinates of the other point from the corresponding 
    coordinates of the given point, and returns a point with the new 
    coordinates. (Vector subtraction.)
*/
const QwtDoublePoint QwtDoublePoint::operator-(
    const QwtDoublePoint &other) const
{
    return QwtDoublePoint(d_x - other.d_x, d_y - other.d_y);
}

/*!
    Multiplies the coordinates of the point by the given scale factor, 
    and returns a point with the new coordinates. 
    (Scalar multiplication of a vector.)
*/
const QwtDoublePoint QwtDoublePoint::operator*(double factor) const
{
    return QwtDoublePoint(d_x * factor, d_y * factor);
}

/*!
    Divides the coordinates of the point by the given scale factor, 
    and returns a point with the new coordinates. 
    (Scalar division of a vector.)
*/
const QwtDoublePoint QwtDoublePoint::operator/(double factor) const
{
    return QwtDoublePoint(d_x / factor, d_y / factor);
}

/*!
    Adds the coordinates of this point to the corresponding coordinates 
    of the other point, and returns a reference to this point with the 
    new coordinates. This is equivalent to vector addition.
*/
QwtDoublePoint &QwtDoublePoint::operator+=(const QwtDoublePoint &other)
{
    d_x += other.d_x;
    d_y += other.d_y;
    return *this;
}

/*!
    Subtracts the coordinates of the other point from the corresponding 
    coordinates of this point, and returns a reference to this point with 
    the new coordinates. This is equivalent to vector subtraction.
*/
QwtDoublePoint &QwtDoublePoint::operator-=(const QwtDoublePoint &other)
{
    d_x -= other.d_x;
    d_y -= other.d_y;
    return *this;
}

/*!
    Multiplies the coordinates of this point by the given scale factor, 
    and returns a reference to this point with the new coordinates. 
    This is equivalent to scalar multiplication of a vector.
*/
QwtDoublePoint &QwtDoublePoint::operator*=(double factor)
{
    d_x *= factor;
    d_y *= factor;
    return *this;
}

/*!
    Divides the coordinates of this point by the given scale factor, 
    and returns a references to this point with the new coordinates. 
    This is equivalent to scalar division of a vector.
*/
QwtDoublePoint &QwtDoublePoint::operator/=(double factor)
{
    d_x /= factor;
    d_y /= factor;
    return *this;
}

//! Constructs an invalid size.
QwtDoubleSize::QwtDoubleSize():
    d_width(-1.0),
    d_height(-1.0)
{   
}   

//! Constructs a size with width width and height height.
QwtDoubleSize::QwtDoubleSize( double width, double height ):
    d_width(width),
    d_height(height)
{   
}   

//! Constructs a size with floating point accuracy from the given size.
QwtDoubleSize::QwtDoubleSize(const QSize &sz):
    d_width(double(sz.width())),
    d_height(double(sz.height()))
{   
}   

//! Swaps the width and height values.
void QwtDoubleSize::transpose()
{   
    double tmp = d_width;
    d_width = d_height;
    d_height = tmp;
}

/*! 
    Returns a size with the maximum width and height of this 
    size and other.
*/
QwtDoubleSize QwtDoubleSize::expandedTo(
    const QwtDoubleSize &other) const
{   
    return QwtDoubleSize(
        qwtMax(d_width, other.d_width),
        qwtMax(d_height, other.d_height)
    );  
}   

/*!
    Returns a size with the minimum width and height of this size and other.
*/
QwtDoubleSize QwtDoubleSize::boundedTo(
    const QwtDoubleSize &other) const
{   
    return QwtDoubleSize(
        qwtMin(d_width, other.d_width),
        qwtMin(d_height, other.d_height)
    );  
}   

//! Returns true if s1 and s2 are equal; otherwise returns false.
bool QwtDoubleSize::operator==(const QwtDoubleSize &other) const
{ 
    return d_width == other.d_width && d_height == other.d_height;
}   

//! Returns true if s1 and s2 are different; otherwise returns false.
bool QwtDoubleSize::operator!=(const QwtDoubleSize &other) const
{ 
    return !operator==(other);
}   

/*! 
  Returns the size formed by adding both components by
  the components of other. Each component is added separately.
*/
const QwtDoubleSize QwtDoubleSize::operator+(
    const QwtDoubleSize &other) const
{   
    return QwtDoubleSize(d_width + other.d_width,
        d_height + other.d_height); 
}       

/*! 
  Returns the size formed by subtracting both components by
  the components of other. Each component is subtracted separately.
*/  
const QwtDoubleSize QwtDoubleSize::operator-(
    const QwtDoubleSize &other) const
{   
    return QwtDoubleSize(d_width - other.d_width,
        d_height - other.d_height); 
}       

//! Returns the size formed by multiplying both components by c.
const QwtDoubleSize QwtDoubleSize::operator*(double c) const
{ 
    return QwtDoubleSize(d_width * c, d_height * c);
}   

//! Returns the size formed by dividing both components by c.
const QwtDoubleSize QwtDoubleSize::operator/(double c) const
{ 
    return QwtDoubleSize(d_width / c, d_height / c);
}   

//! Adds size other to this size and returns a reference to this size.
QwtDoubleSize &QwtDoubleSize::operator+=(const QwtDoubleSize &other)
{   
    d_width += other.d_width; 
    d_height += other.d_height;
    return *this;
}

//! Subtracts size other from this size and returns a reference to this size.
QwtDoubleSize &QwtDoubleSize::operator-=(const QwtDoubleSize &other)
{   
    d_width -= other.d_width; 
    d_height -= other.d_height;
    return *this;
}

/* 
  Multiplies this size's width and height by c, 
  and returns a reference to this size.
*/
QwtDoubleSize &QwtDoubleSize::operator*=(double c)
{   
    d_width *= c; 
    d_height *= c;
    return *this;
}

/* 
  Devides this size's width and height by c, 
  and returns a reference to this size.
*/
QwtDoubleSize &QwtDoubleSize::operator/=(double c)
{
    d_width /= c;
    d_height /= c;
    return *this;
}   

//! Constructs an rectangle with all components set to 0.0 
QwtDoubleRect::QwtDoubleRect():
    d_left(0.0),
    d_right(0.0),
    d_top(0.0),
    d_bottom(0.0)
{
}

/*! 
  Constructs an rectangle with x1 to x2 as x-range and,
  y1 to y2 as y-range.
*/
QwtDoubleRect::QwtDoubleRect(double left, double top,
        double width, double height):
    d_left(left),
    d_right(left + width),
    d_top(top),
    d_bottom(top + height)
{
}

/*! 
  Constructs a rectangle with topLeft as the top-left corner and 
  size as the rectangle size.
*/
QwtDoubleRect::QwtDoubleRect(
        const QwtDoublePoint &p, const QwtDoubleSize &size):
    d_left(p.x()),
    d_right(p.x() + size.width()),
    d_top(p.y()),
    d_bottom(p.y() + size.height())
{
}

QwtDoubleRect::QwtDoubleRect(const QRect &rect):
    d_left(rect.left()),
    d_right(rect.right()),
    d_top(rect.top()),
    d_bottom(rect.bottom())
{
}

QRect QwtDoubleRect::toRect() const
{
    return QRect(qRound(x()), qRound(y()), qRound(width()), qRound(height()));
}

/*! 
  Set the x-range from x1 to x2 and the y-range from y1 to y2.
*/
void QwtDoubleRect::setRect(double left, double top, 
    double width, double height)
{
    d_left = left;
    d_right = left + width;
    d_top = top;
    d_bottom = top + height;
}

/*!
  Sets the size of the rectangle to size. 
  Changes x2 and y2 only.
*/
void QwtDoubleRect::setSize(const QwtDoubleSize &size)
{
    setWidth(size.width());
    setHeight(size.height());
}

/*!
  Returns a normalized rectangle, i.e. a rectangle that has a non-negative 
  width and height. 

  It swaps x1 and x2 if x1() > x2(), and swaps y1 and y2 if y1() > y2(). 
*/
QwtDoubleRect QwtDoubleRect::normalized() const
{
    QwtDoubleRect r;
    if ( d_right < d_left ) 
    {
        r.d_left = d_right;
        r.d_right = d_left;
    } 
    else 
    {
        r.d_left = d_left;
        r.d_right = d_right; 
    }
    if ( d_bottom < d_top ) 
    { 
        r.d_top = d_bottom; 
        r.d_bottom = d_top;
    } 
    else 
    {
        r.d_top = d_top;
        r.d_bottom = d_bottom;
    }
    return r;
}

/*!
  Returns the bounding rectangle of this rectangle and rectangle other. 
  r.unite(s) is equivalent to r|s. 
*/
QwtDoubleRect QwtDoubleRect::unite(const QwtDoubleRect &other) const
{
    return *this | other;
}

/*!
  Returns the intersection of this rectangle and rectangle other. 
  r.intersect(s) is equivalent to r&s. 
*/
QwtDoubleRect QwtDoubleRect::intersect(const QwtDoubleRect &other) const
{
    return *this & other;
}

/*!
  Returns true if this rectangle intersects with rectangle other; 
  otherwise returns false. 
*/
bool QwtDoubleRect::intersects(const QwtDoubleRect &other) const
{
    return ( qwtMax(d_left, other.d_left) <= qwtMin(d_right, other.d_right) ) &&
         ( qwtMax(d_top, other.d_top ) <= qwtMin(d_bottom, other.d_bottom) );
}

//! Returns true if this rect and other are equal; otherwise returns false. 
bool QwtDoubleRect::operator==(const QwtDoubleRect &other) const
{
    return d_left == other.d_left && d_right == other.d_right && 
        d_top == other.d_top && d_bottom == other.d_bottom;
}

//! Returns true if this rect and other are different; otherwise returns false. 
bool QwtDoubleRect::operator!=(const QwtDoubleRect &other) const
{
    return !operator==(other);
}

/*!
  Returns the bounding rectangle of this rectangle and rectangle other. 
  The bounding rectangle of a nonempty rectangle and an empty or 
  invalid rectangle is defined to be the nonempty rectangle. 
*/
QwtDoubleRect QwtDoubleRect::operator|(const QwtDoubleRect &other) const
{
    if ( isEmpty() ) 
        return other;

    if ( other.isEmpty() ) 
        return *this;
        
    const double minX = qwtMin(d_left, other.d_left);
    const double maxX = qwtMax(d_right, other.d_right);
    const double minY = qwtMin(d_top, other.d_top);
    const double maxY = qwtMax(d_bottom, other.d_bottom);

    return QwtDoubleRect(minX, minY, maxX - minX, maxY - minY);
}

/*!
  Returns the intersection of this rectangle and rectangle other. 
  Returns an empty rectangle if there is no intersection. 
*/
QwtDoubleRect QwtDoubleRect::operator&(const QwtDoubleRect &other) const
{
    if (isNull() || other.isNull())
        return QwtDoubleRect();

    const QwtDoubleRect r1 = normalized();
    const QwtDoubleRect r2 = other.normalized();

    const double minX = qwtMax(r1.left(), r2.left());
    const double maxX = qwtMin(r1.right(), r2.right());
    const double minY = qwtMax(r1.top(), r2.top());
    const double maxY = qwtMin(r1.bottom(), r2.bottom());

    return QwtDoubleRect(minX, minY, maxX - minX, maxY - minY);
}

//! Unites this rectangle with rectangle other. 
QwtDoubleRect &QwtDoubleRect::operator|=(const QwtDoubleRect &other)
{
    *this = *this | other;
    return *this;
}

//! Intersects this rectangle with rectangle other. 
QwtDoubleRect &QwtDoubleRect::operator&=(const QwtDoubleRect &other)
{
    *this = *this & other;
    return *this;
}

//! Returns the center point of the rectangle. 
QwtDoublePoint QwtDoubleRect::center() const
{
    return QwtDoublePoint(d_left + (d_right - d_left) / 2.0, 
        d_top + (d_bottom - d_top) / 2.0);
}

/*!
  Returns true if the point (x, y) is inside or on the edge of the rectangle; 
  otherwise returns false. 

  If proper is true, this function returns true only if p is inside 
  (not on the edge). 
*/
bool QwtDoubleRect::contains(double x, double y, bool proper) const
{
    if ( proper )
        return x > d_left && x < d_right && y > d_top && y < d_bottom;
    else
        return x >= d_left && x <= d_right && y >= d_top && y <= d_bottom;
}

/*!
  Returns true if the point p is inside or on the edge of the rectangle; 
  otherwise returns false. 

  If proper is true, this function returns true only if p is inside 
  (not on the edge). 
*/
bool QwtDoubleRect::contains(const QwtDoublePoint &p, bool proper) const
{
    return contains(p.x(), p.y(), proper);
}

/*!
  Returns true if the rectangle other is inside this rectangle; 
  otherwise returns false. 

  If proper is true, this function returns true only if other is entirely 
  inside (not on the edge). 
*/
bool QwtDoubleRect::contains(const QwtDoubleRect &other, bool proper) const
{
    return contains(other.d_left, other.d_top, proper) && 
        contains(other.d_right, other.d_bottom, proper);
}

//! moves x1() to x, leaving the size unchanged
void QwtDoubleRect::moveLeft(double x)
{
    const double w = width();
    d_left = x;
    d_right = d_left + w;
}

//! moves x1() to x, leaving the size unchanged
void QwtDoubleRect::moveRight(double x)
{
    const double w = width();
    d_right = x;
    d_left = d_right - w;
}

//! moves y1() to y, leaving the size unchanged
void QwtDoubleRect::moveTop(double y)
{
    const double h = height();
    d_top = y;
    d_bottom = d_top + h;
}

//! moves y1() to y, leaving the size unchanged
void QwtDoubleRect::moveBottom(double y)
{
    const double h = height();
    d_bottom = y;
    d_top = d_bottom - h;
}

//! moves left() to x and top() to y, leaving the size unchanged
void QwtDoubleRect::moveTo(double x, double y)
{
    moveLeft(x);
    moveTop(y);
}

//! moves x1() by dx and y1() by dy. leaving the size unchanged
void QwtDoubleRect::moveBy(double dx, double dy)
{
    d_left += dx;
    d_right += dx;
    d_top += dy;
    d_bottom += dy;
}

//! moves the center to pos, leaving the size unchanged
void QwtDoubleRect::moveCenter(const QwtDoublePoint &pos)
{
    moveCenter(pos.x(), pos.y());
}

//! moves the center to (x, y), leaving the size unchanged
void QwtDoubleRect::moveCenter(double x, double y)
{
    moveTo(x - width() / 2.0, y - height() / 2.0);
}

#endif // QT_VERSION < 0x040000