~ubuntu-branches/ubuntu/oneiric/qwt/oneiric-proposed

« back to all changes in this revision

Viewing changes to qwt-5.1.0/src/qwt_double_rect.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2008-05-26 10:26:31 UTC
  • mfrom: (1.1.3 upstream) (2.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080526102631-bp95mfccnrb957nx
Tags: 5.1.1-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
2
 
 * Qwt Widget Library
3
 
 * Copyright (C) 1997   Josef Wilgen
4
 
 * Copyright (C) 2002   Uwe Rathmann
5
 
 *
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
 
 *****************************************************************************/
9
 
 
10
 
#include <qglobal.h>
11
 
 
12
 
#if QT_VERSION < 0x040000
13
 
 
14
 
#include "qwt_math.h"
15
 
#include "qwt_double_rect.h"
16
 
 
17
 
/*! 
18
 
    Constructs a null point.
19
 
 
20
 
    \sa QwtDoublePoint::isNull
21
 
*/
22
 
QwtDoublePoint::QwtDoublePoint():
23
 
    d_x(0.0),
24
 
    d_y(0.0)
25
 
{
26
 
}
27
 
 
28
 
//! Constructs a point with coordinates specified by x and y.
29
 
QwtDoublePoint::QwtDoublePoint(double x, double y ):
30
 
    d_x(x),
31
 
    d_y(y)
32
 
{
33
 
}
34
 
 
35
 
/*! 
36
 
    Copy constructor. 
37
 
 
38
 
    Constructs a point using the values of the point specified.
39
 
*/
40
 
QwtDoublePoint::QwtDoublePoint(const QPoint &p):
41
 
    d_x(double(p.x())),
42
 
    d_y(double(p.y()))
43
 
{
44
 
}
45
 
 
46
 
/*! 
47
 
    Returns true if point1 is equal to point2; otherwise returns false.
48
 
 
49
 
    Two points are equal to each other if both x-coordinates and 
50
 
    both y-coordinates are the same.
51
 
*/
52
 
bool QwtDoublePoint::operator==(const QwtDoublePoint &other) const
53
 
{
54
 
    return (d_x == other.d_x) && (d_y == other.d_y);
55
 
}
56
 
 
57
 
//! Returns true if point1 is not equal to point2; otherwise returns false.
58
 
bool QwtDoublePoint::operator!=(const QwtDoublePoint &other) const
59
 
{
60
 
    return !operator==(other);
61
 
}
62
 
 
63
 
/*! 
64
 
    Negates the coordinates of the point, and returns a point with the 
65
 
    new coordinates. (Inversion).
66
 
*/
67
 
const QwtDoublePoint QwtDoublePoint::operator-() const
68
 
{
69
 
    return QwtDoublePoint(-d_x, -d_y);
70
 
}
71
 
 
72
 
/*! 
73
 
    Adds the coordinates of the point to the corresponding coordinates of 
74
 
    the other point, and returns a point with the new coordinates. 
75
 
    (Vector addition.)
76
 
*/
77
 
const QwtDoublePoint QwtDoublePoint::operator+(
78
 
    const QwtDoublePoint &other) const
79
 
{
80
 
    return QwtDoublePoint(d_x + other.d_x, d_y + other.d_y);
81
 
}
82
 
 
83
 
/*! 
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.)
87
 
*/
88
 
const QwtDoublePoint QwtDoublePoint::operator-(
89
 
    const QwtDoublePoint &other) const
90
 
{
91
 
    return QwtDoublePoint(d_x - other.d_x, d_y - other.d_y);
92
 
}
93
 
 
94
 
/*!
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.)
98
 
*/
99
 
const QwtDoublePoint QwtDoublePoint::operator*(double factor) const
100
 
{
101
 
    return QwtDoublePoint(d_x * factor, d_y * factor);
102
 
}
103
 
 
104
 
/*!
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.)
108
 
*/
109
 
const QwtDoublePoint QwtDoublePoint::operator/(double factor) const
110
 
{
111
 
    return QwtDoublePoint(d_x / factor, d_y / factor);
112
 
}
113
 
 
114
 
/*!
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.
118
 
*/
119
 
QwtDoublePoint &QwtDoublePoint::operator+=(const QwtDoublePoint &other)
120
 
{
121
 
    d_x += other.d_x;
122
 
    d_y += other.d_y;
123
 
    return *this;
124
 
}
125
 
 
126
 
/*!
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.
130
 
*/
131
 
QwtDoublePoint &QwtDoublePoint::operator-=(const QwtDoublePoint &other)
132
 
{
133
 
    d_x -= other.d_x;
134
 
    d_y -= other.d_y;
135
 
    return *this;
136
 
}
137
 
 
138
 
/*!
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.
142
 
*/
143
 
QwtDoublePoint &QwtDoublePoint::operator*=(double factor)
144
 
{
145
 
    d_x *= factor;
146
 
    d_y *= factor;
147
 
    return *this;
148
 
}
149
 
 
150
 
/*!
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.
154
 
*/
155
 
QwtDoublePoint &QwtDoublePoint::operator/=(double factor)
156
 
{
157
 
    d_x /= factor;
158
 
    d_y /= factor;
159
 
    return *this;
160
 
}
161
 
 
162
 
//! Constructs an invalid size.
163
 
QwtDoubleSize::QwtDoubleSize():
164
 
    d_width(-1.0),
165
 
    d_height(-1.0)
166
 
{   
167
 
}   
168
 
 
169
 
//! Constructs a size with width width and height height.
170
 
QwtDoubleSize::QwtDoubleSize( double width, double height ):
171
 
    d_width(width),
172
 
    d_height(height)
173
 
{   
174
 
}   
175
 
 
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()))
180
 
