-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.cpp
More file actions
143 lines (127 loc) · 4.05 KB
/
plot.cpp
File metadata and controls
143 lines (127 loc) · 4.05 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
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
#include "plot.h"
// Plot prints just once the function
void Graph::Plot(std::string infix) {
Operation *op = mathParser.toPostfix(infix);
fillValues(op);
plot();
clearMemory();
}
// Loop constantly prints a function that changes over time. A function
// with at least variable name t is required.
void Graph::Loop(std::string infix) {
Operation *op = mathParser.toPostfix(infix);
double t = 0;
for(;;) {
fillValues(op, t);
plot();
t += 0.01;
const int t100 = t * 100;
sleep(100);
std::cout << "t = " << t << (t100 % 10 == 0 ? " " : "") << " s " << std::endl;
cls(3);
}
clearMemory();
}
Graph::Graph(int si, double st) {
size = si;
step = st;
values = new int[size+size+1];
}
void Graph::cls(int offset) {
for(int i = 0; i < size*2 + offset; i++) std::cout << "\x1b[A";
}
void Graph::clearMemory() {
delete[] values;
}
// ----------------- PRIVATE -----------------
double Graph::round(double number) {
return number < 0.0 ? ceil(number - 0.5) : floor(number + 0.5);
}
void Graph::sleep(int milliseconds) {
clock_t time_end;
time_end = clock() + milliseconds * CLOCKS_PER_SEC/1000;
while (clock() < time_end) {}
}
void Graph::fillValues(Operation *op) {
for(int i = 0; i < size+size+1; ++i) {
// Give each variable a value
for(int j = 0; j < op->v_values.size(); j++) {
op->v_values[j] = (i - (size)) * step;
}
// EQUATION
std::string postfix_replaced = replaceVars(op);
double value = mathParser.Solve(postfix_replaced);
/// EQUATION
value = value/step;
values[i] = int(round(value));
}
}
void Graph::fillValues(Operation *op, double t) {
for(int i = 0; i < size+size+1; ++i) {
// Give each variable a value
for(int j = 0; j < op->v_values.size(); j++) {
if(op->postfix[op->v_pos[j]] == 't') op->v_values[j] = t;
else op->v_values[j] = (i - (size)) * step;
}
// EQUATION
std::string postfix_replaced = replaceVars(op);
double value = mathParser.Solve(postfix_replaced);
/// EQUATION
value = value/step;
values[i] = int(round(value));
}
}
void Graph::plot() {
std::cout << '\n';
for(int i = 0; i < size; ++i) {
std::cout << ' ';
for(int b = 0; b < size;++b) {
if(size - i == values[b]) std::cout << line;
else std::cout << graph;
}
if(size -i == values[size]) std::cout << line;
else std::cout << axis;
for(int b = 0; b < size;++b) {
if(size - i == values[b+size+1]) std::cout << line;
else std::cout << graph;
}
std::cout << std::endl;
}
std::cout << ' ';
for(int i = 0; i < size*2+1;++i) {
if(!values[i]) std::cout << line;
else std::cout << axis;
}
std::cout << std::endl;
for(int i = 0; i < size;++i) {
std::cout << ' ';
for(int b = 0; b < size;++b) {
if(0-i-1 == values[b]) std::cout << line;
else std::cout << graph;
}
if(0 -i-1 == values[size]) std::cout << line;
else std::cout << axis;
for(int b = 0; b < size;++b) {
if(0-i-1 == values[b+size+1]) std::cout << line;
else std::cout << graph;
}
std::cout << std::endl;
}
}
std::string Graph::replaceVars(const Operation *op) {
std::ostringstream output;
int offset = 0;
for(int p = 0; p < op->v_pos.size(); p++) {
// Add rest of postfix expression from last replaced var
for(int i = offset; i < op->v_pos[p]; i++) {
output << op->postfix[i];
}
output << op->v_values[p];
offset = op->v_pos[p]+1;
}
// Add the rest of the string if not added
for(int p = offset; p < op->postfix.length(); p++) {
output << op->postfix[p];
}
return output.str();
}