123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
- * Qwt Widget Library
- * Copyright (C) 1997 Josef Wilgen
- * Copyright (C) 2002 Uwe Rathmann
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the Qwt License, Version 1.0
- *****************************************************************************/
- #include "qwt_interval.h"
- #include "qwt_math.h"
- #include <qalgorithms.h>
- /*!
- \brief Normalize the limits of the interval
- If maxValue() < minValue() the limits will be inverted.
- \return Normalized interval
- \sa isValid(), inverted()
- */
- QwtInterval QwtInterval::normalized() const
- {
- if ( d_minValue > d_maxValue )
- {
- return inverted();
- }
- if ( d_minValue == d_maxValue && d_borderFlags == ExcludeMinimum )
- {
- return inverted();
- }
- return *this;
- }
- /*!
- Invert the limits of the interval
- \return Inverted interval
- \sa normalized()
- */
- QwtInterval QwtInterval::inverted() const
- {
- int borderFlags = 0;
- if ( d_borderFlags & ExcludeMinimum )
- borderFlags |= ExcludeMaximum;
- if ( d_borderFlags & ExcludeMaximum )
- borderFlags |= ExcludeMinimum;
- return QwtInterval( d_maxValue, d_minValue, borderFlags );
- }
- /*!
- Test if a value is inside an interval
- \param value Value
- \return true, if value >= minValue() && value <= maxValue()
- */
- bool QwtInterval::contains( double value ) const
- {
- if ( !isValid() )
- return false;
- if ( value < d_minValue || value > d_maxValue )
- return false;
- if ( value == d_minValue && d_borderFlags & ExcludeMinimum )
- return false;
- if ( value == d_maxValue && d_borderFlags & ExcludeMaximum )
- return false;
- return true;
- }
- //! Unite 2 intervals
- QwtInterval QwtInterval::unite( const QwtInterval &other ) const
- {
- /*
- If one of the intervals is invalid return the other one.
- If both are invalid return an invalid default interval
- */
- if ( !isValid() )
- {
- if ( !other.isValid() )
- return QwtInterval();
- else
- return other;
- }
- if ( !other.isValid() )
- return *this;
- QwtInterval united;
- int flags = 0;
- // minimum
- if ( d_minValue < other.minValue() )
- {
- united.setMinValue( d_minValue );
- flags &= d_borderFlags & ExcludeMinimum;
- }
- else if ( other.minValue() < d_minValue )
- {
- united.setMinValue( other.minValue() );
- flags &= other.borderFlags() & ExcludeMinimum;
- }
- else // d_minValue == other.minValue()
- {
- united.setMinValue( d_minValue );
- flags &= ( d_borderFlags & other.borderFlags() ) & ExcludeMinimum;
- }
- // maximum
- if ( d_maxValue > other.maxValue() )
- {
- united.setMaxValue( d_maxValue );
- flags &= d_borderFlags & ExcludeMaximum;
- }
- else if ( other.maxValue() > d_maxValue )
- {
- united.setMaxValue( other.maxValue() );
- flags &= other.borderFlags() & ExcludeMaximum;
- }
- else // d_maxValue == other.maxValue() )
- {
- united.setMaxValue( d_maxValue );
- flags &= d_borderFlags & other.borderFlags() & ExcludeMaximum;
- }
- united.setBorderFlags( flags );
- return united;
- }
- //! Intersect 2 intervals
- QwtInterval QwtInterval::intersect( const QwtInterval &other ) const
- {
- if ( !other.isValid() || !isValid() )
- return QwtInterval();
- QwtInterval i1 = *this;
- QwtInterval i2 = other;
- // swap i1/i2, so that the minimum of i1
- // is smaller then the minimum of i2
- if ( i1.minValue() > i2.minValue() )
- {
- qSwap( i1, i2 );
- }
- else if ( i1.minValue() == i2.minValue() )
- {
- if ( i1.borderFlags() & ExcludeMinimum )
- qSwap( i1, i2 );
- }
- if ( i1.maxValue() < i2.minValue() )
- {
- return QwtInterval();
- }
- if ( i1.maxValue() == i2.minValue() )
- {
- if ( i1.borderFlags() & ExcludeMaximum ||
- i2.borderFlags() & ExcludeMinimum )
- {
- return QwtInterval();
- }
- }
- QwtInterval intersected;
- int flags = 0;
- intersected.setMinValue( i2.minValue() );
- flags |= i2.borderFlags() & ExcludeMinimum;
- if ( i1.maxValue() < i2.maxValue() )
- {
- intersected.setMaxValue( i1.maxValue() );
- flags |= i1.borderFlags() & ExcludeMaximum;
- }
- else if ( i2.maxValue() < i1.maxValue() )
- {
- intersected.setMaxValue( i2.maxValue() );
- flags |= i2.borderFlags() & ExcludeMaximum;
- }
- else // i1.maxValue() == i2.maxValue()
- {
- intersected.setMaxValue( i1.maxValue() );
- flags |= i1.borderFlags() & i2.borderFlags() & ExcludeMaximum;
- }
- intersected.setBorderFlags( flags );
- return intersected;
- }
- //! Unites this interval with the given interval.
- QwtInterval& QwtInterval::operator|=( const QwtInterval & interval )
- {
- *this = *this | interval;
- return *this;
- }
- //! Intersects this interval with the given interval.
- QwtInterval& QwtInterval::operator&=( const QwtInterval & interval )
- {
- *this = *this & interval;
- return *this;
- }
- /*!
- Test if two intervals overlap
- */
- bool QwtInterval::intersects( const QwtInterval &other ) const
- {
- if ( !isValid() || !other.isValid() )
- return false;
- QwtInterval i1 = *this;
- QwtInterval i2 = other;
- // swap i1/i2, so that the minimum of i1
- // is smaller then the minimum of i2
- if ( i1.minValue() > i2.minValue() )
- {
- qSwap( i1, i2 );
- }
- else if ( i1.minValue() == i2.minValue() &&
- i1.borderFlags() & ExcludeMinimum )
- {
- qSwap( i1, i2 );
- }
- if ( i1.maxValue() > i2.minValue() )
- {
- return true;
- }
- if ( i1.maxValue() == i2.minValue() )
- {
- return !( ( i1.borderFlags() & ExcludeMaximum ) ||
- ( i2.borderFlags() & ExcludeMinimum ) );
- }
- return false;
- }
- /*!
- Adjust the limit that is closer to value, so that value becomes
- the center of the interval.
- \param value Center
- \return Interval with value as center
- */
- QwtInterval QwtInterval::symmetrize( double value ) const
- {
- if ( !isValid() )
- return *this;
- const double delta =
- qMax( qAbs( value - d_maxValue ), qAbs( value - d_minValue ) );
- return QwtInterval( value - delta, value + delta );
- }
- /*!
- Limit the interval, keeping the border modes
- \param lowerBound Lower limit
- \param upperBound Upper limit
- \return Limited interval
- */
- QwtInterval QwtInterval::limited( double lowerBound, double upperBound ) const
- {
- if ( !isValid() || lowerBound > upperBound )
- return QwtInterval();
- double minValue = qMax( d_minValue, lowerBound );
- minValue = qMin( minValue, upperBound );
- double maxValue = qMax( d_maxValue, lowerBound );
- maxValue = qMin( maxValue, upperBound );
- return QwtInterval( minValue, maxValue, d_borderFlags );
- }
- /*!
- Extend the interval
- If value is below minValue, value becomes the lower limit.
- If value is above maxValue, value becomes the upper limit.
- extend has no effect for invalid intervals
- \param value Value
- \sa isValid()
- */
- QwtInterval QwtInterval::extend( double value ) const
- {
- if ( !isValid() )
- return *this;
- return QwtInterval( qMin( value, d_minValue ),
- qMax( value, d_maxValue ), d_borderFlags );
- }
- /*!
- Extend an interval
- \param value Value
- \return Reference of the extended interval
- \sa extend()
- */
- QwtInterval& QwtInterval::operator|=( double value )
- {
- *this = *this | value;
- return *this;
- }
- #ifndef QT_NO_DEBUG_STREAM
- QDebug operator<<( QDebug debug, const QwtInterval &interval )
- {
- const int flags = interval.borderFlags();
- debug.nospace() << "QwtInterval("
- << ( ( flags & QwtInterval::ExcludeMinimum ) ? "]" : "[" )
- << interval.minValue() << "," << interval.maxValue()
- << ( ( flags & QwtInterval::ExcludeMaximum ) ? "[" : "]" )
- << ")";
- return debug.space();
- }
- #endif
|