{   
181
 
}   
182
 
 
183
 
//! Swaps the width and height values.
184
 
void QwtDoubleSize::transpose()
185
 
{   
186
 
    double tmp = d_width;
187
 
    d_width = d_height;
188
 
    d_height = tmp;
189
 
}
190
 
 
191
 
/*! 
192
 
    Returns a size with the maximum width and height of this 
193
 
    size and other.
194
 
*/
195
 
QwtDoubleSize QwtDoubleSize::expandedTo(
196
 
    const QwtDoubleSize &other) const
197
 
{   
198
 
    return QwtDoubleSize(
199
 
        qwtMax(d_width, other.d_width),
200
 
        qwtMax(d_height, other.d_height)
201
 
    );  
202
 
}   
203
 
 
204
 
/*!
205
 
    Returns a size with the minimum width and height of this size and other.
206
 
*/
207
 
QwtDoubleSize QwtDoubleSize::boundedTo(
208
 
    const QwtDoubleSize &other) const
209
 
{   
210
 
    return QwtDoubleSize(
211
 
        qwtMin(d_width, other.d_width),
212
 
        qwtMin(d_height, other.d_height)
213
 
    );  
214
 
}   
215
 
 
216
 
//! Returns true if s1 and s2 are equal; otherwise returns false.
217
 
bool QwtDoubleSize::operator==(const QwtDoubleSize &other) const
218
 
219
 
    return d_width == other.d_width && d_height == other.d_height;
220
 
}   
221
 
 
222
 
//! Returns true if s1 and s2 are different; otherwise returns false.
223
 
bool QwtDoubleSize::operator!=(const QwtDoubleSize &other) const
224
 
225
 
    return !operator==(other);
226
 
}   
227
 
 
228
 
/*! 
229
 
  Returns the size formed by adding both components by
230
 
  the components of other. Each component is added separately.
231
 
*/
232
 
const QwtDoubleSize QwtDoubleSize::operator+(
233
 
    const QwtDoubleSize &other) const
234
 
{   
235
 
    return QwtDoubleSize(d_width + other.d_width,
236
 
        d_height + other.d_height); 
237
 
}       
238
 
 
239
 
/*! 
240
 
  Returns the size formed by subtracting both components by
241
 
  the components of other. Each component is subtracted separately.
242
 
*/  
243
 
const QwtDoubleSize QwtDoubleSize::operator-(
244
 
    const QwtDoubleSize &other) const
245
 
{   
246
 
    return QwtDoubleSize(d_width - other.d_width,
247
 
        d_height - other.d_height); 
248
 
}       
249
 
 
250
 
//! Returns the size formed by multiplying both components by c.
251
 
const QwtDoubleSize QwtDoubleSize::operator*(double c) const
252
 
253
 
    return QwtDoubleSize(d_width * c, d_height * c);
