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
/* -*- 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"

17
/*!
pixhawk's avatar
pixhawk committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
    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)
{
}

35 36
/*!
    Copy constructor.
pixhawk's avatar
pixhawk committed
37 38 39 40 41 42 43 44 45

    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()))
{
}

46
/*!
pixhawk's avatar
pixhawk committed
47 48
    Returns true if point1 is equal to point2; otherwise returns false.

49
    Two points are equal to each other if both x-coordinates and
pixhawk's avatar
pixhawk committed
50 51 52 53 54 55 56 57 58 59 60 61 62
    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);
}

63 64
/*!
    Negates the coordinates of the point, and returns a point with the
pixhawk's avatar
pixhawk committed
65 66 67 68 69 70 71
    new coordinates. (Inversion).
*/
const QwtDoublePoint QwtDoublePoint::operator-() const
{
    return QwtDoublePoint(-d_x, -d_y);
}

72 73 74
/*!
    Adds the coordinates of the point to the corresponding coordinates of
    the other point, and returns a point with the new coordinates.
pixhawk's avatar
pixhawk committed
75 76 77 78 79 80 81 82
    (Vector addition.)
*/
const QwtDoublePoint QwtDoublePoint::operator+(
    const QwtDoublePoint &other) const
{
    return QwtDoublePoint(d_x + other.d_x, d_y + other.d_y);
}

83 84 85
/*!
    Subtracts the coordinates of the other point from the corresponding
    coordinates of the given point, and returns a point with the new
pixhawk's avatar
pixhawk committed
86 87 88 89 90 91 92 93 94
    coordinates. (Vector subtraction.)
*/
const QwtDoublePoint QwtDoublePoint::operator-(
    const QwtDoublePoint &other) const
{
    return QwtDoublePoint(d_x - other.d_x, d_y - other.d_y);
}

/*!
95 96
    Multiplies the coordinates of the point by the given scale factor,
    and returns a point with the new coordinates.
pixhawk's avatar
pixhawk committed
97 98 99 100 101 102 103 104
    (Scalar multiplication of a vector.)
*/
const QwtDoublePoint QwtDoublePoint::operator*(double factor) const
{
    return QwtDoublePoint(d_x * factor, d_y * factor);
}

/*!
105 106
    Divides the coordinates of the point by the given scale factor,
    and returns a point with the new coordinates.
pixhawk's avatar
pixhawk committed
107 108 109 110 111 112 113 114
    (Scalar division of a vector.)
*/
const QwtDoublePoint QwtDoublePoint::operator/(double factor) const
{
    return QwtDoublePoint(d_x / factor, d_y / factor);
}

/*!
115 116
    Adds the coordinates of this point to the corresponding coordinates
    of the other point, and returns a reference to this point with the
pixhawk's avatar
pixhawk committed
117 118 119 120 121 122 123 124 125 126
    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;
}

/*!
127 128
    Subtracts the coordinates of the other point from the corresponding
    coordinates of this point, and returns a reference to this point with
pixhawk's avatar
pixhawk committed
129 130 131 132 133 134 135 136 137 138
    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;
}

/*!
139 140
    Multiplies the coordinates of this point by the given scale factor,
    and returns a reference to this point with the new coordinates.
pixhawk's avatar
pixhawk committed
141 142 143 144 145 146 147 148 149 150
    This is equivalent to scalar multiplication of a vector.
*/
QwtDoublePoint &QwtDoublePoint::operator*=(double factor)
{
    d_x *= factor;
    d_y *= factor;
    return *this;
}

/*!
151 152
    Divides the coordinates of this point by the given scale factor,
    and returns a references to this point with the new coordinates.
pixhawk's avatar
pixhawk committed
153 154 155 156 157 158 159 160 161 162 163 164 165
    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)
166 167
{
}
pixhawk's avatar
pixhawk committed
168 169 170 171 172

//! Constructs a size with width width and height height.
QwtDoubleSize::QwtDoubleSize( double width, double height ):
    d_width(width),
    d_height(height)
173 174
{
}
pixhawk's avatar
pixhawk committed
175 176 177 178 179

//! 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()))
180 181
{
}
pixhawk's avatar
pixhawk committed
182 183 184

//! Swaps the width and height values.
void QwtDoubleSize::transpose()
185
{
pixhawk's avatar
pixhawk committed
186 187 188 189 190
    double tmp = d_width;
    d_width = d_height;
    d_height = tmp;
}

191 192
/*!
    Returns a size with the maximum width and height of this
pixhawk's avatar
pixhawk committed
193 194 195 196
    size and other.
*/
QwtDoubleSize QwtDoubleSize::expandedTo(
    const QwtDoubleSize &other) const
