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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
//============================================================================
// Author : Sven Gothel, Qun Gothel, Svenson Gothel
// Copyright : 2022 Gothel Software e.K.
// License : MIT
// Description : C++ Lesson 0.3 Geometry 0 (Simplified Version)
//============================================================================
#include <cstdio>
#include <cmath>
#include <string>
/**
* Lesson 0.3
*
* Implementing rendering of simple geometric Objects using ASCII-Art (simplified)
*/
void print_space(const int n) {
for(int i = 0; i < n; ++i) {
printf(" "); // space
}
}
void print_mark(const int n, const char c='X') {
for(int i = 0; i < n; ++i) {
printf("%c", c);
}
}
void print_newline() {
printf("\n"); // newline or line-feed (lf)
}
/**
* Round given float and return its integer representation.
*
* This function utilizes `constexpr`, i.e. might be evaluated at compile time
* if the used parameters are of constexpr nature.
*
* @param v the float
* @return the rounded integer representation
*/
constexpr int round_to_int(const float v) noexcept {
return static_cast<int>( std::round( v ) );
}
/**
* Paint a simple square, not-filled
* @param len length of each side of the square
* @param dx distance on x-axis, aka the x-position of object's left edge
*/
void square(const int len, const int dx=0) {
print_newline();
for(int y=0; y<len; ++y) {
print_space(dx);
for(int x=0; x<len; ++x) {
if( 0 == y || len-1 == y || 0 == x || len-1 == x ) {
print_mark( 1 );
} else {
print_space( 1 );
}
}
print_newline();
}
}
/**
* Pyramid w/ base_len == 4
```
* XXXX
* XX
```
*
* Pyramid w/ base_len == 5
```
* XXXXX
* XXX
* X
```
*
* @param base_len length of pyramid's base
* @param dx distance on x-axis, aka the x-position of object's left edge
* @param show_title
*/
void pyramid_down(const int base_len, const int dx=0, const bool show_title=true) {
if( show_title ) {
printf("\nPyramid(down, l %d, dx %d)\n", base_len, dx);
}
for(int i=base_len; i > 0; i-=2) {
print_space( dx + ( base_len - i ) / 2 );
print_mark( i );
print_newline();
}
}
/**
* Pyramid w/ base_len == 4
```
* XX
* XXXX
```
*
* Pyramid w/ base_len == 5
```
* X
* XXX
* XXXXX
```
*
* @param base_len length of pyramid's base
* @param dx distance on x-axis, aka the x-position of object's left edge
* @param show_title
*/
void pyramid_up(const int base_len, const int dx=0, const bool show_title=true) {
if( show_title ) {
printf("\nPyramid(up, l %d, dx %d)\n", base_len, dx);
}
int i = 2 - ( base_len % 2 );
for(; i <= base_len; i+=2) {
print_space( dx + ( base_len - i ) / 2 );
print_mark( i );
print_newline();
}
}
/**
* Salino w/ base_len == 4
```
* XX
* XXXX
* XX
```
*
* Salino w/ base_len == 5
```
* X
* XXX
* XXXXX
* XXX
* X
```
*
* @param base_len length of pyramid's base
* @param dx distance on x-axis, aka the x-position of object's left edge
* @param show_title
*/
void salino(const int base_len, const int dx=0, const bool show_title=true) {
if( show_title ) {
printf("\nSalino(l %d, dx %d)\n", base_len, dx);
}
pyramid_up(base_len, dx, false);
pyramid_down(base_len-2, dx + 1, false);
}
/**
* Disk rendering.
*
* Implementation uses a floating point centroid, or barycenter.
*
* Disk1(r 1.00, dx 4, sx 1.00)
```
XX
XX
```
* Disk1(r 2.00, dx 3, sx 1.00)
```
XX
XXXX
XXXX
XX
```
* Disk1(r 3.00, dx 2, sx 1.00)
```
XXXX
XXXXXX
XXXXXX
XXXXXX
XXXXXX
XXXX
```
*
* @param radius radius of disk
* @param dx distance on x-axis, aka the x-position of object's left edge
* @param show_title
*/
void disk_1(const int radius, const int dx=0, const bool show_title=true) {
if( show_title ) {
printf("\nDisk1(r %d, dx %d)\n", radius, dx);
}
const float r_sq = (float)(radius*radius); // square of disk radius
const float disk_p0_x = (float)radius - 0.5f; // disk center point p0, x-component, a centroid
const float disk_p0_y = (float)radius - 0.5f; // disk center point p0, y-component, a centroid
const int aabbox_h = 2 * radius; // disk AABBox height
const int aabbox_w = 2 * radius; // disk AABBox width
for(int y=0; y<aabbox_h; ++y) {
print_space(dx);
for(int x=0; x<aabbox_w; ++x) {
const float a = disk_p0_x - (float)x;
const float b = disk_p0_y - (float)y;
const float dxy_p0_sq = a*a + b*b;
if( dxy_p0_sq <= r_sq ) {
print_mark( 1 );
} else {
print_space( 1 );
}
}
print_newline();
}
}
static std::string brightness("#=+-.");
/**
* Return a char, representing given brightness.
* @param b brightness ranging from [0..1]
*/
char get_char(const float b) {
const size_t max_idx = brightness.size()-1;
const float b_n = std::max<float>( 0.0, std::min<float>(1.0 , b) ); // normalize [0..1]
return brightness[ std::min<size_t>(max_idx, (size_t)std::round( b_n * (float)max_idx ) ) ];
}
/**
* Antialiased (AA) disk rendering using ASCII-Art alike brightness characters.
*
* Implementation uses a floating point centroid, or barycenter.
*
* Disk2(r 1.00, dx 4, sx 1.00, aa_seam 1.00)
```
--
--
```
* Disk2(r 2.00, dx 3, sx 1.00, aa_seam 1.00)
```
++
+##+
+##+
++
```
* Disk2(r 3.00, dx 2, sx 1.00, aa_seam 1.00)
```
.++.
.####.
+####+
+####+
.####.
.++.
```
* Disk2(r 3.00, dx 2, sx 2.00, aa_seam 1.00)
```
.-+++-.
.+#######+.
+#########+
+#########+
.+#######+.
.-+++-.
```
* @param radius radius of disk
* @param dx distance on x-axis, aka the x-position of object's left edge
* @param aa_seam AA seam of pixels considerd for AA brightness adjusted rendering
* @param show_title
*/
void disk_2(const int radius, const int dx=0, const float aa_seam_=1.0, const bool show_title=true) {
const float aa_seam = std::max<float>(1.0, aa_seam_);
if( show_title ) {
printf("\nDisk2(r %d, dx %d, aa_seam %.2f)\n", radius, dx, aa_seam);
}
const float disk_p0_x = (float)radius - 0.5f; // disk center point p0, x-component, a centroid
const float disk_p0_y = (float)radius - 0.5f; // disk center point p0, y-component, a centroid
const int aabbox_h = 2 * radius; // disk AABBox height
const int aabbox_w = 2 * radius; // disk AABBox width
for(int y=0; y<aabbox_h; ++y) {
print_space(dx);
for(int x=0; x<aabbox_w; ++x) {
const float a = disk_p0_x - (float)x;
const float b = disk_p0_y - (float)y;
const float dxy_p0 = std::sqrt( a*a + b*b );
if( dxy_p0 <= (float)radius - aa_seam ) {
print_mark( 1, get_char( 0 ) );
} else if( dxy_p0 <= (float)radius ) {
// dxy_p0 = ] ( r - aa_seam ) .. r ]
const float d_r = ( (float)radius - dxy_p0 ) / aa_seam ; // d_r ~ 1/brightness
print_mark( 1, get_char( 1.0f - d_r ) );
} else {
print_space( 1 );
}
}
print_newline();
}
}
/**
* disk_2 variant using diameter instead of radius
*
* @param d disc diameter size argument
* @param dx distance on x-axis, aka the x-position of object's left edge
*/
void disk_2b(const int d, const int dx) {
disk_2(d/2, dx+1);
}
/**
* Function-pointer to draw an object with given given arguments `sz` and `dx`.
* @param sz object's size argument. Semantic depends on the actual object defintion.
* @param dx distance on x-axis, aka the x-position of object's left edge
*/
typedef void(*paint_func)(const int sz, const int dx);
/**
* Paint something using the given paint_func
* @param sz object's size argument. Semantic depends on the actual object defintion.
* @param dx distance on x-axis, aka the x-position of object's left edge
* @param pf function pointer to draw an object with given arguments `sz` and `dx`.
*/
void paint(const int sz, const int dx, paint_func pf) {
pf(sz, dx);
}
int main(int argc, const char* argv[]) {
{
// loop through all program invocation arguments and print them
for(int i=0; i<argc; ++i) {
printf("cmd_arg[%d]: %s\n", i, argv[i]);
}
}
printf("\nPyramids base_len 4\n");
pyramid_up(4);
pyramid_down(4);
salino(4);
printf("\nPyramids base_len 5\n");
pyramid_up(5);
pyramid_down(5);
salino(5);
printf("\nDisks radius 1, scale-x 1\n");
disk_1(1, 1);
disk_1(2, 1);
disk_2(1, 1);
{
const int max_size = 5;
printf("\nDisks radius [1..5], sx 1.0\n");
for(int i=1; i<=max_size; ++i) {
disk_1(i, max_size-i);
disk_2(i, max_size-i);
}
}
{
/**
* This block paints all objects in all sizes using
* an array of object agnostic `paint_func` function-pointer
* to apply the paint functions for all object types.
*
* This allows polymorphic rendering of objects,
* i.e. painting different objects by invoking instances
* conforming to the common `paint_func` function-type (callable). <br />
* These `paint_func` instances paint the different objects,
* e.g. `pyramid_up`, `pyramid_down` etc.
*/
const int min_size = 3;
const int max_size = 10;
printf("\nAlternate object with size [%d..%d]\n", min_size, max_size);
/*
* Pyramid_up non-capturing lambda function pointer.
*
* Assignment to a variable of type `paint_func` is only possible here,
* since the lambda type is non-capturing.
*/
paint_func p1 = [](const int sz, const int dx) { pyramid_up(sz, dx); };
/* Array of paint_func function pointer */
paint_func paint_funcs[] = {
/* Assigning `square` function-pointer for index 0.*/
square,
/* Assigning pyramid_up non-capturing lambda function-pointer for index 1.*/
p1,
/* Create and assigning pyramid_down non-capturing lambda function-pointer for index 2. */
[](const int sz, const int dx) { pyramid_down(sz, dx); },
/* Assigning `disk_2b` function-pointer for index 3 */
disk_2b
};
// for all sizes
for(int i=min_size; i<=max_size; i++) {
// for all object types using range-loop
for(const paint_func& f : paint_funcs) {
paint(i, max_size-i, f);
}
// A traditional loop would look like:
//
// for(size_t t=0; t<sizeof(paint_funcs)/sizeof(paint_func); ++t) {
// paint(i, max_size-i, paint_funcs[t]);
//}
}
}
return 0;
}
|