254
 
}   
255
 
 
256
 
//! Returns the size formed by dividing both components by c.
257
 
const QwtDoubleSize QwtDoubleSize::operator/(double c) const
258
 
259
 
    return QwtDoubleSize(d_width / c, d_height / c);
260
 
}   
261
 
 
262
 
//! Adds size other to this size and returns a reference to this size.
263
 
QwtDoubleSize &QwtDoubleSize::operator+=(const QwtDoubleSize &other)
264
 
{   
265
 
    d_width += other.d_width; 
266
 
    d_height += other.d_height;
267
 
    return *this;
268
 
}
269
 
 
270
 
//! Subtracts size other from this size and returns a reference to this size.
271
 
QwtDoubleSize &QwtDoubleSize::operator-=(const QwtDoubleSize &other)
272
 
{   
273
 
    d_width -= other.d_width; 
274
 
    d_height -= other.d_height;
275
 
    return *this;
276
 
}
277
 
 
278
 
/* 
279
 
  Multiplies this size's width and height by c, 
280
 
  and returns a reference to this size.
281
 
*/
282
 
QwtDoubleSize &QwtDoubleSize::operator*=(double c)
283
 
{   
284
 
    d_width *= c; 
285
 
    d_height *= c;
286
 
    return *this;
287
 
}
288
 
 
289
 
/* 
290
 
  Devides this size's width and height by c, 
291
 
  and returns a reference to this size.
292
 
*/
293
 
QwtDoubleSize &QwtDoubleSize::operator/=(double c)
294
 
{
295
 
    d_width /= c;
296
 
    d_height /= c;
297
 
    return *this;
298
 
}   
299
 
 
300
 
//! Constructs an rectangle with all components set to 0.0 
301
 
QwtDoubleRect::QwtDoubleRect():
302
 
    d_left(0.0),
303
 
    d_right(0.0),
304
 
    d_top(0.0),
305
 
    d_bottom(0.0)
306
 
{
307
 
}
308
 
 
309
 
/*! 
310
 
  Constructs an rectangle with x1 to x2 as x-range and,
311
 
  y1 to y2 as y-range.
312
 
*/
313
 
QwtDoubleRect::QwtDoubleRect(double left, double top,
314
 
        double width, double height):
315
 
    d_left(left),
316
 
    d_right(left + width),
317
 
    d_top(top),
318
 
    d_bottom(top + height)
319
 
{
320
 
}
321
 
 
322
 
/*! 
323
 
  Constructs a rectangle with topLeft as the top-left corner and 
324
 
  size as the rectangle size.
325
 
*/
326
 
QwtDoubleRect::QwtDoubleRect(
327
 
        const QwtDoublePoint &p, const QwtDoubleSize &size):
328
 
    d_left(p.x()),
329
 
    d_right(p.x() + size.width()),
330
 
    d_top(p.y()),
331
 
    d_bottom(p.y() + size.height())
332
 
{
333
 
}
334
 
 
335
 
QwtDoubleRect::QwtDoubleRect(const QRect &rect):
336
 
    d_left(rect.left()),
337
 
    d_right(rect.right()),
338
 
    d_top(rect.top()),
339
 
    d_bottom(rect.bottom())
340
 
{
341
 
}
342
 
 
343
 
QRect QwtDoubleRect::toRect() const
344
 
{
345
 
    return QRect(qRound(x()), qRound(y()), qRound(width()), qRound(height()));
346
 
}
347
 
 
348
 
/*! 
349
 
  Set the x-range from x1 to x2 and the y-range from y1 to y2.
350
 
*/
351
 
void QwtDoubleRect::setRect(double left, double top, 
352
 
    double width, double height)
353
 
{
354
 
    d_left = left;
355
 
    d_right = left + width;
356
 
    d_top = top;
357
 
    d_bottom = top + height;
358
 
}
359
 
 
360
 
/*!
361
 
  Sets the size of the rectangle to size. 
362
 
  Changes x2 and y2 only.
363
 
*/
364
 
void QwtDoubleRect::setSize(const QwtDoubleSize &size)
365
 
{
366
 
    setWidth(size.width());
367
 
    setHeight(size.height());
368
 
}
369
 
 
370
 
