-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphcontroller.cpp
More file actions
114 lines (100 loc) · 3.06 KB
/
Copy pathgraphcontroller.cpp
File metadata and controls
114 lines (100 loc) · 3.06 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
#include "graphcontroller.h"
QPointF GraphController::calcToGraph(QPointF p) {
double xfac = mpixprops.width / mgraphprops.xr;
double yfac = mpixprops.height / mgraphprops.yr;
double x = xfac * (p.x() - mgraphprops.mtopleft.x());
double y = yfac * (-p.y() + mgraphprops.mtopleft.y());
return QPointF(x, y);
}
QPointF GraphController::graphToCalc(QPointF p) {
double xfac = mgraphprops.xr / mpixprops.width;
double yfac = mgraphprops.yr / mpixprops.height;
double x = xfac * p.x() + mgraphprops.mtopleft.x();
double y = yfac * p.y() - mgraphprops.mtopleft.y();
return QPointF(x, -y);
}
GraphController::GraphController(int numGraphs, Calculator *c, int width, int height, QWidget *parent) :
QWidget(parent) {
mgraphprops = {
QPointF(-2, 2),
4, 1,
4, 1
};
mpixprops = {
width,
height
};
mcalc = c;
mmode = 1;
mfct = mcalc->getFct();
mgraphs = new Graph* [numGraphs];
msize = numGraphs;
QHBoxLayout *layout = new QHBoxLayout();
for(int i = msize - 1; i >= 0; --i) {
mgraphs[i] = new Graph(width, height, this);
Graph *g = mgraphs[i];
layout->addWidget(g);
QObject::connect(g, SIGNAL(s_updatePartnerDot(QPointF)), this, SLOT(s_updateDot(QPointF)));
QObject::connect(g, SIGNAL(s_updatePartnerCursor(QPointF, Graph *)), this, SLOT(s_updateCursor(QPointF, Graph *)));
}
this->setLayout(layout);
}
GraphController::~GraphController() {
for(int i = msize - 1; i >= 0; --i) {
delete (mgraphs + i);
}
delete [] mgraphs;
delete mcalc;
}
void GraphController::setFct(string f) {
mcalc->setFct(f);
}
void GraphController::s_clear() {
for(int i = msize - 1; i >= 0; --i) {
mgraphs[i]->clear();
}
}
void GraphController::s_updateCursor(QPointF p, Graph *g) {
QPointF transformed = calcToGraph( mcalc->eval( graphToCalc(p) ));
for(int i = msize - 1; i >= 0; --i) {
if(g != mgraphs[i]) {
mgraphs[i]->drawCursor(transformed);
}
}
}
// As is stands, this implementation causes the green sursor on the input graph to jump to the dot drawn.
// I am calling it a feature for now (it directs attention to the drawn dot, making it easy to find) pending feedback
void GraphController::s_updateDot(QPointF p) {
// DEBUG
QPointF temp0 = p;
QPointF temp1 = graphToCalc(temp0);
QPointF temp2 = mcalc->eval(temp1);
QPointF temp3 = calcToGraph(temp2);
printf("(%f, %f) -> (%f, %f) -> (%f, %f) -> (%f, %f)\n",
temp0.x(), temp0.y(),
temp1.x(), temp1.y(),
temp2.x(), temp2.y(),
temp3.x(), temp3.y());
// END_DEBUG
if(mmode == 0) {
QPointF transformed = calcToGraph( mcalc->eval( graphToCalc(p) ));
for(int i = msize - 1; i >= 0; --i) {
mgraphs[i]->drawDot(transformed);
}
} else if(mmode == 1) {
mmode = 2;
std::vector<QPointF> *points = new std::vector<QPointF>();
int numPoints = mcalc->iterate(graphToCalc(p), points);
// DEBUG
printf("%d\n", numPoints);
for(int i = numPoints - 1; i >= 0; --i) {
(*points)[i] = calcToGraph( (*points)[i] );
}
for(int i = msize - 1; i >= 0 && mmode == 2; --i) {
mgraphs[i]->drawIterations(points, numPoints);
}
mmode = 1;
} else if(mmode == 2) {
mmode = 1;
}
}