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
*****************************************************************************/
11
#if QT_VERSION >= 0x040000
12
#include <qalgorithms.h>
18
#include "qwt_double_interval.h"
21
\brief Normalize the limits of the interval
23
If maxValue() < minValue() the limits will be inverted.
24
\return Normalized interval
26
\sa isValid(), inverted()
28
QwtDoubleInterval QwtDoubleInterval::normalized() const
30
if ( d_minValue > d_maxValue )
34
if ( d_minValue == d_maxValue && d_borderFlags == ExcludeMinimum )
43
Invert the limits of the interval
44
\return Inverted interval
47
QwtDoubleInterval QwtDoubleInterval::inverted() const
50
if ( d_borderFlags & ExcludeMinimum )
51
borderFlags |= ExcludeMaximum;
52
if ( d_borderFlags & ExcludeMaximum )
53
borderFlags |= ExcludeMinimum;
55
return QwtDoubleInterval(d_maxValue, d_minValue, borderFlags);
59
Test if a value is inside an interval
62
\return true, if value >= minValue() && value <= maxValue()
64
bool QwtDoubleInterval::contains(double value) const
69
if ( value < d_minValue || value > d_maxValue )
72
if ( value == d_minValue && d_borderFlags & ExcludeMinimum )
75
if ( value == d_maxValue && d_borderFlags & ExcludeMaximum )
82
QwtDoubleInterval QwtDoubleInterval::unite(
83
const QwtDoubleInterval &other) const
86
If one of the intervals is invalid return the other one.
87
If both are invalid return an invalid default interval
91
if ( !other.isValid() )
92
return QwtDoubleInterval();
96
if ( !other.isValid() )
99
QwtDoubleInterval united;
103
if ( d_minValue < other.minValue() )
105
united.setMinValue(d_minValue);
106
flags &= d_borderFlags & ExcludeMinimum;
108
else if ( other.minValue() < d_minValue )
110
united.setMinValue(other.minValue());
111
flags &= other.borderFlags() & ExcludeMinimum;
113
else // d_minValue == other.minValue()
115
united.setMinValue(d_minValue);
116
flags &= (d_borderFlags & other.borderFlags()) & ExcludeMinimum;
120
if ( d_maxValue > other.maxValue() )
122
united.setMaxValue(d_maxValue);
123
flags &= d_borderFlags & ExcludeMaximum;
125
else if ( other.maxValue() > d_maxValue )
127
united.setMaxValue(other.maxValue());
128
flags &= other.borderFlags() & ExcludeMaximum;
130
else // d_maxValue == other.maxValue() )
132
united.setMaxValue(d_maxValue);
133
flags &= d_borderFlags & other.borderFlags() & ExcludeMaximum;
136
united.setBorderFlags(flags);
140
//! Intersect 2 intervals
141
QwtDoubleInterval QwtDoubleInterval::intersect(
142
const QwtDoubleInterval &other) const
144
if ( !other.isValid() || !isValid() )
145
return QwtDoubleInterval();
147
QwtDoubleInterval i1 = *this;
148
QwtDoubleInterval i2 = other;
150
// swap i1/i2, so that the minimum of i1
151
// is smaller then the minimum of i2
153
if ( i1.minValue() > i2.minValue() )
157
else if ( i1.minValue() == i2.minValue() )
159
if ( i1.borderFlags() & ExcludeMinimum )
163
if ( i1.maxValue() < i2.minValue() )
165
return QwtDoubleInterval();
168
if ( i1.maxValue() == i2.minValue() )
170
if ( i1.borderFlags() & ExcludeMaximum ||
171
i2.borderFlags() & ExcludeMinimum )
173
return QwtDoubleInterval();
177
QwtDoubleInterval intersected;
180
intersected.setMinValue(i2.minValue());
181
flags |= i2.borderFlags() & ExcludeMinimum;
183
if ( i1.maxValue() < i2.maxValue() )
185
intersected.setMaxValue(i1.maxValue());
186
flags |= i1.borderFlags() & ExcludeMaximum;
188
else if ( i2.maxValue() < i1.maxValue() )
190
intersected.setMaxValue(i2.maxValue());
191
flags |= i2.borderFlags() & ExcludeMaximum;
193
else // i1.maxValue() == i2.maxValue()
195
intersected.setMaxValue(i1.maxValue() );
196
flags |= i1.borderFlags() & i2.borderFlags() & ExcludeMaximum;
199
intersected.setBorderFlags(flags);
203
//! Unites this interval with the given interval.
204
QwtDoubleInterval& QwtDoubleInterval::operator|=(
205
const QwtDoubleInterval &interval)
207
*this = *this | interval;
211
//! Intersects this interval with the given interval.
212
QwtDoubleInterval& QwtDoubleInterval::operator&=(
213
const QwtDoubleInterval &interval)
215
*this = *this & interval;
220
Test if two intervals overlap
222
bool QwtDoubleInterval::intersects(const QwtDoubleInterval &other) const
224
if ( !isValid() || !other.isValid() )
227
QwtDoubleInterval i1 = *this;
228
QwtDoubleInterval i2 = other;
230
// swap i1/i2, so that the minimum of i1
231
// is smaller then the minimum of i2
233
if ( i1.minValue() > i2.minValue() )
237
else if ( i1.minValue() == i2.minValue() &&
238
i1.borderFlags() & ExcludeMinimum )
243
if ( i1.maxValue() > i2.minValue() )
247
if ( i1.maxValue() == i2.minValue() )
249
return !( (i1.borderFlags() & ExcludeMaximum) ||
250
(i2.borderFlags() & ExcludeMinimum) );
256
Adjust the limit that is closer to value, so that value becomes
257
the center of the interval.
260
\return Interval with value as center
262
QwtDoubleInterval QwtDoubleInterval::symmetrize(double value) const
268
qwtMax(qwtAbs(value - d_maxValue), qwtAbs(value - d_minValue));
270
return QwtDoubleInterval(value - delta, value + delta);
274
Limit the interval, keeping the border modes
276
\param lowerBound Lower limit
277
\param upperBound Upper limit
279
\return Limited interval
281
QwtDoubleInterval QwtDoubleInterval::limited(
282
double lowerBound, double upperBound) const
284
if ( !isValid() || lowerBound > upperBound )
285
return QwtDoubleInterval();
287
double minValue = qwtMax(d_minValue, lowerBound);
288
minValue = qwtMin(minValue, upperBound);
290
double maxValue = qwtMax(d_maxValue, lowerBound);
291
maxValue = qwtMin(maxValue, upperBound);
293
return QwtDoubleInterval(minValue, maxValue, d_borderFlags);
299
If value is below minValue, value becomes the lower limit.
300
If value is above maxValue, value becomes the upper limit.
302
extend has no effect for invalid intervals
307
QwtDoubleInterval QwtDoubleInterval::extend(double value) const
312
return QwtDoubleInterval( qwtMin(value, d_minValue),
313
qwtMax(value, d_maxValue), d_borderFlags );
316
QwtDoubleInterval& QwtDoubleInterval::operator|=(double value)
318
*this = *this | value;