Qwt User's Guide 6.3.0
Loading...
Searching...
No Matches
qwt_interval.cpp
1/******************************************************************************
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 "qwt_interval.h"
11
12namespace
13{
14 static const struct RegisterQwtInterval
15 {
16 inline RegisterQwtInterval() { qRegisterMetaType< QwtInterval >(); }
17
18 } qwtRegisterQwtInterval;
19}
20
30{
31 if ( m_minValue > m_maxValue )
32 {
33 return inverted();
34 }
35 if ( m_minValue == m_maxValue && m_borderFlags == ExcludeMinimum )
36 {
37 return inverted();
38 }
39
40 return *this;
41}
42
49{
51
52 if ( m_borderFlags & ExcludeMinimum )
54
55 if ( m_borderFlags & ExcludeMaximum )
57
58 return QwtInterval( m_maxValue, m_minValue, borderFlags );
59}
60
67bool QwtInterval::contains( double value ) const
68{
69 if ( !isValid() )
70 return false;
71
72 if ( ( value < m_minValue ) || ( value > m_maxValue ) )
73 return false;
74
75 if ( ( value == m_minValue ) && ( m_borderFlags & ExcludeMinimum ) )
76 return false;
77
78 if ( ( value == m_maxValue ) && ( m_borderFlags & ExcludeMaximum ) )
79 return false;
80
81 return true;
82}
83
90bool QwtInterval::contains( const QwtInterval& interval ) const
91{
92 if ( !isValid() || !interval.isValid() )
93 return false;
94
95 if ( ( interval.m_minValue < m_minValue ) || ( interval.m_maxValue > m_maxValue ) )
96 return false;
97
98 if ( m_borderFlags )
99 {
100 if ( interval.m_minValue == m_minValue )
101 {
102 if ( ( m_borderFlags & ExcludeMinimum )
103 && !( interval.m_borderFlags & ExcludeMinimum ) )
104 {
105 return false;
106 }
107 }
108
109 if ( interval.m_maxValue == m_maxValue )
110 {
111 if ( ( m_borderFlags & ExcludeMaximum )
112 && !( interval.m_borderFlags & ExcludeMaximum ) )
113 {
114 return false;
115 }
116 }
117 }
118
119 return true;
120}
121
124{
125 /*
126 If one of the intervals is invalid return the other one.
127 If both are invalid return an invalid default interval
128 */
129 if ( !isValid() )
130 {
131 if ( !other.isValid() )
132 return QwtInterval();
133 else
134 return other;
135 }
136 if ( !other.isValid() )
137 return *this;
138
139 QwtInterval united;
141
142 // minimum
143 if ( m_minValue < other.minValue() )
144 {
145 united.setMinValue( m_minValue );
146 flags &= m_borderFlags & ExcludeMinimum;
147 }
148 else if ( other.minValue() < m_minValue )
149 {
150 united.setMinValue( other.minValue() );
151 flags &= other.borderFlags() & ExcludeMinimum;
152 }
153 else // m_minValue == other.minValue()
154 {
155 united.setMinValue( m_minValue );
156 flags &= ( m_borderFlags & other.borderFlags() ) & ExcludeMinimum;
157 }
158
159 // maximum
160 if ( m_maxValue > other.maxValue() )
161 {
162 united.setMaxValue( m_maxValue );
163 flags &= m_borderFlags & ExcludeMaximum;
164 }
165 else if ( other.maxValue() > m_maxValue )
166 {
167 united.setMaxValue( other.maxValue() );
168 flags &= other.borderFlags() & ExcludeMaximum;
169 }
170 else // m_maxValue == other.maxValue() )
171 {
172 united.setMaxValue( m_maxValue );
173 flags &= m_borderFlags & other.borderFlags() & ExcludeMaximum;
174 }
175
176 united.setBorderFlags( flags );
177 return united;
178}
179
187{
188 if ( !other.isValid() || !isValid() )
189 return QwtInterval();
190
191 QwtInterval i1 = *this;
192 QwtInterval i2 = other;
193
194 // swap i1/i2, so that the minimum of i1
195 // is smaller then the minimum of i2
196
197 if ( i1.minValue() > i2.minValue() )
198 {
199 qSwap( i1, i2 );
200 }
201 else if ( i1.minValue() == i2.minValue() )
202 {
203 if ( i1.borderFlags() & ExcludeMinimum )
204 qSwap( i1, i2 );
205 }
206
207 if ( i1.maxValue() < i2.minValue() )
208 {
209 return QwtInterval();
210 }
211
212 if ( i1.maxValue() == i2.minValue() )
213 {
214 if ( i1.borderFlags() & ExcludeMaximum ||
216 {
217 return QwtInterval();
218 }
219 }
220
221 QwtInterval intersected;
223
224 intersected.setMinValue( i2.minValue() );
225 flags |= i2.borderFlags() & ExcludeMinimum;
226
227 if ( i1.maxValue() < i2.maxValue() )
228 {
229 intersected.setMaxValue( i1.maxValue() );
230 flags |= i1.borderFlags() & ExcludeMaximum;
231 }
232 else if ( i2.maxValue() < i1.maxValue() )
233 {
234 intersected.setMaxValue( i2.maxValue() );
235 flags |= i2.borderFlags() & ExcludeMaximum;
236 }
237 else // i1.maxValue() == i2.maxValue()
238 {
239 intersected.setMaxValue( i1.maxValue() );
240 flags |= i1.borderFlags() & i2.borderFlags() & ExcludeMaximum;
241 }
242
243 intersected.setBorderFlags( flags );
244 return intersected;
245}
246
254{
255 *this = *this | other;
256 return *this;
257}
258
266{
267 *this = *this & other;
268 return *this;
269}
270
277bool QwtInterval::intersects( const QwtInterval& other ) const
278{
279 if ( !isValid() || !other.isValid() )
280 return false;
281
282 QwtInterval i1 = *this;
283 QwtInterval i2 = other;
284
285 // swap i1/i2, so that the minimum of i1
286 // is smaller then the minimum of i2
287
288 if ( i1.minValue() > i2.minValue() )
289 {
290 qSwap( i1, i2 );
291 }
292 else if ( i1.minValue() == i2.minValue() &&
294 {
295 qSwap( i1, i2 );
296 }
297
298 if ( i1.maxValue() > i2.minValue() )
299 {
300 return true;
301 }
302 if ( i1.maxValue() == i2.minValue() )
303 {
304 return !( ( i1.borderFlags() & ExcludeMaximum ) ||
305 ( i2.borderFlags() & ExcludeMinimum ) );
306 }
307 return false;
308}
309
318{
319 if ( !isValid() )
320 return *this;
321
322 const double delta =
323 qMax( qAbs( value - m_maxValue ), qAbs( value - m_minValue ) );
324
325 return QwtInterval( value - delta, value + delta );
326}
327
336QwtInterval QwtInterval::limited( double lowerBound, double upperBound ) const
337{
338 if ( !isValid() || lowerBound > upperBound )
339 return QwtInterval();
340
341 double minValue = qMax( m_minValue, lowerBound );
342 minValue = qMin( minValue, upperBound );
343
344 double maxValue = qMax( m_maxValue, lowerBound );
345 maxValue = qMin( maxValue, upperBound );
346
347 return QwtInterval( minValue, maxValue, m_borderFlags );
348}
349
363QwtInterval QwtInterval::extend( double value ) const
364{
365 if ( !isValid() )
366 return *this;
367
368 return QwtInterval( qMin( value, m_minValue ),
369 qMax( value, m_maxValue ), m_borderFlags );
370}
371
381{
382 *this = *this | value;
383 return *this;
384}
385
386#ifndef QT_NO_DEBUG_STREAM
387
388#include <qdebug.h>
389
390QDebug operator<<( QDebug debug, const QwtInterval& interval )
391{
392 const int flags = interval.borderFlags();
393
394 debug.nospace() << "QwtInterval("
395 << ( ( flags& QwtInterval::ExcludeMinimum ) ? "]" : "[" )
396 << interval.minValue() << "," << interval.maxValue()
397 << ( ( flags& QwtInterval::ExcludeMaximum ) ? "[" : "]" )
398 << ")";
399
400 return debug.space();
401}
402
403#endif
A class representing an interval.
double minValue() const
QwtInterval normalized() const
Normalize the limits of the interval.
QwtInterval & operator&=(const QwtInterval &)
Intersect this interval with the given interval.
@ ExcludeMaximum
Max value is not included in the interval.
@ ExcludeMinimum
Min value is not included in the interval.
@ IncludeBorders
Min/Max values are inside the interval.
void setMaxValue(double)
double maxValue() const
QwtInterval unite(const QwtInterval &) const
Unite 2 intervals.
bool intersects(const QwtInterval &) const
Test if two intervals overlap.
QwtInterval limited(double lowerBound, double upperBound) const
QwtInterval & operator|=(const QwtInterval &)
Unite this interval with the given interval.
QwtInterval extend(double value) const
Extend the interval.
void setMinValue(double)
QwtInterval inverted() const
QwtInterval intersect(const QwtInterval &) const
Intersect 2 intervals.
BorderFlags borderFlags() const
QwtInterval()
Default Constructor.
void setBorderFlags(BorderFlags)
bool isValid() const
bool contains(double value) const
QFlags< BorderFlag > BorderFlags
Border flags.
QwtInterval symmetrize(double value) const