/*!
371
 
  Returns a normalized rectangle, i.e. a rectangle that has a non-negative 
372
 
  width and height. 
373
 
 
374
 
  It swaps x1 and x2 if x1() > x2(), and swaps y1 and y2 if y1() > y2(). 
375
 
*/
376
 
QwtDoubleRect QwtDoubleRect::normalized() const
377
 
{
378
 
    QwtDoubleRect r;
379
 
    if ( d_right < d_left ) 
380
 
    {
381
 
        r.d_left = d_right;
382
 
        r.d_right = d_left;
383
 
    } 
384
 
    else 
385
 
    {
386
 
        r.d_left = d_left;
387
 
        r.d_right = d_right; 
388
 
    }
389
 
    if ( d_bottom < d_top ) 
390
 
    { 
391
 
        r.d_top = d_bottom; 
392
 
        r.d_bottom = d_top;
393
 
    } 
394
 
    else 
395
 
    {
396
 
        r.d_top = d_top;
397
 
        r.d_bottom = d_bottom;
398
 
    }
399
 
    return r;
400
 
}
401
 
 
402
 
/*!
403
 
  Returns the bounding rectangle of this rectangle and rectangle other. 
404
 
  r.unite(s) is equivalent to r|s. 
405
 
*/
406
 
QwtDoubleRect QwtDoubleRect::unite(const QwtDoubleRect &other) const
407
 
{
408
 
    return *this | other;
409
 
}
410
 
 
411
 
/*!
412
 
  Returns the intersection of this rectangle and rectangle other. 
413
 
  r.intersect(s) is equivalent to r&s. 
414
 
*/
415
 
QwtDoubleRect QwtDoubleRect::intersect(const QwtDoubleRect &other) const
416
 
{
417
 
    return *this & other;
418
 
}
419
 
 
420
 
/*!
421
 
  Returns true if this rectangle intersects with rectangle other; 
422
 
  otherwise returns false. 
423
 
*/
424
 
bool QwtDoubleRect::intersects(const QwtDoubleRect &other) const
425
 
{
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) );
428
 
}
429
 
 
430
 
//! Returns true if this rect and other are equal; otherwise returns false. 
431
 
bool QwtDoubleRect::operator==(const QwtDoubleRect &other) const
432
 
{
433
 
    return d_left == other.d_left && d_right == other.d_right && 
434
 
        d_top == other.d_top && d_bottom == other.d_bottom;
435
 
}
436
 
 
437
 
//! Returns true if this rect and other are different; otherwise returns false. 
438
 
bool QwtDoubleRect::operator!=(const QwtDoubleRect &other) const
439
 
{
440
 
    return !operator==(other);
441
 
}
442
 
 
443
 
/*!
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. 
447
 
*/
448
 
QwtDoubleRect QwtDoubleRect::operator|(const QwtDoubleRect &other) const
449
 
{
450
 
    if ( isEmpty() ) 
451
 
        return other;
452
 
 
453
 
    if ( other.isEmpty() ) 
454
 
        return *this;
455
 
        
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);
460
 
 
461
 
    return QwtDoubleRect(minX, minY, maxX - minX, maxY - minY);
462
 
}
463
 
 
464
 
/*!
465
 
  Returns the intersection of this rectangle and rectangle other. 
466
 
  Returns an empty rectangle if there is no intersection. 
467
 
*/
468
 
QwtDoubleRect QwtDoubleRect::operator&(const QwtDoubleRect &other) const
469
 
{
470
 
    if (isNull() || other.isNull())
471
 
        return QwtDoubleRect();
472
 
 
473
 
    const QwtDoubleRect r1 = normalized();
474
 
    const QwtDoubleRect r2 = other.normalized();
475
 
 
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());
480
 
 
481
 
    return QwtDoubleRect(minX, minY, maxX - minX, maxY - minY);
482
 
}
483
 
 
484
 
//! Unites this rectangle with rectangle other. 
485
 
QwtDoubleRect &QwtDoubleRect::operator|=(const QwtDoubleRect &other)
486
 
{
487
 
    *this = *this | other;
488
 
    return *this;
489
 
}
490
 
 
491
 
//! Intersects this rectangle with rectangle other. 
492
 
QwtDoubleRect &QwtDoubleRect::operator&=(const QwtDoubleRect &other)
493
 
