-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.h
More file actions
330 lines (272 loc) · 8.87 KB
/
plot.h
File metadata and controls
330 lines (272 loc) · 8.87 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
MIT License
Copyright (c) 2016 Panagiotis Petridis
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <bits/stdc++.h>
using namespace std;
int w = 128; // Width
int h = 128; // Height
int maxColorVal = 255; // Maximum Value that each color pixel can have
// Variables to set plot to scale
double Xmax = -99999;
double Xmin = +99999;
double Ymax = -99999;
double Ymin = +99999;
// RGB Color Struct
struct Color{
double r; // Red
double g; // Green
double b; // Blue
// Default Constructor
Color(){
r = 0;
g = 0;
b = 0;
}
// Constructor with parameters
template<typename T>
Color(T rr, T gg, T bb){
r = (double)rr;
g = (double)gg;
b = (double)bb;
}
};
// 2D point struct
struct point{
double x;
double y;
// Default constructor
point(){ x = 0; y = 0; }
// Constructor with parameters
template<typename T>
point(T a, T b){ x = a; y = b; }
};
// Converts 2D vector to point vector
template <typename T>
vector<point> getPointVec(vector< vector<T> > v, int xAxis, int yAxis){
vector<point> temp;
for(int i = 0; i < v.size(); i++)temp.push_back(point(v[i][xAxis], v[i][yAxis]));
return temp;
}
//Check if filename is valid
bool validName(string filename){
return (filename.find(".ppm")==filename.size()-4);
}
// Sets color of all Pixels to white
void setPixelsToWhite(vector< vector<Color> > &pixels){
for(int i = 0; i < pixels.size(); i++){
for(int j = 0; j < pixels[i].size(); j++){
pixels[i][j].r = 255;
pixels[i][j].g = 255;
pixels[i][j].b = 255;
}
}
}
// Prints point vector
void printPointVec(vector<point> a){ for(int i = 0; i < a.size(); i++)cout << "x: " << a[i].x << "\t" << "y: " << a[i].y << endl; }
// Returns PPM file Header
string getPPMHeader(){
ostringstream ppmHeader;
ppmHeader << "P3\n" << w << endl << h << endl << maxColorVal << endl;
return ppmHeader.str();
}
// Gets a string version of everything that needs to be printed
string getPixelsPrintString(vector< vector<Color> > &pixels){
ostringstream pixelsString;
for(int y = h-1; y >= 0; y--){
for(int x = 0; x < w; x++){
pixelsString << pixels[y][x].r << endl;
pixelsString << pixels[y][x].g << endl;
pixelsString << pixels[y][x].b << endl;
}
}
return pixelsString.str();
}
// Returns Pixels Vector
vector< vector<Color> > getPixelsVector(){ return vector< vector<Color> > (h, vector<Color>(w)); }
// Sets image width
template<typename T>
void setWidth(T a){ w = a; }
// Sets image height
template<typename T>
void setHeight(T a){ h = a; }
// Tests that everything works properly
void testPlot(string filename){
assert(validName(filename));
vector< vector<Color> > pixels = getPixelsVector();
ofstream write;
write.open(filename.c_str());
// Write PPM header
write << getPPMHeader();
for(int y = 0; y < h; y++){
for(int x = 0; x < w; x++){
pixels[y][x].r = 255;
pixels[y][x].g = 0;
pixels[y][x].b = 255;
}
}
write << getPixelsPrintString(pixels);
write.close();
}
// Sets Global Minimum and Maximum Values
void setMinMaxScalers(vector<point> points){
for(int i = 0; i < points.size(); i++){
if(points[i].x>Xmax)Xmax=points[i].x;
if(points[i].x<Xmin)Xmin=points[i].x;
}
for(int i = 0; i < points.size(); i++){
if(points[i].y>Ymax)Ymax=points[i].y;
if(points[i].y<Ymin)Ymin=points[i].y;
}
}
// Sets vector values and grid to the correct scale so that they display nicely on the image
void setVecToScale(vector<point> &points){
for(int i = 0; i < points.size(); i++){
points[i].x = (int)(((w)*(points[i].x - Xmin))/(Xmax - Xmin));
points[i].x -= points[i].x>0 ? 1 : 0;
}
for(int i = 0; i < points.size(); i++){
points[i].y = (int)(((h)*(points[i].y - Ymin))/(Ymax - Ymin));
points[i].y -= points[i].y>0 ? 1 : 0;
}
}
// Makes cross on pixels
void makeCross(vector< vector<Color> > &pixels, int x, int y, double r, Color c){
if(x<pixels[0].size() && x>=0 && y<pixels.size() && y>=0)pixels[y][x] = c;
else return;
for(int i = 1; i < r; i++){
if(y+i<pixels.size()){pixels[y+i][x] = c; makeCross(pixels, x, y+i, r*0.15, c);}
if(x+i<pixels[0].size()){pixels[y][x+i] = c; makeCross(pixels, x+i, y, r*0.15, c);}
if(x-i>=0){pixels[y][x-i] = c; makeCross(pixels, x-i, y, r*0.15, c);}
if(y-i>=0){pixels[y-i][x] = c; makeCross(pixels, x, y-i, r*0.15, c);}
}
}
// Makes dot on pixels
void makeDot(vector< vector<Color> > &pixels, int x, int y, double r, Color c){
if(x<pixels[0].size() && x>=0 && y<pixels.size() && y>=0)pixels[y][x] = c;
else return;
for(int i = 1; i < r; i++){
if(y+i<pixels.size()){pixels[y+i][x] = c; makeDot(pixels, x, y+i, r-1, c);}
if(x+i<pixels[0].size()){pixels[y][x+i] = c; makeDot(pixels, x+i, y, r-1, c);}
if(x-i>=0){pixels[y][x-i] = c; makeDot(pixels, x-i, y, r-1, c);}
if(y-i>=0){pixels[y-i][x] = c; makeDot(pixels, x, y-i, r-1, c);}
}
}
// Adds Grid to plot
void addGridToPlot(vector< vector<Color> > &pixels){
for(int y = h-1; y >= 0; y--){
for(int x = 0; x < w; x++){
if(x%((int)w/10)==0 || y%((int)h/10)==0){
pixels[y][x].r = 180;
pixels[y][x].g = 180;
pixels[y][x].b = 180;
}
}
}
}
// Prints empty image with grid
void plotTestGrid(string filename){
assert(validName(filename));
vector< vector<Color> > pixels = getPixelsVector();
setPixelsToWhite(pixels);
addGridToPlot(pixels);
ofstream write;
write.open(filename.c_str());
write << getPPMHeader();
write << getPixelsPrintString(pixels);
write.close();
}
// Plots Points on image
template <typename T>
void plotPoints(string filename, vector< vector< vector<T> > > vVec, int xAxis, int yAxis, double r, vector<Color> cVec, vector<string> typeVec){
assert(validName(filename));
vector< vector<Color> > pixels = getPixelsVector();
setPixelsToWhite(pixels);
addGridToPlot(pixels);
Xmax = -99999;
Xmin = +99999;
Ymax = -99999;
Ymin = +99999;
for(int i = 0; i < vVec.size(); i++)setMinMaxScalers(getPointVec(vVec[i], xAxis, yAxis));
for(int i = 0; i < vVec.size(); i++){
vector<point> points = getPointVec(vVec[i], xAxis, yAxis);
setVecToScale(points);
string type = typeVec[i];
Color c = cVec[i];
for(int i = 0; i < points.size(); i++){
int x = points[i].x;
int y = points[i].y;
if(type=="*")makeDot(pixels, x, y, r, c);
if(type=="+")makeCross(pixels, x, y, r, c);
}
}
ofstream write;
write.open(filename.c_str());
write << getPPMHeader();
write << getPixelsPrintString(pixels);
write.close();
}
// adds thicknss to pixels
void paintUpDownOnPixels(int x, int y, int thickness, Color c, vector< vector<Color> > &pixels){
for(int i = 1; i <= thickness; i++){
if(y+i<pixels.size())pixels[y+i][x] = c;
if(y-i>=0)pixels[y-i][x] = c;
if(x+i<pixels.size())pixels[y][x+i] = c;
if(x-i>=0)pixels[y][x-i] = c;
if(x-i>=0 && y-i>=0)pixels[y-i][x-i] = c;
if(x+i<pixels.size() && y+i<pixels.size())pixels[y+i][x+i] = c;
}
}
// Draws Line on Pixels
void makeLineOnPixels(int x1, int y1, int x2, int y2, Color c, double r, vector< vector<Color> > &pixels){
double dx = x2 - x1;
double dy = y2 - y1;
int x = x1;
while(x != x2){
int y = y1 + dy * (x-x1)/dx;
pixels[y][x] = c;
paintUpDownOnPixels(x, y, r, c, pixels);
x = x>x2 ? x-1 : x+1;
}
}
template <typename T>
void plotLine(string filename, vector< vector< vector<T> > > vVec, int xAxis, int yAxis, double r, vector<Color>cVec){
assert(validName(filename));
vector< vector<Color> > pixels = getPixelsVector();
setPixelsToWhite(pixels);
addGridToPlot(pixels);
Xmax = -99999;
Xmin = +99999;
Ymax = -99999;
Ymin = +99999;
for(int i = 0; i < vVec.size(); i++)setMinMaxScalers(getPointVec(vVec[i], xAxis, yAxis));
for(int vi = 0; vi < vVec.size(); vi++){
vector<point> points = getPointVec(vVec[vi], xAxis, yAxis);
setVecToScale(points);
Color c = cVec[vi];
for(int i = 1; i < points.size(); i++){
if(i==0)makeLineOnPixels(0, 0, points[i].x, points[i].y, c, r, pixels);
else makeLineOnPixels(points[i-1].x, points[i-1].y, points[i].x, points[i].y, c, r, pixels);
}
}
ofstream write;
write.open(filename.c_str());
write << getPPMHeader();
write << getPixelsPrintString(pixels);
write.close();
}