qwt_double_interval.cpp 4.51 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
5
 *
pixhawk's avatar
pixhawk committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#include <qglobal.h>
#if QT_VERSION >= 0x040000
#include <qalgorithms.h>
#else
#include <qtl.h>
#endif

#include "qwt_math.h"
#include "qwt_double_interval.h"

/*!
   \brief Normalize the limits of the interval

   If maxValue() < minValue() the limits will be inverted.
   \return Normalized interval

   \sa isValid, inverted
*/
QwtDoubleInterval QwtDoubleInterval::normalized() const
{
30
    if ( !isValid() ) {
pixhawk's avatar
pixhawk committed
31
        return QwtDoubleInterval(d_maxValue, d_minValue);
32
    } else
pixhawk's avatar
pixhawk committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
        return *this;
}

/*!
   Invert the limits of the interval
   \return Inverted interval
   \sa normalized
*/
QwtDoubleInterval QwtDoubleInterval::inverted() const
{
    return QwtDoubleInterval(d_maxValue, d_minValue);
}

/*!
  Test if a value is inside an interval

  \param value Value
  \return true, if value >= minValue() && value <= maxValue()
*/
bool QwtDoubleInterval::contains(double value) const
{
    if ( !isValid() )
        return false;

    return (value >= d_minValue) && (value <= d_maxValue);
}

//! Unite 2 intervals
QwtDoubleInterval QwtDoubleInterval::unite(
    const QwtDoubleInterval &interval) const
{
64
    if ( !isValid() ) {
pixhawk's avatar
pixhawk committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
        if ( !interval.isValid() )
            return QwtDoubleInterval();
        else
            return interval;
    }
    if ( !interval.isValid() )
        return *this;

    const double minValue = qwtMin(d_minValue, interval.minValue());
    const double maxValue = qwtMax(d_maxValue, interval.maxValue());

    return QwtDoubleInterval(minValue, maxValue);
}

//! Intersect 2 intervals
QwtDoubleInterval QwtDoubleInterval::intersect(
    const QwtDoubleInterval &interval) const
{
    if ( !interval.isValid() || !isValid() )
        return QwtDoubleInterval();

    QwtDoubleInterval i1 = *this;
    QwtDoubleInterval i2 = interval;

    if ( i1.minValue() > i2.minValue() )
        qSwap(i1, i2);

    if ( i1.maxValue() < i2.minValue() )
        return QwtDoubleInterval();

95 96
    return QwtDoubleInterval(i2.minValue(),
                             qwtMin(i1.maxValue(), i2.maxValue()));
pixhawk's avatar
pixhawk committed
97 98 99 100 101 102 103 104 105 106
}

QwtDoubleInterval& QwtDoubleInterval::operator|=(
    const QwtDoubleInterval &interval)
{
    *this = *this | interval;
    return *this;
}

QwtDoubleInterval& QwtDoubleInterval::operator&=(
107
    const QwtDoubleInterval &interval)
pixhawk's avatar
pixhawk committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
{
    *this = *this & interval;
    return *this;
}

/*!
   Test if two intervals overlap
*/
bool QwtDoubleInterval::intersects(const QwtDoubleInterval &interval) const
{
    if ( !isValid() || !interval.isValid() )
        return false;

    QwtDoubleInterval i1 = *this;
    QwtDoubleInterval i2 = interval;

    if ( i1.minValue() > i2.minValue() )
        qSwap(i1, i2);

    return i1.maxValue() >= i2.minValue();
}

/*!
   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
*/
QwtDoubleInterval QwtDoubleInterval::symmetrize(double value) const
{
    if ( !isValid() )
        return *this;

    const double delta =
        qwtMax(qwtAbs(value - d_maxValue), qwtAbs(value - d_minValue));

    return QwtDoubleInterval(value - delta, value + delta);
}

/*!
   Limit the interval

   \param lBound Lower limit
   \param hBound Upper limit

   \return Limited interval
*/
QwtDoubleInterval QwtDoubleInterval::limited(
    double lBound, double hBound) const
{
    if ( !isValid() || lBound > hBound )
        return QwtDoubleInterval();

    double minValue = qwtMax(d_minValue, lBound);
    minValue = qwtMin(minValue, hBound);

    double maxValue = qwtMax(d_maxValue, lBound);
    maxValue = qwtMin(maxValue, hBound);

    return QwtDoubleInterval(minValue, maxValue);
}

/*!
   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
*/
QwtDoubleInterval QwtDoubleInterval::extend(double value) const
{
    if ( !isValid() )
        return *this;

    return QwtDoubleInterval(
188
               qwtMin(value, d_minValue), qwtMax(value, d_maxValue) );
pixhawk's avatar
pixhawk committed
189 190 191 192 193 194 195
}

QwtDoubleInterval& QwtDoubleInterval::operator|=(double value)
{
    *this = *this | value;
    return *this;
}