-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathaltitude_graph.cpp
111 lines (90 loc) · 2.57 KB
/
altitude_graph.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
#include "altitude_graph.hpp"
#include "text.hpp"
using namespace std;
namespace fs = boost::filesystem;
#define BORDER_X 50
#define BORDER_Y 50
AltitudeGraph::AltitudeGraph(settings_t& _s)
: s(_s), width(800), height(600)
{
altitudeRegistry.reset(new long[mc::MapY]);
for(int i = 0; i < mc::MapY; i++)
{
altitudeRegistry[i] = 0;
}
}
void AltitudeGraph::createGraph()
{
image_ptr graphImg;
memory_image* image = new memory_image(width, height);
graphImg.reset(image);
color bgcolor(255, 255, 255, 255);
color fgcolor(150,0,0,255);
color axiscolor(0,0,0,255);
color seacolor(0,0,255,255);
text::font_face ffsea(s.ttf_path, 8, seacolor);
ffsea.init();
text::font_face ff12(s.ttf_path, 12, axiscolor);
ff12.init();
// fill background
graphImg->fill(bgcolor);
int _w = width - BORDER_X;
int _h = height - BORDER_Y;
long maxVal = this->getMax();
int x_step = _w / mc::MapY;
std::stringstream maxss;
maxss << "MAX = " << maxVal;
ff12.draw(graphImg, "NB of blocks", BORDER_X - 30 , BORDER_Y - 25);
ff12.draw(graphImg, maxss.str(), BORDER_X - 20 , BORDER_Y - 10);
ff12.draw(graphImg, "Altitude", _w - 2*BORDER_X , _h + 17);
int x=0, y=0, x0=BORDER_X, y0=_h;
for(int i = 0; i < mc::MapY; i++)
{
x = BORDER_X + x_step*i;
if (maxVal == 0) {
y = _h;
} else {
y = _h - (int)( ( (float)altitudeRegistry[i] / (float)maxVal ) * (_h-BORDER_Y) );
}
graphImg->draw_line(x, y, x0, y0, fgcolor);
x0 = x;
y0 = y;
}
// draw axis
graphImg->draw_line(BORDER_X, BORDER_Y, BORDER_X, _h, axiscolor);
graphImg->draw_line(BORDER_X, _h, _w, _h, axiscolor);
// draw axis labels
for(int i=0; i < mc::MapY; i++)
{
color _axiscolor = axiscolor;
x = BORDER_X + x_step*i;
int size = 2;
if(i == 63)
{
_axiscolor = seacolor;
ffsea.draw(graphImg, "Sea", x+4, _h+25);
size = 25;
}
if(i%10 == 0)
{
size = 5;
}
graphImg->draw_line(x, _h, x, _h+size, _axiscolor);
}
png_format::opt_type opts;
graphImg->save<png_format>(s.statistics_path.string() + "_graph.png", opts);
}
void AltitudeGraph::registerBloc(mc::MaterialT *material, int altitude)
{
altitudeRegistry[altitude] += 1;
}
long AltitudeGraph::getMax()
{
long max = 0;
for(int i = 0; i < mc::MapY; i++)
{
if(max < altitudeRegistry[i])
max = altitudeRegistry[i];
}
return max;
}