My omnium-gatherom of scripts and source code.
1/* ========================================================================
2 *
3 * Filename: main.cpp
4 * Description: simple c++ renderer
5 * Author: Diego A. Estrada Rivera
6 * Version: 0.0.1
7 *
8 * ======================================================================== */
9#include <format>
10#include <tuple>
11#include <iostream>
12#include "utils.hpp"
13
14fn main() -> int
15{
16 static var c = Canvas<1000, 1000>(Point(0.0, 0.0), 3.0);
17
18 static let histogram = [] {
19 var ret = std::array<Color, 255>();
20 let begin = Color(182, 0.00, 0.0);
21 let end = Color(1.0, 1.00, 1.0);
22 var t = 1;
23 for (var &c : ret)
24 c = begin.lerp(end, (double)t / 255.0), ++t;
25
26 return ret;
27 }();
28
29
30 [[maybe_unused]] let mandelbrot = [](Point coordinate) {
31 let max_iteration = 20000ul;
32 var x = 0.0L;
33 var y = 0.0L;
34 var x_squared = 0.0L;
35 var y_squared = 0.0L;
36 var iteration = 0u;
37
38 while (std::islessequal((x_squared + y_squared), 4) &&
39 (iteration < max_iteration)) {
40 y = fma(2 * x, y, coordinate.y);
41 x = x_squared - y_squared + coordinate.x;
42 x_squared = x * x;
43 y_squared = y * y;
44 iteration++;
45 }
46
47 return Color(iteration);
48 };
49
50 [[maybe_unused]] let julia = [](Point z) {
51 let R = 4.0;
52 var iteration = 0u;
53 let max_iteration = 1000u;
54 let c = Point(-0.6, 0.8);
55
56
57 while (z.x * z.x + z.y * z.y < R * R && iteration < max_iteration) {
58 auto xtemp = z.x * z.x - z.y * z.y;
59 z.y = 2 * z.x * z.y + c.y;
60 z.x = xtemp + c.x;
61 ++iteration;
62 }
63
64 return histogram[iteration & 0xFF];
65 };
66
67 [[maybe_unused]] let newton = [](Point z) {
68 let f = [](Point z){
69 return Point(z.x * z.x * z.x, - 1);
70 };
71
72 let dx = [](Point z){
73 (void)z;
74
75 };
76
77 (void)z;
78 (void)f;
79 (void)dx;
80
81 return Color(0.0, 0.0, 0.0);
82 };
83
84 ignore = c.construct(mandelbrot);
85 if (not c.save_to_ppm("tmp.ppm"))
86 std::cout << "Could not output to tmp.ppm\n";
87
88 return 0;
89}
90