qwt_interval.cpp
Go to the documentation of this file.
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 "qwt_interval.h"
11 
21 {
22  if ( d_minValue > d_maxValue )
23  {
24  return inverted();
25  }
27  {
28  return inverted();
29  }
30 
31  return *this;
32 }
33 
40 {
42 
44  borderFlags |= ExcludeMaximum;
45 
47  borderFlags |= ExcludeMinimum;
48 
49  return QwtInterval( d_maxValue, d_minValue, borderFlags );
50 }
51 
58 bool QwtInterval::contains( double value ) const
59 {
60  if ( !isValid() )
61  return false;
62 
63  if ( ( value < d_minValue ) || ( value > d_maxValue ) )
64  return false;
65 
66  if ( ( value == d_minValue ) && ( d_borderFlags & ExcludeMinimum ) )
67  return false;
68 
69  if ( ( value == d_maxValue ) && ( d_borderFlags & ExcludeMaximum ) )
70  return false;
71 
72  return true;
73 }
74 
81 bool QwtInterval::contains( const QwtInterval& interval ) const
82 {
83  if ( !isValid() || !interval.isValid() )
84  return false;
85 
86  if ( ( interval.d_minValue < d_minValue ) || ( interval.d_maxValue > d_maxValue ) )
87  return false;
88 
89  if ( d_borderFlags )
90  {
91  if ( interval.d_minValue == d_minValue )
92  {
93  if ( ( d_borderFlags & ExcludeMinimum )
94  && !( interval.d_borderFlags & ExcludeMinimum ) )
95  {
96  return false;
97  }
98  }
99 
100  if ( interval.d_maxValue == d_maxValue )
101  {
102  if ( ( d_borderFlags & ExcludeMaximum )
103  && !( interval.d_borderFlags & ExcludeMaximum ) )
104  {
105  return false;
106  }
107  }
108  }
109 
110  return true;
111 }
112 
115 {
116  /*
117  If one of the intervals is invalid return the other one.
118  If both are invalid return an invalid default interval
119  */
120  if ( !isValid() )
121  {
122  if ( !other.isValid() )
123  return QwtInterval();
124  else
125  return other;
126  }
127  if ( !other.isValid() )
128  return *this;
129 
130  QwtInterval united;
131  BorderFlags flags = IncludeBorders;
132 
133  // minimum
134  if ( d_minValue < other.minValue() )
135  {
136  united.setMinValue( d_minValue );
137  flags &= d_borderFlags & ExcludeMinimum;
138  }
139  else if ( other.minValue() < d_minValue )
140  {
141  united.setMinValue( other.minValue() );
142  flags &= other.borderFlags() & ExcludeMinimum;
143  }
144  else // d_minValue == other.minValue()
145  {
146  united.setMinValue( d_minValue );
147  flags &= ( d_borderFlags & other.borderFlags() ) & ExcludeMinimum;
148  }
149 
150  // maximum
151  if ( d_maxValue > other.maxValue() )
152  {
153  united.setMaxValue( d_maxValue );
154  flags &= d_borderFlags & ExcludeMaximum;
155  }
156  else if ( other.maxValue() > d_maxValue )
157  {
158  united.setMaxValue( other.maxValue() );
159  flags &= other.borderFlags() & ExcludeMaximum;
160  }
161  else // d_maxValue == other.maxValue() )
162  {
163  united.setMaxValue( d_maxValue );
164  flags &= d_borderFlags & other.borderFlags() & ExcludeMaximum;
165  }
166 
167  united.setBorderFlags( flags );
168  return united;
169 }
170 
178 {
179  if ( !other.isValid() || !isValid() )
180  return QwtInterval();
181 
182  QwtInterval i1 = *this;
183  QwtInterval i2 = other;
184 
185  // swap i1/i2, so that the minimum of i1
186  // is smaller then the minimum of i2
187 
188  if ( i1.minValue() > i2.minValue() )
189  {
190  qSwap( i1, i2 );
191  }
192  else if ( i1.minValue() == i2.minValue() )
193  {
194  if ( i1.borderFlags() & ExcludeMinimum )
195  qSwap( i1, i2 );
196  }
197 
198  if ( i1.maxValue() < i2.minValue() )
199  {
200  return QwtInterval();
201  }
202 
203  if ( i1.maxValue() == i2.minValue() )
204  {
205  if ( i1.borderFlags() & ExcludeMaximum ||
206  i2.borderFlags() & ExcludeMinimum )
207  {
208  return QwtInterval();
209  }
210  }
211 
212  QwtInterval intersected;
213  BorderFlags flags = IncludeBorders;
214 
215  intersected.setMinValue( i2.minValue() );
216  flags |= i2.borderFlags() & ExcludeMinimum;
217 
218  if ( i1.maxValue() < i2.maxValue() )
219  {
220  intersected.setMaxValue( i1.maxValue() );
221  flags |= i1.borderFlags() & ExcludeMaximum;
222  }
223  else if ( i2.maxValue() < i1.maxValue() )
224  {
225  intersected.setMaxValue( i2.maxValue() );
226  flags |= i2.borderFlags() & ExcludeMaximum;
227  }
228  else // i1.maxValue() == i2.maxValue()
229  {
230  intersected.setMaxValue( i1.maxValue() );
231  flags |= i1.borderFlags() & i2.borderFlags() & ExcludeMaximum;
232  }
233 
234  intersected.setBorderFlags( flags );
235  return intersected;
236 }
237 
245 {
246  *this = *this | other;
247  return *this;
248 }
249 
257 {
258  *this = *this & other;
259  return *this;
260 }
261 
268 bool QwtInterval::intersects( const QwtInterval &other ) const
269 {
270  if ( !isValid() || !other.isValid() )
271  return false;
272 
273  QwtInterval i1 = *this;
274  QwtInterval i2 = other;
275 
276  // swap i1/i2, so that the minimum of i1
277  // is smaller then the minimum of i2
278 
279  if ( i1.minValue() > i2.minValue() )
280  {
281  qSwap( i1, i2 );
282  }
283  else if ( i1.minValue() == i2.minValue() &&
284  i1.borderFlags() & ExcludeMinimum )
285  {
286  qSwap( i1, i2 );
287  }
288 
289  if ( i1.maxValue() > i2.minValue() )
290  {
291  return true;
292  }
293  if ( i1.maxValue() == i2.minValue() )
294  {
295  return !( ( i1.borderFlags() & ExcludeMaximum ) ||
296  ( i2.borderFlags() & ExcludeMinimum ) );
297  }
298  return false;
299 }
300 
309 {
310  if ( !isValid() )
311  return *this;
312 
313  const double delta =
314  qMax( qAbs( value - d_maxValue ), qAbs( value - d_minValue ) );
315 
316  return QwtInterval( value - delta, value + delta );
317 }
318 
327 QwtInterval QwtInterval::limited( double lowerBound, double upperBound ) const
328 {
329  if ( !isValid() || lowerBound > upperBound )
330  return QwtInterval();
331 
332  double minValue = qMax( d_minValue, lowerBound );
333  minValue = qMin( minValue, upperBound );
334 
335  double maxValue = qMax( d_maxValue, lowerBound );
336  maxValue = qMin( maxValue, upperBound );
337 
338  return QwtInterval( minValue, maxValue, d_borderFlags );
339 }
340 
355 {
356  if ( !isValid() )
357  return *this;
358 
359  return QwtInterval( qMin( value, d_minValue ),
360  qMax( value, d_maxValue ), d_borderFlags );
361 }
362 
372 {
373  *this = *this | value;
374  return *this;
375 }
376 
377 #ifndef QT_NO_DEBUG_STREAM
378 
379 #include <qdebug.h>
380 
381 QDebug operator<<( QDebug debug, const QwtInterval &interval )
382 {
383  const int flags = interval.borderFlags();
384 
385  debug.nospace() << "QwtInterval("
386  << ( ( flags & QwtInterval::ExcludeMinimum ) ? "]" : "[" )
387  << interval.minValue() << "," << interval.maxValue()
388  << ( ( flags & QwtInterval::ExcludeMaximum ) ? "[" : "]" )
389  << ")";
390 
391  return debug.space();
392 }
393 
394 #endif
enum MQTTPropertyCodes value
QwtInterval normalized() const
Normalize the limits of the interval.
QFlags< BorderFlag > BorderFlags
Border flags.
Definition: qwt_interval.h:45
bool contains(double value) const
A class representing an interval.
Definition: qwt_interval.h:22
Max value is not included in the interval.
Definition: qwt_interval.h:38
double minValue() const
Definition: qwt_interval.h:190
bool intersects(const QwtInterval &) const
Test if two intervals overlap.
double maxValue() const
Definition: qwt_interval.h:196
QwtInterval unite(const QwtInterval &) const
Unite 2 intervals.
QwtInterval limited(double lowerBound, double upperBound) const
QDebug operator<<(QDebug debug, const QwtInterval &interval)
QwtInterval intersect(const QwtInterval &) const
Intersect 2 intervals.
QwtInterval inverted() const
bool isValid() const
Definition: qwt_interval.h:208
QwtInterval & operator|=(const QwtInterval &)
Unite this interval with the given interval.
QwtInterval()
Default Constructor.
Definition: qwt_interval.h:110
Min value is not included in the interval.
Definition: qwt_interval.h:35
BorderFlags d_borderFlags
Definition: qwt_interval.h:99
void setBorderFlags(BorderFlags)
Definition: qwt_interval.h:155
BorderFlags borderFlags() const
Definition: qwt_interval.h:164
double d_maxValue
Definition: qwt_interval.h:98
void setMinValue(double)
Definition: qwt_interval.h:174
QwtInterval & operator&=(const QwtInterval &)
Intersect this interval with the given interval.
QwtInterval extend(double value) const
Extend the interval.
double d_minValue
Definition: qwt_interval.h:97
QwtInterval symmetrize(double value) const
void setMaxValue(double)
Definition: qwt_interval.h:184
Min/Max values are inside the interval.
Definition: qwt_interval.h:32


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:10