197
{
pixhawk's avatar
pixhawk committed
198
    return QwtDoubleSize(
199 200 201 202
               qwtMax(d_width, other.d_width),
               qwtMax(d_height, other.d_height)
           );
}
pixhawk's avatar
pixhawk committed
203 204 205 206 207 208

/*!
    Returns a size with the minimum width and height of this size and other.
*/
QwtDoubleSize QwtDoubleSize::boundedTo(
    const QwtDoubleSize &other) const
209
{
pixhawk's avatar
pixhawk committed
210
    return QwtDoubleSize(
211 212 213 214
               qwtMin(d_width, other.d_width),
               qwtMin(d_height, other.d_height)
           );
}
pixhawk's avatar
pixhawk committed
215 216 217

//! Returns true if s1 and s2 are equal; otherwise returns false.
bool QwtDoubleSize::operator==(const QwtDoubleSize &other) const
218
{
pixhawk's avatar
pixhawk committed
219
    return d_width == other.d_width && d_height == other.d_height;
220
}
pixhawk's avatar
pixhawk committed
221 222 223

//! Returns true if s1 and s2 are different; otherwise returns false.
bool QwtDoubleSize::operator!=(const QwtDoubleSize &other) const
224
{
pixhawk's avatar
pixhawk committed
225
    return !operator==(other);
226
}
pixhawk's avatar
pixhawk committed
227

228
/*!
pixhawk's avatar
pixhawk committed
229 230 231 232 233
  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
234
{
pixhawk's avatar
pixhawk committed
235
    return QwtDoubleSize(d_width + other.d_width,
236 237
                         d_height + other.d_height);
}
pixhawk's avatar
pixhawk committed
238

239
/*!
pixhawk's avatar
pixhawk committed
240 241
  Returns the size formed by subtracting both components by
  the components of other. Each component is subtracted separately.
242
*/
pixhawk's avatar
pixhawk committed
243 244
const QwtDoubleSize QwtDoubleSize::operator-(
    const QwtDoubleSize &other) const
245
{
pixhawk's avatar
pixhawk committed
246
    return QwtDoubleSize(d_width - other.d_width,
247 248
                         d_height - other.d_height);
}
pixhawk's avatar
pixhawk committed
249 250 251

//! Returns the size formed by multiplying both components by c.
const QwtDoubleSize QwtDoubleSize::operator*(double c) const
252
{
pixhawk's avatar
pixhawk committed
253
    return QwtDoubleSize(d_width * c, d_height * c);
254
}
pixhawk's avatar
pixhawk committed
255 256 257

//! Returns the size formed by dividing both components by c.
const QwtDoubleSize QwtDoubleSize::operator/(double c) const
258
{
pixhawk's avatar
pixhawk committed
259
    return QwtDoubleSize(d_width / c, d_height / c);
260
}
pixhawk's avatar
pixhawk committed
261 262 263

//! Adds size other to this size and returns a reference to this size.
QwtDoubleSize &QwtDoubleSize::operator+=(const QwtDoubleSize &other)
264 265
{
    d_width += other.d_width;
pixhawk's avatar
pixhawk committed
266 267 268 269 270 271
    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)
272 273
{
    d_width -= other.d_width;
pixhawk's avatar
pixhawk committed
274 275 276 277
    d_height -= other.d_height;
    return *this;
}

278 279
/*
  Multiplies this size's width and height by c,
pixhawk's avatar
pixhawk committed
280 281 282
  and returns a reference to this size.
*/
QwtDoubleSize &QwtDoubleSize::operator*=(double c)
283 284
{
    d_width *= c;
pixhawk's avatar
pixhawk committed
285 286 287 288
    d_height *= c;
    return *this;
}

