-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
232 lines (227 loc) · 8.99 KB
/
main.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
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
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include "colour.h"
#include "stringUtils.h"
#include "viewport.h"
using namespace std;
int main(int argc, char *argv[]) {
bool utf = true;
int pixelsHigh = 0;
int pixelsWide = 0;
double minX = -2.5;
double maxX = 1.5;
double minY = -2;
double maxY = 2;
double stretch_k = 1.0;
double theta = 0.0;
int iterates = 1000;
int antialiasing = 2;
double magnification = 1;
double colourScale = 128;
int colourOffset = 0;
vector<string> args(argv, argv + argc);
string saveLocation = "";
for (int i = 0; i < args.size(); ++i) {
if (args.at(i) == "--help") {
cout << "Usage:" << args.at(0)
<< " [--min-x <min x>] [--max-x <max x>] [--min-y <min y>] "
"[--max-y <max y>] [--antialiasing <antialiasing>] "
"[--width <pixels wide>] [--height <pixels high>] "
"[--no-utf] [--k <k value>] [--theta <theta value>] "
"[--iterates <max iterations>] [--dir <save location>]"
<< endl;
cout << "Keys:" << endl;
cout << "h,j,k,l: scroll left, down, up, right." << endl;
cout << "u,d: zoom in, out." << endl;
cout << "n,m: colour scale up, down." << endl;
cout << "e,r: iteration count up, down." << endl;
cout << "p: print to file, supports any output format supported by "
"imagemagick."
<< endl;
cout << "q: quit." << endl;
cout << endl
<< "UTF characters are on by default as they increase the "
"resolution in both directions by a factor of 2; if you "
"are having problems displaying images then please use "
"--no-utf."
<< endl;
cout << "Detected terminal size: " << pixelsHigh << "x"
<< pixelsWide << endl;
return 0;
}
if (args.at(i) == "--min-x" && args.size() > i + 1) {
minX = stringUtils::fromString<double>(args.at(i + 1));
}
if (args.at(i) == "--max-x" && args.size() > i + 1) {
maxX = stringUtils::fromString<double>(args.at(i + 1));
}
if (args.at(i) == "--min-y" && args.size() > i + 1) {
minY = stringUtils::fromString<double>(args.at(i + 1));
}
if (args.at(i) == "--max-y" && args.size() > i + 1) {
maxY = stringUtils::fromString<double>(args.at(i + 1));
}
if (args.at(i) == "--height" && args.size() > i + 1) {
pixelsHigh = stringUtils::fromString<int>(args.at(i + 1));
}
if (args.at(i) == "--width" && args.size() > i + 1) {
pixelsWide = stringUtils::fromString<int>(args.at(i + 1));
}
if (args.at(i) == "--no-utf") {
utf = false;
}
if (args.at(i) == "--antialiasing" && args.size() > i + 1) {
antialiasing = stringUtils::fromString<int>(args.at(i + 1));
}
if (args.at(i) == "--k" && args.size() > i + 1) {
stretch_k = stringUtils::fromString<double>(args.at(i + 1));
}
if (args.at(i) == "--theta" && args.size() > i + 1) {
theta = stringUtils::fromString<double>(args.at(i + 1));
}
if (args.at(i) == "--dir" && args.size() > i + 1) {
saveLocation = args.at(i + 1);
}
if (args.at(i) == "--iterates" && args.size() > i + 1) {
iterates = stringUtils::fromString<int>(args.at(i + 1));
}
}
if (pixelsWide == 0 && pixelsHigh == 0) {
struct winsize w;
ioctl(0, TIOCGWINSZ, &w);
cout << "w:" << w.ws_col << "; h:" << w.ws_row << " ";
if ((w.ws_col / 2) < w.ws_row) {
pixelsHigh = w.ws_col;
pixelsWide = w.ws_col;
cout << "width" << endl;
if (!utf) {
pixelsHigh /= 2;
pixelsWide /= 2;
}
} else {
pixelsHigh = w.ws_row;
pixelsWide = w.ws_row;
cout << "height" << endl;
pixelsHigh -= 2;
pixelsWide -= 2;
if (utf) {
pixelsHigh *= 2;
pixelsWide *= 2;
}
}
}
termios before, after;
tcgetattr(STDIN_FILENO,
&before); // fill 'before' with current termios values
after = before; // make a copy to be modified
after.c_lflag &=
(~ICANON); // Disable canonical mode, including line buffering
after.c_lflag &= (~ECHO); // Don't echo characters on the screen (optional)
tcsetattr(STDIN_FILENO, TCSANOW, &after); // Set the modified flags
char ch;
// cout << "\033[s";
while (true) {
viewport camera(minX, maxX, minY, maxY, stretch_k, theta, pixelsHigh,
pixelsWide, antialiasing, colourScale, colourOffset,
iterates);
camera.render();
cout << "\033[u";
cout << "\033[0;0H";
if (utf) {
camera.drawToUnicode();
} else {
camera.drawToTerminal();
}
cout << "\033[2KMagnification: " << magnification
<< "; x: " << (minX + maxX) / 2 << "; y: " << (minY + maxY) / 2
<< "; iterates: " << iterates << ";";
if (ch = cin.get()) {
if (ch == 'q') {
cout << endl;
tcsetattr(STDIN_FILENO, TCSANOW, &before);
return 0;
} else if (ch == 'h') {
double tempMinX = minX - (maxX - minX) / 2.0;
maxX = maxX - (maxX - minX) / 2.0;
minX = tempMinX;
} else if (ch == 'j') {
double tempMinY = minY + (maxY - minY) / 2.0;
maxY = maxY + (maxY - minY) / 2.0;
minY = tempMinY;
} else if (ch == 'k') {
double tempMinY = minY - (maxY - minY) / 2.0;
maxY = maxY - (maxY - minY) / 2.0;
minY = tempMinY;
} else if (ch == 'l') {
double tempMinX = minX + (maxX - minX) / 2.0;
maxX = maxX + (maxX - minX) / 2.0;
minX = tempMinX;
} else if (ch == 'u') {
double tempMinX = minX + (maxX - minX) / 4;
maxX = maxX - (maxX - minX) / 4;
minX = tempMinX;
double tempMinY = minY + (maxY - minY) / 4;
maxY = maxY - (maxY - minY) / 4;
minY = tempMinY;
magnification *= 2;
} else if (ch == 'd') {
double tempMinX = 3.0 * minX / 2.0 - maxX / 2.0;
maxX = 3.0 * maxX / 2.0 - minX / 2.0;
minX = tempMinX;
double tempMinY = 3.0 * minY / 2.0 - maxY / 2.0;
maxY = 3.0 * maxY / 2.0 - minY / 2.0;
minY = tempMinY;
magnification /= 2;
} else if (ch == '.') {
colourOffset += 100;
} else if (ch == ',') {
colourOffset -= 100;
} else if (ch == 'n') {
colourScale *= 2;
} else if (ch == 'm') {
colourScale /= 2;
} else if (ch == 'e') {
iterates *= 2;
} else if (ch == 'r') {
iterates /= 2;
} else if (ch == 'p') {
string defaultFilename =
saveLocation + "fracviewX" +
stringUtils::toString<double>((maxX + minX) / 2) + "Y" +
stringUtils::toString<double>((maxY + minY) / 2) + "." +
stringUtils::toString<double>(magnification) + ".png";
string filename;
string defaultResX = "800";
string resX;
string defaultResY = "800";
string resY;
tcsetattr(STDIN_FILENO, TCSANOW, &before);
cout << "Enter filename (" << defaultFilename << "):";
getline(cin, filename);
if (filename.empty()) {
filename = defaultFilename;
}
cout << "Enter x resolution (800):";
getline(cin, resX);
if (resX.empty()) {
resX = defaultResX;
}
cout << "Enter y resolution (800):";
getline(cin, resY);
if (resY.empty()) {
resY = defaultResY;
}
tcsetattr(STDIN_FILENO, TCSANOW, &after);
viewport camera(minX, maxX, minY, maxY, stretch_k, theta,
stringUtils::fromString<int>(resX),
stringUtils::fromString<int>(resY),
antialiasing, colourScale, colourOffset);
camera.render();
camera.drawToFile(filename);
cin.ignore(); // clear the input buffer
}
}
}
return 0;
}