qwt_interval.cpp
Go to the documentation of this file.
00001 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
00002  * Qwt Widget Library
00003  * Copyright (C) 1997   Josef Wilgen
00004  * Copyright (C) 2002   Uwe Rathmann
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the Qwt License, Version 1.0
00008  *****************************************************************************/
00009 
00010 #include "qwt_interval.h"
00011 #include "qwt_math.h"
00012 #include <qalgorithms.h>
00013 
00022 QwtInterval QwtInterval::normalized() const
00023 {
00024     if ( d_minValue > d_maxValue )
00025     {
00026         return inverted();
00027     }
00028     if ( d_minValue == d_maxValue && d_borderFlags == ExcludeMinimum )
00029     {
00030         return inverted();
00031     }
00032 
00033     return *this;
00034 }
00035 
00041 QwtInterval QwtInterval::inverted() const
00042 {
00043     BorderFlags borderFlags = IncludeBorders;
00044     if ( d_borderFlags & ExcludeMinimum )
00045         borderFlags |= ExcludeMaximum;
00046     if ( d_borderFlags & ExcludeMaximum )
00047         borderFlags |= ExcludeMinimum;
00048 
00049     return QwtInterval( d_maxValue, d_minValue, borderFlags );
00050 }
00051 
00058 bool QwtInterval::contains( double value ) const
00059 {
00060     if ( !isValid() )
00061         return false;
00062 
00063     if ( value < d_minValue || value > d_maxValue )
00064         return false;
00065 
00066     if ( value == d_minValue && d_borderFlags & ExcludeMinimum )
00067         return false;
00068 
00069     if ( value == d_maxValue && d_borderFlags & ExcludeMaximum )
00070         return false;
00071 
00072     return true;
00073 }
00074 
00076 QwtInterval QwtInterval::unite( const QwtInterval &other ) const
00077 {
00078     /*
00079      If one of the intervals is invalid return the other one.
00080      If both are invalid return an invalid default interval
00081      */
00082     if ( !isValid() )
00083     {
00084         if ( !other.isValid() )
00085             return QwtInterval();
00086         else
00087             return other;
00088     }
00089     if ( !other.isValid() )
00090         return *this;
00091 
00092     QwtInterval united;
00093     BorderFlags flags = IncludeBorders;
00094 
00095     // minimum
00096     if ( d_minValue < other.minValue() )
00097     {
00098         united.setMinValue( d_minValue );
00099         flags &= d_borderFlags & ExcludeMinimum;
00100     }
00101     else if ( other.minValue() < d_minValue )
00102     {
00103         united.setMinValue( other.minValue() );
00104         flags &= other.borderFlags() & ExcludeMinimum;
00105     }
00106     else // d_minValue == other.minValue()
00107     {
00108         united.setMinValue( d_minValue );
00109         flags &= ( d_borderFlags & other.borderFlags() ) & ExcludeMinimum;
00110     }
00111 
00112     // maximum
00113     if ( d_maxValue > other.maxValue() )
00114     {
00115         united.setMaxValue( d_maxValue );
00116         flags &= d_borderFlags & ExcludeMaximum;
00117     }
00118     else if ( other.maxValue() > d_maxValue )
00119     {
00120         united.setMaxValue( other.maxValue() );
00121         flags &= other.borderFlags() & ExcludeMaximum;
00122     }
00123     else // d_maxValue == other.maxValue() )
00124     {
00125         united.setMaxValue( d_maxValue );
00126         flags &= d_borderFlags & other.borderFlags() & ExcludeMaximum;
00127     }
00128 
00129     united.setBorderFlags( flags );
00130     return united;
00131 }
00132 
00139 QwtInterval QwtInterval::intersect( const QwtInterval &other ) const
00140 {
00141     if ( !other.isValid() || !isValid() )
00142         return QwtInterval();
00143 
00144     QwtInterval i1 = *this;
00145     QwtInterval i2 = other;
00146 
00147     // swap i1/i2, so that the minimum of i1
00148     // is smaller then the minimum of i2
00149 
00150     if ( i1.minValue() > i2.minValue() )
00151     {
00152         qSwap( i1, i2 );
00153     }
00154     else if ( i1.minValue() == i2.minValue() )
00155     {
00156         if ( i1.borderFlags() & ExcludeMinimum )
00157             qSwap( i1, i2 );
00158     }
00159 
00160     if ( i1.maxValue() < i2.minValue() )
00161     {
00162         return QwtInterval();
00163     }
00164 
00165     if ( i1.maxValue() == i2.minValue() )
00166     {
00167         if ( i1.borderFlags() & ExcludeMaximum ||
00168             i2.borderFlags() & ExcludeMinimum )
00169         {
00170             return QwtInterval();
00171         }
00172     }
00173 
00174     QwtInterval intersected;
00175     BorderFlags flags = IncludeBorders;
00176 
00177     intersected.setMinValue( i2.minValue() );
00178     flags |= i2.borderFlags() & ExcludeMinimum;
00179 
00180     if ( i1.maxValue() < i2.maxValue() )
00181     {
00182         intersected.setMaxValue( i1.maxValue() );
00183         flags |= i1.borderFlags() & ExcludeMaximum;
00184     }
00185     else if ( i2.maxValue() < i1.maxValue() )
00186     {
00187         intersected.setMaxValue( i2.maxValue() );
00188         flags |= i2.borderFlags() & ExcludeMaximum;
00189     }
00190     else // i1.maxValue() == i2.maxValue()
00191     {
00192         intersected.setMaxValue( i1.maxValue() );
00193         flags |= i1.borderFlags() & i2.borderFlags() & ExcludeMaximum;
00194     }
00195 
00196     intersected.setBorderFlags( flags );
00197     return intersected;
00198 }
00199 
00206 QwtInterval& QwtInterval::operator|=( const QwtInterval &other )
00207 {
00208     *this = *this | other;
00209     return *this;
00210 }
00211 
00218 QwtInterval& QwtInterval::operator&=( const QwtInterval &other )
00219 {
00220     *this = *this & other;
00221     return *this;
00222 }
00223 
00230 bool QwtInterval::intersects( const QwtInterval &other ) const
00231 {
00232     if ( !isValid() || !other.isValid() )
00233         return false;
00234 
00235     QwtInterval i1 = *this;
00236     QwtInterval i2 = other;
00237 
00238     // swap i1/i2, so that the minimum of i1
00239     // is smaller then the minimum of i2
00240 
00241     if ( i1.minValue() > i2.minValue() )
00242     {
00243         qSwap( i1, i2 );
00244     }
00245     else if ( i1.minValue() == i2.minValue() &&
00246               i1.borderFlags() & ExcludeMinimum )
00247     {
00248         qSwap( i1, i2 );
00249     }
00250 
00251     if ( i1.maxValue() > i2.minValue() )
00252     {
00253         return true;
00254     }
00255     if ( i1.maxValue() == i2.minValue() )
00256     {
00257         return !( ( i1.borderFlags() & ExcludeMaximum ) ||
00258             ( i2.borderFlags() & ExcludeMinimum ) );
00259     }
00260     return false;
00261 }
00262 
00270 QwtInterval QwtInterval::symmetrize( double value ) const
00271 {
00272     if ( !isValid() )
00273         return *this;
00274 
00275     const double delta =
00276         qMax( qAbs( value - d_maxValue ), qAbs( value - d_minValue ) );
00277 
00278     return QwtInterval( value - delta, value + delta );
00279 }
00280 
00289 QwtInterval QwtInterval::limited( double lowerBound, double upperBound ) const
00290 {
00291     if ( !isValid() || lowerBound > upperBound )
00292         return QwtInterval();
00293 
00294     double minValue = qMax( d_minValue, lowerBound );
00295     minValue = qMin( minValue, upperBound );
00296 
00297     double maxValue = qMax( d_maxValue, lowerBound );
00298     maxValue = qMin( maxValue, upperBound );
00299 
00300     return QwtInterval( minValue, maxValue, d_borderFlags );
00301 }
00302 
00316 QwtInterval QwtInterval::extend( double value ) const
00317 {
00318     if ( !isValid() )
00319         return *this;
00320 
00321     return QwtInterval( qMin( value, d_minValue ),
00322         qMax( value, d_maxValue ), d_borderFlags );
00323 }
00324 
00333 QwtInterval& QwtInterval::operator|=( double value )
00334 {
00335     *this = *this | value;
00336     return *this;
00337 }
00338 
00339 #ifndef QT_NO_DEBUG_STREAM
00340 
00341 QDebug operator<<( QDebug debug, const QwtInterval &interval )
00342 {
00343     const int flags = interval.borderFlags();
00344 
00345     debug.nospace() << "QwtInterval("
00346         << ( ( flags & QwtInterval::ExcludeMinimum ) ? "]" : "[" )
00347         << interval.minValue() << "," << interval.maxValue()
00348         << ( ( flags & QwtInterval::ExcludeMaximum ) ? "[" : "]" )
00349         << ")";
00350 
00351     return debug.space();
00352 }
00353 
00354 #endif


plotjuggler
Author(s): Davide Faconti
autogenerated on Fri Sep 1 2017 02:41:56