289 290
/*
  Devides this size's width and height by c,
pixhawk's avatar
pixhawk committed
291 292 293 294 295 296 297
  and returns a reference to this size.
*/
QwtDoubleSize &QwtDoubleSize::operator/=(double c)
{
    d_width /= c;
    d_height /= c;
    return *this;
298
}
pixhawk's avatar
pixhawk committed
299

300
//! Constructs an rectangle with all components set to 0.0
pixhawk's avatar
pixhawk committed
301 302 303 304 305 306 307 308
QwtDoubleRect::QwtDoubleRect():
    d_left(0.0),
    d_right(0.0),
    d_top(0.0),
    d_bottom(0.0)
{
}

309
/*!
pixhawk's avatar
pixhawk committed
310 311 312 313
  Constructs an rectangle with x1 to x2 as x-range and,
  y1 to y2 as y-range.
*/
QwtDoubleRect::QwtDoubleRect(double left, double top,
314
                             double width, double height):
pixhawk's avatar
pixhawk committed
315 316 317 318 319 320 321
    d_left(left),
    d_right(left + width),
    d_top(top),
    d_bottom(top + height)
{
}

322 323
/*!
  Constructs a rectangle with topLeft as the top-left corner and
pixhawk's avatar
pixhawk committed
324 325 326
  size as the rectangle size.
*/
QwtDoubleRect::QwtDoubleRect(
327
    const QwtDoublePoint &p, const QwtDoubleSize &size):
pixhawk's avatar
pixhawk committed
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
    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()));
}

348
/*!
pixhawk's avatar
pixhawk committed
349 350
  Set the x-range from x1 to x2 and the y-range from y1 to y2.
*/
351 352
void QwtDoubleRect::setRect(double left, double top,
                            double width, double height)
pixhawk's avatar
pixhawk committed
353 354 355 356 357 358 359 360
{
    d_left = left;
    d_right = left + width;
    d_top = top;
    d_bottom = top + height;
}

/*!
361
  Sets the size of the rectangle to size.
pixhawk's avatar
pixhawk committed
362 363 364 365 366 367 368 369 370
  Changes x2 and y2 only.
*/
void QwtDoubleRect::setSize(const QwtDoubleSize &size)
{
    setWidth(size.width());
    setHeight(size.height());
}

/*!
371 372
  Returns a normalized rectangle, i.e. a rectangle that has a non-negative
  width and height.
pixhawk's avatar
pixhawk committed
373

374
  It swaps x1 and x2 if x1() > x2(), and swaps y1 and y2 if y1() > y2().
pixhawk's avatar
pixhawk committed
375 376 377 378
*/
QwtDoubleRect QwtDoubleRect::normalized() const
{
    QwtDoubleRect r;
379
    if ( d_right < d_left ) {
pixhawk's avatar
pixhawk committed
380 381
        r.d_left = d_right;
        r.d_right = d_left;
382
    } else {
pixhawk's avatar
pixhawk committed
383
        r.d_left = d_left;
384
        r.d_right = d_right;
pixhawk's avatar
pixhawk committed
385
    }
386 387
    if ( d_bottom < d_top ) {
        r.d_top = d_bottom;
pixhawk's avatar
pixhawk committed
388
        r.d_bottom = d_top;
389
    } else {
pixhawk's avatar
pixhawk committed
390 391 392 393 394 395 396
        r.d_top = d_top;
        r.d_bottom = d_bottom;
    }
    return r;
}

/*!
397 398
  Returns the bounding rectangle of this rectangle and rectangle other.
  r.unite(s) is equivalent to r|s.
pixhawk's avatar
pixhawk committed
399 400 401 402 403 404 405
*/
QwtDoubleRect QwtDoubleRect::unite(const QwtDoubleRect &other) const
{
    return *this | other;
}

/*!
406 407
  Returns the intersection of this rectangle and rectangle other.
  r.intersect(s) is equivalent to r&s.
pixhawk's avatar
pixhawk committed
408 409 410 411 412 413 414
*/
QwtDoubleRect QwtDoubleRect::intersect(const QwtDoubleRect &other) const
{
    return *this & other;
}

