~ubuntu-branches/ubuntu/trusty/kalgebra/trusty-updates

« back to all changes in this revision

Viewing changes to analitzagui/functionimpl.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Philip Muškovac
  • Date: 2011-07-08 20:10:34 UTC
  • Revision ID: james.westby@ubuntu.com-20110708201034-0cqpagx7uz4pu82n
Tags: upstream-4.6.90+repack1
Import upstream version 4.6.90+repack1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*************************************************************************************
 
2
 *  Copyright (C) 2007-2009 by Aleix Pol <aleixpol@kde.org>                          *
 
3
 *                                                                                   *
 
4
 *  This program is free software; you can redistribute it and/or                    *
 
5
 *  modify it under the terms of the GNU General Public License                      *
 
6
 *  as published by the Free Software Foundation; either version 2                   *
 
7
 *  of the License, or (at your option) any later version.                           *
 
8
 *                                                                                   *
 
9
 *  This program is distributed in the hope that it will be useful,                  *
 
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
 
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
 
12
 *  GNU General Public License for more details.                                     *
 
13
 *                                                                                   *
 
14
 *  You should have received a copy of the GNU General Public License                *
 
15
 *  along with this program; if not, write to the Free Software                      *
 
16
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
 
17
 *************************************************************************************/
 
18
 
 
19
#include "functionimpl.h"
 
20
#include "analitza/variables.h"
 
21
#include "analitza/expression.h"
 
22
#include "functionfactory.h"
 
23
#include <cmath>
 
24
 
 
25
#include <KDebug>
 
26
 
 
27
using namespace std;
 
28
using namespace Analitza;
 
29
 
 
30
FunctionImpl::FunctionImpl(const Expression& newFunc, Variables* v, double defDl, double defUl)
 
31
        : points(), func(v), m_deriv(0), m_res(0), mUplimit(defUl), mDownlimit(defDl)
 
32
{
 
33
        func.setExpression(newFunc);
 
34
        func.simplify();
 
35
        
 
36
        func.flushErrors();
 
37
}
 
38
 
 
39
FunctionImpl::FunctionImpl(const FunctionImpl& fi)
 
40
        : points(), func(fi.func.variables()), m_deriv(0), m_res(fi.m_res)
 
41
        , mUplimit(fi.mUplimit), mDownlimit(fi.mDownlimit)
 
42
{
 
43
//      Q_ASSERT(fi.isCorrect());
 
44
        func.setExpression(fi.func.expression());
 
45
        if(fi.m_deriv)
 
46
                m_deriv = new Expression(*fi.m_deriv);
 
47
}
 
48
 
 
49
FunctionImpl::~FunctionImpl()
 
50
{
 
51
        delete m_deriv;
 
52
}
 
53
 
 
54
bool FunctionImpl::isSimilar(double a, double b, double diff)
 
55
{
 
56
        return fabs(a-b)<diff;
 
57
}
 
58
 
 
59
bool FunctionImpl::addValue(const QPointF& p)
 
60
{
 
61
        int count=points.count();
 
62
        if(count<2) {
 
63
                points.append(p);
 
64
                return false;
 
65
        }
 
66
        
 
67
        double angle1=std::atan2(points[count-1].y()-points[count-2].y(), points[count-1].x()-points[count-2].x());
 
68
        double angle2=std::atan2(p.y()-points[count-1].y(), p.x()-points[count-1].x());
 
69
        
 
70
        bool append=!isSimilar(angle1, angle2);
 
71
        if(append)
 
72
                points.append(p);
 
73
        else
 
74
                points.last()=p;
 
75
        
 
76
        return append;
 
77
}
 
78
 
 
79
void FunctionImpl::setResolution(uint res)
 
80
{
 
81
        Q_ASSERT(res>2);
 
82
        
 
83
        if(res!=m_res) {
 
84
                points.clear();
 
85
                m_jumps.clear();
 
86
        }
 
87
        m_res=res;
 
88
}
 
89
 
 
90
double FunctionImpl::uplimit() const
 
91
{
 
92
        return mUplimit;
 
93
}
 
94
 
 
95
double FunctionImpl::downlimit() const
 
96
{
 
97
        return mDownlimit;
 
98
}
 
99
 
 
100
void FunctionImpl::setLimits(double d, double u)
 
101
{
 
102
        Q_ASSERT(u>=d);
 
103
        mUplimit=u;
 
104
        mDownlimit=d;
 
105
}