C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
Functions
exprtk_julia_set_fractal.cpp File Reference
#include <chrono>
#include <cmath>
#include <cstdio>
#include <string>
#include <thread>
#include "exprtk.hpp"
Include dependency graph for exprtk_julia_set_fractal.cpp:

Go to the source code of this file.

Functions

template<typename T >
void julia_set_fractal ()
 
int main ()
 

Function Documentation

◆ julia_set_fractal()

template<typename T >
void julia_set_fractal ( )

Definition at line 30 of file exprtk_julia_set_fractal.cpp.

31{
32 typedef exprtk::symbol_table<T> symbol_table_t;
33 typedef exprtk::expression<T> expression_t;
34 typedef exprtk::parser<T> parser_t;
35 typedef exprtk::function_compositor<T> compositor_t;
36 typedef typename compositor_t::function function_t;
37
38 const T width = T(120);
39 const T height = T( 42);
40
42
43 symbol_table_t symbol_table;
44
45 symbol_table.add_constant("width" , width );
46 symbol_table.add_constant("height" , height );
47 symbol_table.add_function("println", println);
48
49 symbol_table.
50 add_function("sleep",
51 [](T time_ms) -> T
52 {
53 std::this_thread::sleep_for(
54 std::chrono::milliseconds(static_cast<std::size_t>(time_ms)));
55 return T(1);
56 });
57
58 symbol_table.
59 add_function("clear",
60 [](T full) -> T
61 {
62 printf("%s\033[H", full == T(1) ? "" : "\033[2J");
63 std::fflush(stdout);
64 return T(1);
65 });
66
67 symbol_table.
68 add_function("log_details",
69 [](T frame, T c_real, T c_imag) -> T
70 {
71 printf("ExprTk Julia Set Fractal Animation (Frame: %04lu - c = %6.3f + %6.3fi)\n",
72 static_cast<std::size_t>(frame + 1),
73 c_real,
74 c_imag);
75 return T(1);
76 });
77
78 symbol_table.
79 add_function("putch",
80 [](T v) -> T
81 {
82 static const char palette[] = "........,'`^\":;-_~=+<>!?|\\/()[]"
83 "{}1iIljtfxrnuvczmwqpdbkhaoGXYUJCL"
84 "Q0OZ*#MW&8%@$";
85 static const int palette_size = sizeof(palette) / sizeof(char) - 1;
86 int index = static_cast<int>(v);
87 if (index < 0) index = 0;
88 if (index >= palette_size) index = palette_size - 1;
89 printf("%c", palette[index]);
90 return T(1);
91 });
92
93 compositor_t compositor(symbol_table);
94
95 compositor.load_variables(true);
96 compositor.load_vectors (true);
97
98 compositor.add(
99 function_t("julia_set_fractal")
100 .vars("c_real", "c_imag")
101 .expression
102 (
103 " var imag_max := +1.5; "
104 " var imag_min := -1.5; "
105 " var real_max := +1.5; "
106 " var real_min := -1.5; "
107 " var x_step := (real_max - real_min) / width; "
108 " var y_step := (imag_max - imag_min) / height; "
109 " "
110 " for (var y := 0; y < height; y += 1) "
111 " { "
112 " var imag := imag_min + (y_step * y); "
113 " "
114 " for (var x := 0; x < width; x += 1) "
115 " { "
116 " var real := real_min + x_step * x; "
117 " var z_real := real; "
118 " var z_imag := imag; "
119 " var plot_value := 0; "
120 " "
121 " for (var n := 0; n < 50; n += 1) "
122 " { "
123 " var a := z_real^2; "
124 " var b := z_imag^2; "
125 " plot_value := n; "
126 " "
127 " if ((a + b) < 4) "
128 " { "
129 " z_imag := 2 * z_real * z_imag + c_imag; "
130 " z_real := a - b + c_real; "
131 " } "
132 " else "
133 " break; "
134 " }; "
135 " "
136 " putch(plot_value); "
137 " }; "
138 " "
139 " println() "
140 " } "
141 ));
142
143 const std::string julia_set_fractal_driver =
144 " var t := 0; "
145 " for (var frame := 0; frame < 1000; frame += 1) "
146 " { "
147 " var radius := 0.4 + 0.1 * sin(t / 5); "
148 " var c_real := -0.74543 + radius * cos(t); "
149 " var c_imag := 0.27015 + radius * sin(t); "
150 " t += 0.02; "
151 " "
152 " clear(frame % 100 == 0); "
153 " log_details(frame, c_real, c_imag); "
154 " julia_set_fractal(c_real, c_imag); "
155 " sleep(10); "
156 " }; ";
157
158 expression_t expression;
159 expression.register_symbol_table(symbol_table);
160
161 parser_t parser;
162 parser.compile(julia_set_fractal_driver,expression);
163
164 expression.value();
165}

◆ main()

int main ( )

Definition at line 167 of file exprtk_julia_set_fractal.cpp.

168{
169 julia_set_fractal<double>();
170 return 0;
171}