{
494
 
    *this = *this & other;
495
 
    return *this;
496
 
}
497
 
 
498
 
//! Returns the center point of the rectangle. 
499
 
QwtDoublePoint QwtDoubleRect::center() const
500
 
{
501
 
    return QwtDoublePoint(d_left + (d_right - d_left) / 2.0, 
502
 
        d_top + (d_bottom - d_top) / 2.0);
503
 
}
504
 
 
505
 
/*!
506
 
  Returns true if the point (x, y) is inside or on the edge of the rectangle; 
507
 
  otherwise returns false. 
508
 
 
509
 
  If proper is true, this function returns true only if p is inside 
510
 
  (not on the edge). 
511
 
*/
512
 
bool QwtDoubleRect::contains(double x, double y, bool proper) const
513
 
{
514
 
    if ( proper )
515
 
        return x > d_left && x < d_right && y > d_top && y < d_bottom;
516
 
    else
517
 
        return x >= d_left && x <= d_right && y >= d_top && y <= d_bottom;
518
 
}
519
 
 
520
 
/*!
521
 
  Returns true if the point p is inside or on the edge of the rectangle; 
522
 
  otherwise returns false. 
523
 
 
524
 
  If proper is true, this function returns true only if p is inside 
525
 
  (not on the edge). 
526
 
*/
527
 
bool QwtDoubleRect::contains(const QwtDoublePoint &p, bool proper) const
528
 
{
529
 
    return contains(p.x(), p.y(), proper);
530
 
}
531
 
 
532
 
/*!
533
 
  Returns true if the rectangle other is inside this rectangle; 
534
 
  otherwise returns false. 
535
 
 
536
 
  If proper is true, this function returns true only if other is entirely 
537
 
  inside (not on the edge). 
538
 
*/
539
 
bool QwtDoubleRect::contains(const QwtDoubleRect &other, bool proper) const
540
 
{
541
 
    return contains(other.d_left, other.d_top, proper) && 
542
 
        contains(other.d_right, other.d_bottom, proper);
543
 
}
544
 
 
545
 
//! moves x1() to x, leaving the size unchanged
546
 
void QwtDoubleRect::moveLeft(double x)
547
 
{
548
 
    const double w = width();
549
 
    d_left = x;
550
 
    d_right = d_left + w;
551
 
}
552
 
 
553
 
//! moves x1() to x, leaving the size unchanged
554
 
void QwtDoubleRect::moveRight(double x)
555
 
{
556
 
    const double w = width();
557
 
    d_right = x;
558
 
    d_left = d_right - w;
559
 
}
560
 
 
561
 
//! moves y1() to y, leaving the size unchanged
562
 
void QwtDoubleRect::moveTop(double y)
563
 
{
564
 
    const double h = height();
565
 
    d_top = y;
566
 
    d_bottom = d_top + h;
567
 
}
568
 
 
569
 
//! moves y1() to y, leaving the size unchanged
570
 
void QwtDoubleRect::moveBottom(double y)
571
 
{
572
 
    const double h = height();
573
 
    d_bottom = y;
574
 
    d_top = d_bottom - h;
575
 
}
576
 
 
577
 
//! moves left() to x and top() to y, leaving the size unchanged
578
 
void QwtDoubleRect::moveTo(double x, double y)
579
 
{
580
 
    moveLeft(x);
581
 
    moveTop(y);
582
 
}
583
 
 
584
 
//! moves x1() by dx and y1() by dy. leaving the size unchanged
585
 
void QwtDoubleRect::moveBy(double dx, double dy)
586
 
{
587
 
    d_left += dx;
588
 
    d_right += dx;
589
 
    d_top += dy;
590
 
    d_bottom += dy;
591
 
}
592
 
 
593
 
//! moves the center to pos, leaving the size unchanged
594
 
void QwtDoubleRect::moveCenter(const QwtDoublePoint &pos)
595
 
{
596
 
    moveCenter(pos.x(), pos.y());
597
 
}
598
 
 
599
 
//! moves the center to (x, y), leaving the size unchanged
600
 
void QwtDoubleRect::moveCenter(double x, double y)
601
 
{
602
 
    moveTo(x - width() / 2.0, y - height() / 2.0);
603
 
}
604
 
 
605
 
#endif // QT_VERSION < 0x040000