/*!
415 416
  Returns true if this rectangle intersects with rectangle other;
  otherwise returns false.
pixhawk's avatar
pixhawk committed
417 418 419 420
*/
bool QwtDoubleRect::intersects(const QwtDoubleRect &other) const
{
    return ( qwtMax(d_left, other.d_left) <= qwtMin(d_right, other.d_right) ) &&
421
           ( qwtMax(d_top, other.d_top ) <= qwtMin(d_bottom, other.d_bottom) );
pixhawk's avatar
pixhawk committed
422 423
}

424
//! Returns true if this rect and other are equal; otherwise returns false.
pixhawk's avatar
pixhawk committed
425 426
bool QwtDoubleRect::operator==(const QwtDoubleRect &other) const
{
427 428
    return d_left == other.d_left && d_right == other.d_right &&
           d_top == other.d_top && d_bottom == other.d_bottom;
pixhawk's avatar
pixhawk committed
429 430
}

431
//! Returns true if this rect and other are different; otherwise returns false.
pixhawk's avatar
pixhawk committed
432 433 434 435 436 437
bool QwtDoubleRect::operator!=(const QwtDoubleRect &other) const
{
    return !operator==(other);
}

/*!
438 439 440
  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.
pixhawk's avatar
pixhawk committed
441 442 443
*/
QwtDoubleRect QwtDoubleRect::operator|(const QwtDoubleRect &other) const
{
444
    if ( isEmpty() )
pixhawk's avatar
pixhawk committed
445 446
        return other;

447
    if ( other.isEmpty() )
pixhawk's avatar
pixhawk committed
448
        return *this;
449

pixhawk's avatar
pixhawk committed
450 451 452 453 454 455 456 457 458
    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);
}

/*!
459 460
  Returns the intersection of this rectangle and rectangle other.
  Returns an empty rectangle if there is no intersection.
pixhawk's avatar
pixhawk committed
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
*/
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);
}

478
//! Unites this rectangle with rectangle other.
pixhawk's avatar
pixhawk committed
479 480 481 482 483 484
QwtDoubleRect &QwtDoubleRect::operator|=(const QwtDoubleRect &other)
{
    *this = *this | other;
    return *this;
}

485
//! Intersects this rectangle with rectangle other.
pixhawk's avatar
pixhawk committed
486 487 488 489 490 491
QwtDoubleRect &QwtDoubleRect::operator&=(const QwtDoubleRect &other)
{
    *this = *this & other;
    return *this;
}

492
//! Returns the center point of the rectangle.
pixhawk's avatar
pixhawk committed
493 494
QwtDoublePoint QwtDoubleRect::center() const
{
495 496
    return QwtDoublePoint(d_left + (d_right - d_left) / 2.0,
                          d_top + (d_bottom - d_top) / 2.0);
pixhawk's avatar
pixhawk committed
497 498 499
}

/*!
500 501
  Returns true if the point (x, y) is inside or on the edge of the rectangle;
  otherwise returns false.
pixhawk's avatar
pixhawk committed
502

503 504
  If proper is true, this function returns true only if p is inside
  (not on the edge).
pixhawk's avatar
pixhawk committed
505 506 507 508 509 510 511 512 513 514
*/
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;
}

/*!
515 516
  Returns true if the point p is inside or on the edge of the rectangle;
  otherwise returns false.
pixhawk's avatar
pixhawk committed
517

518 519
  If proper is true, this function returns true only if p is inside
  (not on the edge).
pixhawk's avatar
pixhawk committed
520 521 522 523 524 525 526
*/
bool QwtDoubleRect::contains(const QwtDoublePoint &p, bool proper) const
{
    return contains(p.x(), p.y(), proper);
}

/*!
527 528
  Returns true if the rectangle other is inside this rectangle;
  otherwise returns false.
pixhawk's avatar
pixhawk committed
529

530 531
  If proper is true, this function returns true only if other is entirely
  inside (not on the edge).
pixhawk's avatar
pixhawk committed
532 533 534
*/
bool QwtDoubleRect::contains(const QwtDoubleRect &other, bool proper) const
{
535 536
    return contains(other.d_left, other.d_top, proper) &&
           contains(other.d_right, other.d_bottom, proper);
pixhawk's avatar
pixhawk committed
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
}

//! 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