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 #include "qwt_math.h"
12 #include <qalgorithms.h>
13 
23 {
24  if ( d_minValue > d_maxValue )
25  {
26  return inverted();
27  }
29  {
30  return inverted();
31  }
32 
33  return *this;
34 }
35 
42 {
45  borderFlags |= ExcludeMaximum;
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 
77 {
78  /*
79  If one of the intervals is invalid return the other one.
80  If both are invalid return an invalid default interval
81  */
82  if ( !isValid() )
83  {
84  if ( !other.isValid() )
85  return QwtInterval();
86  else
87  return other;
88  }
89  if ( !other.isValid() )
90  return *this;
91 
92  QwtInterval united;
94 
95  // minimum
96  if ( d_minValue < other.minValue() )
97  {
98  united.setMinValue( d_minValue );
99  flags &= d_borderFlags & ExcludeMinimum;
100  }
101  else if ( other.minValue() < d_minValue )
102  {
103  united.setMinValue( other.minValue() );
104  flags &= other.borderFlags() & ExcludeMinimum;
105  }
106  else // d_minValue == other.minValue()
107  {
108  united.setMinValue( d_minValue );
109  flags &= ( d_borderFlags & other.borderFlags() ) & ExcludeMinimum;
110  }
111 
112  // maximum
113  if ( d_maxValue > other.maxValue() )
114  {
115  united.setMaxValue( d_maxValue );
116  flags &= d_borderFlags & ExcludeMaximum;
117  }
118  else if ( other.maxValue() > d_maxValue )
119  {
120  united.setMaxValue( other.maxValue() );
121  flags &= other.borderFlags() & ExcludeMaximum;
122  }
123  else // d_maxValue == other.maxValue() )
124  {
125  united.setMaxValue( d_maxValue );
126  flags &= d_borderFlags & other.borderFlags() & ExcludeMaximum;
127  }
128 
129  united.setBorderFlags( flags );
130  return united;
131 }
132 
140 {
141  if ( !other.isValid() || !isValid() )
142  return QwtInterval();
143 
144  QwtInterval i1 = *this;
145  QwtInterval i2 = other;
146 
147  // swap i1/i2, so that the minimum of i1
148  // is smaller then the minimum of i2
149 
150  if ( i1.minValue() > i2.minValue() )
151  {
152  qSwap( i1, i2 );
153  }
154  else if ( i1.minValue() == i2.minValue() )
155  {
156  if ( i1.borderFlags() & ExcludeMinimum )
157  qSwap( i1, i2 );
158  }
159 
160  if ( i1.maxValue() < i2.minValue() )
161  {
162  return QwtInterval();
163  }
164 
165  if ( i1.maxValue() == i2.minValue() )
166  {
167  if ( i1.borderFlags() & ExcludeMaximum ||
168  i2.borderFlags() & ExcludeMinimum )
169  {
170  return QwtInterval();
171  }
172  }
173 
174  QwtInterval intersected;
176 
177  intersected.setMinValue( i2.minValue() );
178  flags |= i2.borderFlags() & ExcludeMinimum;
179 
180  if ( i1.maxValue() < i2.maxValue() )
181  {
182  intersected.setMaxValue( i1.maxValue() );
183  flags |= i1.borderFlags() & ExcludeMaximum;
184  }
185  else if ( i2.maxValue() < i1.maxValue() )
186  {
187  intersected.setMaxValue( i2.maxValue() );
188  flags |= i2.borderFlags() & ExcludeMaximum;
189  }
190  else // i1.maxValue() == i2.maxValue()
191  {
192  intersected.setMaxValue( i1.maxValue() );
193  flags |= i1.borderFlags() & i2.borderFlags() & ExcludeMaximum;
194  }
195 
196  intersected.setBorderFlags( flags );
197  return intersected;
198 }
199 
207 {
208  *this = *this | other;
209  return *this;
210 }
211 
219 {
220  *this = *this & other;
221  return *this;
222 }
223 
230 bool QwtInterval::intersects( const QwtInterval &other ) const
231 {
232  if ( !isValid() || !other.isValid() )
233  return false;
234 
235  QwtInterval i1 = *this;
236  QwtInterval i2 = other;
237 
238  // swap i1/i2, so that the minimum of i1
239  // is smaller then the minimum of i2
240 
241  if ( i1.minValue() > i2.minValue() )
242  {
243  qSwap( i1, i2 );
244  }
245  else if ( i1.minValue() == i2.minValue() &&
246  i1.borderFlags() & ExcludeMinimum )
247  {
248  qSwap( i1, i2 );
249  }
250 
251  if ( i1.maxValue() > i2.minValue() )
252  {
253  return true;
254  }
255  if ( i1.maxValue() == i2.minValue() )
256  {
257  return !( ( i1.borderFlags() & ExcludeMaximum ) ||
258  ( i2.borderFlags() & ExcludeMinimum ) );
259  }
260  return false;
261 }
262 
271 {
272  if ( !isValid() )
273  return *this;
274 
275  const double delta =
276  qMax( qAbs( value - d_maxValue ), qAbs( value - d_minValue ) );
277 
278  return QwtInterval( value - delta, value + delta );
279 }
280 
289 QwtInterval QwtInterval::limited( double lowerBound, double upperBound ) const
290 {
291  if ( !isValid() || lowerBound > upperBound )
292  return QwtInterval();
293 
294  double minValue = qMax( d_minValue, lowerBound );
295  minValue = qMin( minValue, upperBound );
296 
297  double maxValue = qMax( d_maxValue, lowerBound );
298  maxValue = qMin( maxValue, upperBound );
299 
300  return QwtInterval( minValue, maxValue, d_borderFlags );
301 }
302 
316 QwtInterval QwtInterval::extend( double value ) const
317 {
318  if ( !isValid() )
319  return *this;
320 
321  return QwtInterval( qMin( value, d_minValue ),
322  qMax( value, d_maxValue ), d_borderFlags );
323 }
324 
334 {
335  *this = *this | value;
336  return *this;
337 }
338 
339 #ifndef QT_NO_DEBUG_STREAM
340 
341 QDebug operator<<( QDebug debug, const QwtInterval &interval )
342 {
343  const int flags = interval.borderFlags();
344 
345  debug.nospace() << "QwtInterval("
346  << ( ( flags & QwtInterval::ExcludeMinimum ) ? "]" : "[" )
347  << interval.minValue() << "," << interval.maxValue()
348  << ( ( flags & QwtInterval::ExcludeMaximum ) ? "[" : "]" )
349  << ")";
350 
351  return debug.space();
352 }
353 
354 #endif
QwtInterval limited(double minValue, double maxValue) const
QwtInterval normalized() const
Normalize the limits of the interval.
QFlags< BorderFlag > BorderFlags
Border flags.
Definition: qwt_interval.h:49
bool contains(double value) const
A class representing an interval.
Definition: qwt_interval.h:26
Max value is not included in the interval.
Definition: qwt_interval.h:42
double minValue() const
Definition: qwt_interval.h:193
bool intersects(const QwtInterval &) const
Test if two intervals overlap.
double maxValue() const
Definition: qwt_interval.h:199
QwtInterval unite(const QwtInterval &) const
Unite 2 intervals.
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:211
QwtInterval & operator|=(const QwtInterval &)
Unite this interval with the given interval.
QwtInterval()
Default Constructor.
Definition: qwt_interval.h:113
Min value is not included in the interval.
Definition: qwt_interval.h:39
BorderFlags d_borderFlags
Definition: qwt_interval.h:102
void setBorderFlags(BorderFlags)
Definition: qwt_interval.h:158
BorderFlags borderFlags() const
Definition: qwt_interval.h:167
double d_maxValue
Definition: qwt_interval.h:101
void setMinValue(double)
Definition: qwt_interval.h:177
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:100
QwtInterval symmetrize(double value) const
void setMaxValue(double)
Definition: qwt_interval.h:187
int flags
Min/Max values are inside the interval.
Definition: qwt_interval.h:36


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:17