-
Notifications
You must be signed in to change notification settings - Fork 0
/
P3.cpp
74 lines (62 loc) · 1.81 KB
/
P3.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
/* basic i/o */
#include <iostream>
#include <fstream>
#include <sstream>
/* data stack */
#include <vector>
using namespace std;
int main(void) {
ifstream ifs("Input_P3.txt");
string is;
int count = 0; /* Number of circles */
/* check if file is empty and ignore the first line */
if (!getline(ifs, is)) {
cout << "Error: File Corrupted..." << endl;
return 1;
}
/* read the data and store into the 2d array */
int mode = 0;
vector<vector<double>> c;
while (getline(ifs, is)) {
if (is.length() == 0) {
mode = 0; continue;
}
else if (is.compare(0, 19, "circle image points") == 0) {
mode = 1; continue;
}
istringstream iss(is);
vector<double> x(3);
iss >> x[0] >> x[1] >> x[2];
if (mode == 1) {
c.push_back(x);
}
}
ifs.close(); // close file handle
/* [v] 2022. 3. 13. 16:05 -> Check the Piazza Q&A answer about output ps scale */
constexpr float SCALE = 72;
constexpr float LINEWIDTH = 1;
ofstream output;
output.open("P3.ps");
/* magic number */
output << "%!PS" << '\n';
for (int i = 0; i < c.size(); i++) {
output << "newpath" << '\n'; // newpath
output
<< c[0][0] * SCALE << ' '
<< c[0][1] * SCALE << ' '
<< c[0][2] * SCALE << ' '
<< 0.0 << ' ' << 360.0 << ' ' << "arc" << '\n';
output << "closepath" << '\n'; // closepath
output << "gsave" << '\n'; // as the command 'fill' consumes current path, save current state for the subsequent command
output << 0.9 << ' ' << "setgray" << '\n';
output << "fill" << '\n';
output << "grestore" << '\n'; // continue to work on the path
output << 0.4 << ' ' << "setgray" << '\n';
output << LINEWIDTH << ' ' << "setlinewidth" << '\n';
output << "stroke" << '\n';
}
/* showpage */
output << "showpage" << endl;
output.close();
return 0;
}