-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExpressionTree.cpp
87 lines (73 loc) · 1.72 KB
/
ExpressionTree.cpp
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
#include "ExpressionTree.h"
ExpressionTree::ExpressionTree()
{
root = nullptr;
}
ExpressionTree::ExpressionTree(string expr)
{
create(expr);
}
ExpressionTree::~ExpressionTree()
{
destroy();
}
bool ExpressionTree::create(string expr){
this->expr = expr;
destroy();
return recursivelyGen(root, expr);
}
void ExpressionTree::destroy(){
if(root != nullptr)
delete root;
}
double ExpressionTree::eval(Vec2d v){
if (root != nullptr)
return root->eval(v);
else
return 0;
}
bool ExpressionTree::recursivelyGen(ExpRoot &root, string expr){
Parser parser;
cout << "Parsing " << expr << endl;
ParseResult* res = parser.parse(expr);
cout << "Parsed" << endl;
if (res == nullptr)
return false;
if (res->id < tokens.size()){
if (res->id == VARX || res->id == VARY){
root = new VariableNode(res->id);
return true;
}
root = new OperatorNode(res->id);
return recursivelyGen(root->left, res->lExpr) & recursivelyGen(root->right, res->rExpr);
}
else {
double d = strtod(expr.c_str(), nullptr);
root = new ConstantNode(d);
return true;
}
return false;
}
PlotData* ExpressionTree::evalRange(Range xrange, Range yrange, float step)
{
PlotData* data = new PlotData();
data->xrange = xrange;
data->yrange = yrange;
data->zrange.low = 0; data->zrange.high = 0;
data->step = step;
data->cols = data->rows = 0;
for (float x = xrange.low; x <= xrange.high; x += step){
for (float y = yrange.low; y <= yrange.high; y += step){
float z = (float) this->eval({ x, y });
data->points.push_back(Vector3f(x, y, z));
data->cols++;
if (z < data->zrange.low)
data->zrange.low = z;
else if (z > data->zrange.high)
data->zrange.high = z;
}
data->rows++;
}
data->cols /= data->rows;
return data;
}