1
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
3
* Copyright (C) 1997 Josef Wilgen
4
* Copyright (C) 2002 Uwe Rathmann
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the Qwt License, Version 1.0
8
*****************************************************************************/
12
#if QT_VERSION < 0x040000
15
#include "qwt_double_rect.h"
18
Constructs a null point.
20
\sa QwtDoublePoint::isNull
22
QwtDoublePoint::QwtDoublePoint():
28
//! Constructs a point with coordinates specified by x and y.
29
QwtDoublePoint::QwtDoublePoint(double x, double y ):
38
Constructs a point using the values of the point specified.
40
QwtDoublePoint::QwtDoublePoint(const QPoint &p):
47
Returns true if point1 is equal to point2; otherwise returns false.
49
Two points are equal to each other if both x-coordinates and
50
both y-coordinates are the same.
52
bool QwtDoublePoint::operator==(const QwtDoublePoint &other) const
54
return (d_x == other.d_x) && (d_y == other.d_y);
57
//! Returns true if point1 is not equal to point2; otherwise returns false.
58
bool QwtDoublePoint::operator!=(const QwtDoublePoint &other) const
60
return !operator==(other);
64
Negates the coordinates of the point, and returns a point with the
65
new coordinates. (Inversion).
67
const QwtDoublePoint QwtDoublePoint::operator-() const
69
return QwtDoublePoint(-d_x, -d_y);
73
Adds the coordinates of the point to the corresponding coordinates of
74
the other point, and returns a point with the new coordinates.
77
const QwtDoublePoint QwtDoublePoint::operator+(
78
const QwtDoublePoint &other) const
80
return QwtDoublePoint(d_x + other.d_x, d_y + other.d_y);
84
Subtracts the coordinates of the other point from the corresponding
85
coordinates of the given point, and returns a point with the new
86
coordinates. (Vector subtraction.)
88
const QwtDoublePoint QwtDoublePoint::operator-(
89
const QwtDoublePoint &other) const
91
return QwtDoublePoint(d_x - other.d_x, d_y - other.d_y);
95
Multiplies the coordinates of the point by the given scale factor,
96
and returns a point with the new coordinates.
97
(Scalar multiplication of a vector.)
99
const QwtDoublePoint QwtDoublePoint::operator*(double factor) const
101
return QwtDoublePoint(d_x * factor, d_y * factor);
105
Divides the coordinates of the point by the given scale factor,
106
and returns a point with the new coordinates.
107
(Scalar division of a vector.)
109
const QwtDoublePoint QwtDoublePoint::operator/(double factor) const
111
return QwtDoublePoint(d_x / factor, d_y / factor);
115
Adds the coordinates of this point to the corresponding coordinates
116
of the other point, and returns a reference to this point with the
117
new coordinates. This is equivalent to vector addition.
119
QwtDoublePoint &QwtDoublePoint::operator+=(const QwtDoublePoint &other)
127
Subtracts the coordinates of the other point from the corresponding
128
coordinates of this point, and returns a reference to this point with
129
the new coordinates. This is equivalent to vector subtraction.
131
QwtDoublePoint &QwtDoublePoint::operator-=(const QwtDoublePoint &other)
139
Multiplies the coordinates of this point by the given scale factor,
140
and returns a reference to this point with the new coordinates.
141
This is equivalent to scalar multiplication of a vector.
143
QwtDoublePoint &QwtDoublePoint::operator*=(double factor)
151
Divides the coordinates of this point by the given scale factor,
152
and returns a references to this point with the new coordinates.
153
This is equivalent to scalar division of a vector.
155
QwtDoublePoint &QwtDoublePoint::operator/=(double factor)
162
//! Constructs an invalid size.
163
QwtDoubleSize::QwtDoubleSize():
169
//! Constructs a size with width width and height height.
170
QwtDoubleSize::QwtDoubleSize( double width, double height ):
176
//! Constructs a size with floating point accuracy from the given size.
177
QwtDoubleSize::QwtDoubleSize(const QSize &sz):
178
d_width(double(sz.width())),
179
d_height(double(sz.height()))
183
//! Swaps the width and height values.
184
void QwtDoubleSize::transpose()
186
double tmp = d_width;
192
Returns a size with the maximum width and height of this
195
QwtDoubleSize QwtDoubleSize::expandedTo(
196
const QwtDoubleSize &other) const
198
return QwtDoubleSize(
199
qwtMax(d_width, other.d_width),
200
qwtMax(d_height, other.d_height)
205
Returns a size with the minimum width and height of this size and other.
207
QwtDoubleSize QwtDoubleSize::boundedTo(
208
const QwtDoubleSize &other) const
210
return QwtDoubleSize(
211
qwtMin(d_width, other.d_width),
212
qwtMin(d_height, other.d_height)
216
//! Returns true if s1 and s2 are equal; otherwise returns false.
217
bool QwtDoubleSize::operator==(const QwtDoubleSize &other) const
219
return d_width == other.d_width && d_height == other.d_height;
222
//! Returns true if s1 and s2 are different; otherwise returns false.
223
bool QwtDoubleSize::operator!=(const QwtDoubleSize &other) const
225
return !operator==(other);
229
Returns the size formed by adding both components by
230
the components of other. Each component is added separately.
232
const QwtDoubleSize QwtDoubleSize::operator+(
233
const QwtDoubleSize &other) const
235
return QwtDoubleSize(d_width + other.d_width,
236
d_height + other.d_height);
240
Returns the size formed by subtracting both components by
241
the components of other. Each component is subtracted separately.
243
const QwtDoubleSize QwtDoubleSize::operator-(
244
const QwtDoubleSize &other) const
246
return QwtDoubleSize(d_width - other.d_width,
247
d_height - other.d_height);
250
//! Returns the size formed by multiplying both components by c.
251
const QwtDoubleSize QwtDoubleSize::operator*(double c) const
253
return QwtDoubleSize(d_width * c, d_height * c);
256
//! Returns the size formed by dividing both components by c.
257
const QwtDoubleSize QwtDoubleSize::operator/(double c) const
259
return QwtDoubleSize(d_width / c, d_height / c);
262
//! Adds size other to this size and returns a reference to this size.
263
QwtDoubleSize &QwtDoubleSize::operator+=(const QwtDoubleSize &other)
265
d_width += other.d_width;
266
d_height += other.d_height;
270
//! Subtracts size other from this size and returns a reference to this size.
271
QwtDoubleSize &QwtDoubleSize::operator-=(const QwtDoubleSize &other)
273
d_width -= other.d_width;
274
d_height -= other.d_height;
279
Multiplies this size's width and height by c,
280
and returns a reference to this size.
282
QwtDoubleSize &QwtDoubleSize::operator*=(double c)
290
Devides this size's width and height by c,
291
and returns a reference to this size.
293
QwtDoubleSize &QwtDoubleSize::operator/=(double c)
300
//! Constructs an rectangle with all components set to 0.0
301
QwtDoubleRect::QwtDoubleRect():
310
Constructs an rectangle with x1 to x2 as x-range and,
313
QwtDoubleRect::QwtDoubleRect(double left, double top,
314
double width, double height):
316
d_right(left + width),
318
d_bottom(top + height)
323
Constructs a rectangle with topLeft as the top-left corner and
324
size as the rectangle size.
326
QwtDoubleRect::QwtDoubleRect(
327
const QwtDoublePoint &p, const QwtDoubleSize &size):
329
d_right(p.x() + size.width()),
331
d_bottom(p.y() + size.height())
335
QwtDoubleRect::QwtDoubleRect(const QRect &rect):
337
d_right(rect.right()),
339
d_bottom(rect.bottom())
343
QRect QwtDoubleRect::toRect() const
345
return QRect(qRound(x()), qRound(y()), qRound(width()), qRound(height()));
349
Set the x-range from x1 to x2 and the y-range from y1 to y2.
351
void QwtDoubleRect::setRect(double left, double top,
352
double width, double height)
355
d_right = left + width;
357
d_bottom = top + height;
361
Sets the size of the rectangle to size.
362
Changes x2 and y2 only.
364
void QwtDoubleRect::setSize(const QwtDoubleSize &size)
366
setWidth(size.width());
367
setHeight(size.height());
371
Returns a normalized rectangle, i.e. a rectangle that has a non-negative
374
It swaps x1 and x2 if x1() > x2(), and swaps y1 and y2 if y1() > y2().
376
QwtDoubleRect QwtDoubleRect::normalized() const
379
if ( d_right < d_left )
389
if ( d_bottom < d_top )
397
r.d_bottom = d_bottom;
403
Returns the bounding rectangle of this rectangle and rectangle other.
404
r.unite(s) is equivalent to r|s.
406
QwtDoubleRect QwtDoubleRect::unite(const QwtDoubleRect &other) const
408
return *this | other;
412
Returns the intersection of this rectangle and rectangle other.
413
r.intersect(s) is equivalent to r&s.
415
QwtDoubleRect QwtDoubleRect::intersect(const QwtDoubleRect &other) const
417
return *this & other;
421
Returns true if this rectangle intersects with rectangle other;
422
otherwise returns false.
424
bool QwtDoubleRect::intersects(const QwtDoubleRect &other) const
426
return ( qwtMax(d_left, other.d_left) <= qwtMin(d_right, other.d_right) ) &&
427
( qwtMax(d_top, other.d_top ) <= qwtMin(d_bottom, other.d_bottom) );
430
//! Returns true if this rect and other are equal; otherwise returns false.
431
bool QwtDoubleRect::operator==(const QwtDoubleRect &other) const
433
return d_left == other.d_left && d_right == other.d_right &&
434
d_top == other.d_top && d_bottom == other.d_bottom;
437
//! Returns true if this rect and other are different; otherwise returns false.
438
bool QwtDoubleRect::operator!=(const QwtDoubleRect &other) const
440
return !operator==(other);
444
Returns the bounding rectangle of this rectangle and rectangle other.
445
The bounding rectangle of a nonempty rectangle and an empty or
446
invalid rectangle is defined to be the nonempty rectangle.
448
QwtDoubleRect QwtDoubleRect::operator|(const QwtDoubleRect &other) const
453
if ( other.isEmpty() )
456
const double minX = qwtMin(d_left, other.d_left);
457
const double maxX = qwtMax(d_right, other.d_right);
458
const double minY = qwtMin(d_top, other.d_top);
459
const double maxY = qwtMax(d_bottom, other.d_bottom);
461
return QwtDoubleRect(minX, minY, maxX - minX, maxY - minY);
465
Returns the intersection of this rectangle and rectangle other.
466
Returns an empty rectangle if there is no intersection.
468
QwtDoubleRect QwtDoubleRect::operator&(const QwtDoubleRect &other) const
470
if (isNull() || other.isNull())
471
return QwtDoubleRect();
473
const QwtDoubleRect r1 = normalized();
474
const QwtDoubleRect r2 = other.normalized();
476
const double minX = qwtMax(r1.left(), r2.left());
477
const double maxX = qwtMin(r1.right(), r2.right());
478
const double minY = qwtMax(r1.top(), r2.top());
479
const double maxY = qwtMin(r1.bottom(), r2.bottom());
481
return QwtDoubleRect(minX, minY, maxX - minX, maxY - minY);
484
//! Unites this rectangle with rectangle other.
485
QwtDoubleRect &QwtDoubleRect::operator|=(const QwtDoubleRect &other)
487
*this = *this | other;
491
//! Intersects this rectangle with rectangle other.
492
QwtDoubleRect &QwtDoubleRect::operator&=(const QwtDoubleRect &other)
494
*this = *this & other;
498
//! Returns the center point of the rectangle.
499
QwtDoublePoint QwtDoubleRect::center() const
501
return QwtDoublePoint(d_left + (d_right - d_left) / 2.0,
502
d_top + (d_bottom - d_top) / 2.0);
506
Returns true if the point (x, y) is inside or on the edge of the rectangle;
507
otherwise returns false.
509
If proper is true, this function returns true only if p is inside
512
bool QwtDoubleRect::contains(double x, double y, bool proper) const
515
return x > d_left && x < d_right && y > d_top && y < d_bottom;
517
return x >= d_left && x <= d_right && y >= d_top && y <= d_bottom;
521
Returns true if the point p is inside or on the edge of the rectangle;
522
otherwise returns false.
524
If proper is true, this function returns true only if p is inside
527
bool QwtDoubleRect::contains(const QwtDoublePoint &p, bool proper) const
529
return contains(p.x(), p.y(), proper);
533
Returns true if the rectangle other is inside this rectangle;
534
otherwise returns false.
536
If proper is true, this function returns true only if other is entirely
537
inside (not on the edge).
539
bool QwtDoubleRect::contains(const QwtDoubleRect &other, bool proper) const
541
return contains(other.d_left, other.d_top, proper) &&
542
contains(other.d_right, other.d_bottom, proper);
545
//! moves x1() to x, leaving the size unchanged
546
void QwtDoubleRect::moveLeft(double x)
548
const double w = width();
550
d_right = d_left + w;
553
//! moves x1() to x, leaving the size unchanged
554
void QwtDoubleRect::moveRight(double x)
556
const double w = width();
558
d_left = d_right - w;
561
//! moves y1() to y, leaving the size unchanged
562
void QwtDoubleRect::moveTop(double y)
564
const double h = height();
566
d_bottom = d_top + h;
569
//! moves y1() to y, leaving the size unchanged
570
void QwtDoubleRect::moveBottom(double y)
572
const double h = height();
574
d_top = d_bottom - h;
577
//! moves left() to x and top() to y, leaving the size unchanged
578
void QwtDoubleRect::moveTo(double x, double y)
584
//! moves x1() by dx and y1() by dy. leaving the size unchanged
585
void QwtDoubleRect::moveBy(double dx, double dy)
593
//! moves the center to pos, leaving the size unchanged
594
void QwtDoubleRect::moveCenter(const QwtDoublePoint &pos)
596
moveCenter(pos.x(), pos.y());
599
//! moves the center to (x, y), leaving the size unchanged
600
void QwtDoubleRect::moveCenter(double x, double y)
602
moveTo(x - width() / 2.0, y - height() / 2.0);
605
#endif // QT_VERSION < 0x040000