-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphicsStuff.cpp
More file actions
289 lines (239 loc) · 9.05 KB
/
graphicsStuff.cpp
File metadata and controls
289 lines (239 loc) · 9.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <fstream>
using namespace std;
#define DIMX 400
#define DIMY 400
struct Punto {
int x, y;
};
// These are the functions implemented this file
void writeImage2File(queue<int> thePath, string _mapName);
void drawPath(double theImage[DIMX][DIMY][3], queue<int> thePath);
void drawConnections(double theImage[DIMX][DIMY][3]);
void drawNodes(double theImage[DIMX][DIMY][3]);
void drawSquare(double theImage[DIMX][DIMY][3], int x, int y, int w, double r, double g, double b);
void drawSegment(double theImage[DIMX][DIMY][3], int x1, int y1, int x2, int y2, int w, double r, double g, double b);
void readMap(string _mapName);
void writeBMPFile(double theImage[DIMX][DIMY][3]);
// Some global variables :-)
vector<Punto> theCoords;
vector< vector<int> > theCosts;
void writeImage2File(queue<int> thePath, string _mapName) {
readMap(_mapName);
double theImage[DIMX][DIMY][3];
// give the image background
for (int i = 0; i < DIMX; i++) {
for (int j = 0; j < DIMX; j++) {
theImage[i][j][0] = 0.95;
theImage[i][j][1] = 0.95;
theImage[i][j][2] = 0.95;
}
}
drawPath(theImage, thePath);
drawConnections(theImage);
drawNodes(theImage);
writeBMPFile(theImage);
}
void drawPath(double theImage[DIMX][DIMY][3], queue<int> thePath) {
int xmin = 99999, xmax = -99999, ymin = 99999, ymax = -99999;
for (int i = 0; i < theCoords.size(); i++) {
if (theCoords[i].x > xmax)
xmax = theCoords[i].x;
if (theCoords[i].x < xmin)
xmin = theCoords[i].x;
if (theCoords[i].y > ymax)
ymax = theCoords[i].y;
if (theCoords[i].y < ymin)
ymin = theCoords[i].y;
}
if (thePath.empty())
return;
int nuevo, cur;
cur = thePath.front();
thePath.pop();
while (!thePath.empty()) {
nuevo = thePath.front();
thePath.pop();
double c1x = ((double)(theCoords[cur].x - xmin) / (double)(xmax - xmin)) * (DIMX - 40) + 20;
double c1y = DIMY - (((double)(theCoords[cur].y - ymin) / (double)(ymax - ymin)) * (DIMY - 40) + 20);
double c2x = ((double)(theCoords[nuevo].x - xmin) / (double)(xmax - xmin)) * (DIMX - 40) + 20;
double c2y = DIMY - (((double)(theCoords[nuevo].y - ymin) / (double)(ymax - ymin)) * (DIMY - 40) + 20);
drawSegment(theImage, c1x, c1y, c2x, c2y, 7, 0.2, 1.0, 0.2);
cur = nuevo;
}
}
void drawConnections(double theImage[DIMX][DIMY][3]) {
int xmin = 99999, xmax = -99999, ymin = 99999, ymax = -99999;
for (int i = 0; i < theCoords.size(); i++) {
if (theCoords[i].x > xmax)
xmax = theCoords[i].x;
if (theCoords[i].x < xmin)
xmin = theCoords[i].x;
if (theCoords[i].y > ymax)
ymax = theCoords[i].y;
if (theCoords[i].y < ymin)
ymin = theCoords[i].y;
}
int cmax = 0, cmin = 99999;
for (int i = 0; i < theCoords.size(); i++) {
for (int j = 0; j < theCoords.size(); j++) {
if (theCosts[i][j] >= 0) {
if (cmin > theCosts[i][j])
cmin = theCosts[i][j];
if (cmax < theCosts[i][j])
cmax = theCosts[i][j];
}
}
}
double c;
double base = 0.3;
for (int i = 0; i < theCoords.size(); i++) {
for (int j = 0; j < theCoords.size(); j++) {
if (theCosts[i][j] >= 0) {
double c1x = ((double)(theCoords[i].x - xmin) / (double)(xmax - xmin)) * (DIMX - 40) + 20;
double c1y = DIMY - (((double)(theCoords[i].y - ymin) / (double)(ymax - ymin)) * (DIMY - 40) + 20);
double c2x = ((double)(theCoords[j].x - xmin) / (double)(xmax - xmin)) * (DIMX - 40) + 20;
double c2y = DIMY - (((double)(theCoords[j].y - ymin) / (double)(ymax - ymin)) * (DIMY - 40) + 20);
if (cmax == cmin)
c = 0.0;
else
c = (double)(theCosts[i][j] - cmin) / (double)(cmax - cmin);
double r = 1.0 * c + base * (1-c);
double g = base;
double b = base * c + 1.0 * (1-c);
drawSegment(theImage, c1x, c1y, c2x, c2y, 1, r, g, b);
}
}
}
}
void drawNodes(double theImage[DIMX][DIMY][3]) {
int xmin = 99999, xmax = -99999, ymin = 99999, ymax = -99999;
for (int i = 0; i < theCoords.size(); i++) {
if (theCoords[i].x > xmax)
xmax = theCoords[i].x;
if (theCoords[i].x < xmin)
xmin = theCoords[i].x;
if (theCoords[i].y > ymax)
ymax = theCoords[i].y;
if (theCoords[i].y < ymin)
ymin = theCoords[i].y;
}
// cout << "xmax: " << xmax << endl;
// cout << "ymax: " << xmin << endl;
// cout << "xmin: " << ymax << endl;
// cout << "ymin: " << ymin << endl;
for (int i = 0; i < theCoords.size(); i++) {
double cx = ((double)(theCoords[i].x - xmin) / (double)(xmax - xmin)) * (DIMX - 40) + 20;
double cy = DIMY - (((double)(theCoords[i].y - ymin) / (double)(ymax - ymin)) * (DIMY - 40) + 20);
//theImage[(int)cx][(int)cy][0] = 0.0;
//theImage[(int)cx][(int)cy][1] = 0.0;
//theImage[(int)cx][(int)cy][2] = 0.0;
drawSquare(theImage, (int)cx, (int)cy, 3, 0.0, 0.0, 0.0);
}
}
void drawSquare(double theImage[DIMX][DIMY][3], int x, int y, int w, double r, double g, double b) {
for (int i = x - (w+1)/2; i <= x + (w+1)/2; i++) {
for (int j = y - (w+1)/2; j <= y + (w+1)/2; j++) {
theImage[i][j][0] = r;
theImage[i][j][1] = g;
theImage[i][j][2] = b;
}
}
}
void drawSegment(double theImage[DIMX][DIMY][3], int x1, int y1, int x2, int y2, int w, double r, double g, double b) {
double inc = 0.001;
double x, y, cx, cy;
for (double t = 0.0; t <= 1.0; t += inc) {
x = t * x1 + (1.0 - t) * x2;
y = t * y1 + (1.0 - t) * y2;
cx = (int)(x + 0.5);
cy = (int)(y + 0.5);
drawSquare(theImage, cx, cy, w, r, g, b);
}
}
// reads the file and stores the data in the global variables theCoords and theCosts
void readMap(string _mapName) {
// string filename = "../Maps/" + _mapName + ".txt";
// ifstream input(filename);
ifstream input(_mapName);
if (input.fail()) {
cout << "file " << _mapName << " does not exist" << endl;
exit(1);
}
int numNodes;
input >> numNodes;
for (int i = 0; i < numNodes; i++) {
vector<int> myRow;
for (int j = 0; j < numNodes; j++) {
int val;
input >> val;
myRow.push_back(val);
}
theCosts.push_back(myRow);
}
for (int i = 0; i < numNodes; i++) {
Punto p;
input >> p.x;
input >> p.y;
theCoords.push_back(p);
}
input.close();
}
// code in this function was modifed from code obtained at this website (obtained 8/12/2021):
// https://newbedev.com/writing-bmp-image-in-pure-c-c-without-other-libraries
void writeBMPFile(double theImage[DIMX][DIMY][3]) {
int w = DIMX;
int h = DIMY;
int x, y;
unsigned char r, g, b;
FILE *f;
unsigned char *img = NULL;
int filesize = 54 + 3*w*h; //w is your image width, h is image height, both int
img = (unsigned char *)malloc(3*w*h);
memset(img,0,3*w*h);
for(int i = 0; i < w; i++) {
for(int j = 0; j < h; j++) {
x = i;
y = (h - 1) - j;
r = (unsigned char)(theImage[i][j][0]*255);
g = (unsigned char)(theImage[i][j][1]*255);
b = (unsigned char)(theImage[i][j][2]*255);
if (r > 255)
r = 255;
if (g > 255)
g = 255;
if (b > 255)
b = 255;
img[(x+y*w)*3+2] = (unsigned char)(r);
img[(x+y*w)*3+1] = (unsigned char)(g);
img[(x+y*w)*3+0] = (unsigned char)(b);
}
}
unsigned char bmpfileheader[14] = {'B','M', 0,0,0,0, 0,0, 0,0, 54,0,0,0};
unsigned char bmpinfoheader[40] = {40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0, 24,0};
unsigned char bmppad[3] = {0,0,0};
bmpfileheader[ 2] = (unsigned char)(filesize );
bmpfileheader[ 3] = (unsigned char)(filesize>> 8);
bmpfileheader[ 4] = (unsigned char)(filesize>>16);
bmpfileheader[ 5] = (unsigned char)(filesize>>24);
bmpinfoheader[ 4] = (unsigned char)( w );
bmpinfoheader[ 5] = (unsigned char)( w>> 8);
bmpinfoheader[ 6] = (unsigned char)( w>>16);
bmpinfoheader[ 7] = (unsigned char)( w>>24);
bmpinfoheader[ 8] = (unsigned char)( h );
bmpinfoheader[ 9] = (unsigned char)( h>> 8);
bmpinfoheader[10] = (unsigned char)( h>>16);
bmpinfoheader[11] = (unsigned char)( h>>24);
f = fopen("Map.bmp","wb");
fwrite(bmpfileheader,1,14,f);
fwrite(bmpinfoheader,1,40,f);
for(int i = 0; i < h; i++) {
fwrite(img+(w*(h-i-1)*3),3,w,f);
fwrite(bmppad,1,(4-(w*3)%4)%4,f);
}
free(img);
fclose(f);
}