-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.cpp
More file actions
108 lines (90 loc) · 1.98 KB
/
Copy pathcalculator.cpp
File metadata and controls
108 lines (90 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
64
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "calculator.h"
Calculator::Calculator() {
t = new parser::Tree(ITR_VAR);
}
Calculator::Calculator(string f) {
t = new parser::Tree(f);
}
Calculator::~Calculator() {
delete t;
}
void Calculator::setFct(string f) {
delete t;
t = new parser::Tree(f);
}
void Calculator::setVar(string var, string a) {
t->setVar(var, a);
}
void Calculator::setVar(string var, QPointF p) {
t->setVar(var, qpfToCx(p));
}
QPointF Calculator::evalVar(string var) {
return cxToQpf( t->evalVar(var) );
}
QPointF Calculator::eval(QPointF seed) {
t->setVar(ITR_VAR, qpfToCx(seed));
return cxToQpf( t->eval() );
}
string Calculator::toString() {
return t->toString();
}
void Calculator::applyPreset(int preset) {
// delete t;
// IMPLEMENTATION NEEDED
switch(preset) {
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
}
}
string Calculator::getFct() {
return t->getFct();
}
string Calculator::getVarFct(string var) {
return t->getVarFct(var);
}
//Utils
QPointF Calculator::cxToQpf(cx a) {
return QPointF(a.real(), a.imag());
}
QPointF Calculator::cxToQpf(cx *a) {
return QPointF(a->real(), a->imag());
}
cx Calculator::qpfToCx(QPointF p) {
return cx(p.x(), p.y());
}
cx Calculator::qpfToCx(QPointF *p) {
return cx(p->x(), p->y());
}
int Calculator::iterate(QPointF seed, std::vector<QPointF> *output) {
if(output->size() != 0) {
output->clear();
}
cx result;
double xdifference;
double ydifference;
t->setVar(ITR_VAR, qpfToCx(seed));
for(int i = 0; i < 10000; ++i) {
try {
result = t->eval();
output->push_back(cxToQpf(result));
if(i >= 1) {
xdifference = (*output)[i].x() - (*output)[i-1].x();
ydifference = (*output)[i].y() - (*output)[i-1].y();
if( (xdifference > 0 ? xdifference : -xdifference) < ITERATION_THRESHHOLD &&
(ydifference > 0 ? ydifference : -ydifference) < ITERATION_THRESHHOLD) {
return i;
}
}
t->setVar(ITR_VAR , result);
} catch(const std::invalid_argument& e) {
return i;
}
}
return 10000;
}