/
graphView.cpp
116 lines (89 loc) · 2.44 KB
/
graphView.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
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
#include "graphView.h"
graphView::graphView(){
this->tree = NULL;
}
graphView::~graphView(){
}
void graphView::setTree(eqTreeView *eqTree)
{
this->tree = eqTree;
}
void graphView::redraw(){
Glib::RefPtr<Gdk::Window> win = this->get_window();
if(win)
{
Gdk::Rectangle r(0, 0, get_allocation().get_width(), get_allocation().get_height());
win->invalidate_rect(r, false);
}
}
bool graphView::on_draw(const Cairo::RefPtr<Cairo::Context>& cr){
//Get the allocation of our widget, and get the height and width from it
Gtk::Allocation wAllocation = this->get_allocation();
const int wHeight = wAllocation.get_height();
const int wWidth = wAllocation.get_width();
const double lineWidth = wHeight;
//scale the Cario context obj to the current wHeight and wWidth
//cr->scale(wWidth, wHeight);
//now change the offset point coordinates, so point (0,0) will be at (0.5,0.5)
cr->translate(wWidth/2, wHeight/2);
//set the line width to 2 px, this should be a settings for each graph in the futur
cr->set_line_width(1);
//paint the background white
cr->set_source_rgb(1,1,1);
cr->paint();
//draw the coordinatesystem
cr->set_source_rgb(0,0,0);
cr->move_to(0,0);
cr->line_to(wWidth,0);
cr->move_to(0,0);
cr->line_to(0,wHeight);
cr->move_to(0,0);
cr->line_to(-1*wWidth,0);
cr->move_to(0,0);
cr->line_to(0,-1*wHeight);
//draw the axes unit, again this should't be hardcoded in the futur
cr->move_to(0,0);
for(int i = 1; i <= 25; i++)
{
cr->move_to((wWidth*0.5/25)*i,0);
cr->rel_move_to(0, -8);
cr->rel_line_to(0,16);
cr->rel_move_to(0, -8);
cr->move_to((wWidth*0.5/25)*-i,0);
cr->rel_move_to(0, -8);
cr->rel_line_to(0,16);
cr->rel_move_to(0, -8);
}
cr->move_to(0, 0);
for(int i = 1; i <= 25; i++)
{
cr->move_to(0,(wHeight*0.5/25)*i);
cr->rel_move_to(-8,0);
cr->rel_line_to(16,0);
cr->rel_move_to(-8,0);
cr->move_to(0,(wHeight*0.5/25)*-i);
cr->rel_move_to(-8,0);
cr->rel_line_to(16,0);
cr->rel_move_to(-8,0);
}
cr->stroke();
cr->set_source_rgb(0.8,0,0);
if(this->tree)
{
cr->set_line_width(3);
std::vector<equation*> eqs = this->tree->getEquations();
double pxWidth = wWidth/50;
double pxHeight = wHeight/50;
for(int i = 0; i < eqs.size(); i++){
equation *eq = eqs[i];
for(double x = -25; x<= 25; x+= 0.01)
{
double y = eq->getYFromX(x);
cr->move_to(x*pxWidth,-1*(y*pxHeight));
cr->rel_line_to(1,1);
}
cr->stroke();
}
}
return true;
}