forked from bishyboi/DSAProj_3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphics.cpp
More file actions
187 lines (163 loc) · 5.61 KB
/
Graphics.cpp
File metadata and controls
187 lines (163 loc) · 5.61 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <valarray>
#include <iostream>
#include "Graphics.h"
Graphics::Graphics()
{
window.create(sf::VideoMode(950, 600), "Group project");
textBoxString = "Enter Language";
textBack.setSize(sf::Vector2f(200, 20));
textBack.setFillColor(sf::Color::White);
textBack.setPosition(sf::Vector2f(875 / 2 - (textBack.getSize().x / 2), 30));
// set up texts
font.loadFromFile("fonts/times.ttf");
textLang.setFont(font);
textLang.setCharacterSize(18);
textLang.setFillColor(sf::Color::Red);
textLang.setPosition(textBack.getPosition().x + 5, textBack.getPosition().y - 3);
// x scale text
for (int i = 0; i <= END_YEAR - START_YEAR; i++)
{
xLabels.emplace_back(sf::Text());
xLabels[i].setString(std::to_string((START_YEAR + i) % 100));
if (xLabels[i].getString().getSize() < 2)
{
xLabels[i].setString("0" + xLabels[i].getString());
}
xLabels[i].setFont(font);
xLabels[i].setFillColor(sf::Color::White);
xLabels[i].setCharacterSize(12);
// position based on how many years there are
xLabels[i].setPosition(i * (850 / (END_YEAR - START_YEAR)) + 25, 550);
xLabels[i].setRotation(315.f);
}
// y scale text
for (int i = 0; i < 11; i++)
{
yLabels.emplace_back(sf::Text());
yLabels[i].setString("0");
yLabels[i].setFillColor(sf::Color::White);
yLabels[i].setFont(font);
yLabels[i].setPosition(0, 82 + (i * 45));
yLabels[i].setCharacterSize(12);
}
// cool math that makes the graph axis bars
for (int i = 0; i < 2; i++)
{
axisBars.emplace_back(sf::RectangleShape());
axisBars[i].setPosition(25, 540);
axisBars[i].setRotation(i * 270);
axisBars[i].setFillColor(sf::Color::White);
axisBars[i].setSize(sf::Vector2f(845 - (i * 390), 3));
}
// set up timer characteristics
timer1.setFont(font);
timer1.setFillColor(sf::Color::White);
timer1.setCharacterSize(18);
timer1.setPosition(sf::Vector2f(textBack.getSize().x + textBack.getPosition().x + 10, textBack.getPosition().y - 10));
timer2.setFont(font);
timer2.setFillColor(sf::Color::White);
timer2.setCharacterSize(18);
timer2.setPosition(sf::Vector2f(textBack.getSize().x + textBack.getPosition().x + 10, textBack.getPosition().y + 10));
xAxis.setFont(font);
xAxis.setFillColor(sf::Color::White);
xAxis.setCharacterSize(18);
xAxis.setPosition(400, 560);
xAxis.setString("Year");
yAxis.setFont(font);
yAxis.setFillColor(sf::Color::White);
yAxis.setCharacterSize(18);
yAxis.setPosition(925, 180);
yAxis.setRotation(90);
yAxis.setString("Average # of swears in a song");
}
void Graphics::drawConstants(std::string time1, std::string time2)
{
window.draw(textBack);
textLang.setString(textBoxString);
window.draw(textLang);
std::string string1 = timer1.getString();
string1 = string1 + time1;
timer1.setString(string1);
std::string string2 = timer2.getString();
string2 = string2 + time2;
timer2.setString(string2);
window.draw(timer1);
window.draw(timer2);
timer1.setString("Hash map's time (micro s): ");
timer2.setString("Heap's time (micro s): ");
for (int i = 0; i < xLabels.size(); i++)
{
window.draw(xLabels[i]);
}
for (int i = 0; i < yLabels.size(); i++)
{
window.draw(yLabels[i]);
}
for (int i = 0; i < axisBars.size(); i++)
{
window.draw(axisBars[i]);
}
}
void Graphics::drawGraph(std::vector<double> data)
{
// clear old data
graphBars.clear();
dataLabels.clear();
// y axis scaling
float largestBar = 0;
for (int i = 0; i < data.size(); i++)
{
if (data[i] > largestBar)
largestBar = data[i];
}
largestBar = std::pow(10.0, std::ceil(std::log10(largestBar)));
for (int i = 0; i < 11; i++)
{
yLabels[i].setString(std::to_string(int((largestBar / 10) * (10 - i))));
}
// make bars for graph
for (int i = 0; i < data.size(); i++)
{
graphBars.emplace_back(sf::RectangleShape());
graphBars[i].setFillColor(sf::Color::Cyan);
// position is based on the year below it, size is based on the data spread over how many pixels we have
graphBars[i].setPosition(i * (850 / (END_YEAR - START_YEAR)) + 28 + (850 / (END_YEAR - START_YEAR)), 540);
if (data[i] != -1)
{
graphBars[i].setSize(sf::Vector2f(850 / (END_YEAR - START_YEAR), 450 * (data[i] / largestBar)));
}
graphBars[i].setRotation(180);
}
// draw bars for graph
for (int i = 0; i < graphBars.size(); i++)
{
window.draw(graphBars[i]);
}
// set up data labels
for (int i = 0; i < data.size(); i++)
{
dataLabels.emplace_back(sf::Text());
if (data[i] != -1)
{
dataLabels[i].setString(std::to_string(data[i]).substr(0, 3));
}
else
{
// if there is no data set text to Null
dataLabels[i].setString("Null");
}
dataLabels[i].setFont(font);
dataLabels[i].setFillColor(sf::Color::White);
dataLabels[i].setCharacterSize(12);
dataLabels[i].setRotation(270);
// variable sized based on how many years we have
dataLabels[i].setPosition(sf::Vector2f(graphBars[i].getPosition().x - (850 / (END_YEAR - START_YEAR) + 2), 535 - graphBars[i].getSize().y));
}
// draw data labels for graph
for (int i = 0; i < dataLabels.size(); i++)
{
window.draw(dataLabels[i]);
}
window.draw(xAxis);
window.draw(yAxis);
}