Logo Search packages:      
Sourcecode: darkroom version File versions

double KCurve::getCurveValue ( const QList< QPointF > &  curve,
double  x 
) [static]

Returns:
the y value on the specified curve corresponding to the specified x value.

Definition at line 391 of file kcurve.cc.

Referenced by getCurveValue().

{
    double t;
    QPointF p;
    QPointF p0,p1,p2,p3;
    double c0,c1,c2,c3;
    double val;

    if(curve.count() == 0)
        return 0.5;

    // First find curve segment
    p = curve.first();
    if(x < p.x())
        return p.y();

    p = curve.last();
    if(x >= p.x())
        return p.y();

    // Find the four control points (two on each side of x)
    int i = 0;
    while(x >= curve.at(i).x())
    {
        ++i;
    }
    --i;

    if (i - 1 < 0)
    {
        p1 = p0 = curve.first();
    }
    else
    {
        p0 = curve.at(i - 1);
        p1 = curve.at(i);
    }

    if (i + 2 > curve.count() - 1)
    {
        p2 = p3 = curve.last();
    }
    else
    {
        p2 = curve.at(i + 1);
        p3 = curve.at(i + 2);
    }

    // Calculate the value
    t = (x - p1.x()) / (p2.x() - p1.x());
    c2 = (p2.y() - p0.y()) * (p2.x()-p1.x()) / (p2.x()-p0.x());
    c3 = p1.y();
    c0 = -2*p2.y() + 2*c3 + c2 + (p3.y() - p1.y()) * (p2.x() - p1.x()) / (p3.x() - p1.x());
    c1 = p2.y() - c3 - c2 - c0;
    val = ((c0*t + c1)*t + c2)*t + c3;

    if(val < 0.0)
        val = 0.0;
    if(val > 1.0)
        val = 1.0;
    return val;
}


Generated by  Doxygen 1.6.0   Back to index