C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_mandelbrot.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * ExprTk Mandelbrot Fractal Generator Example *
6 * Author: Arash Partow (1999-2024) *
7 * URL: https://www.partow.net/programming/exprtk/index.html *
8 * *
9 * Copyright notice: *
10 * Free use of the Mathematical Expression Toolkit Library is *
11 * permitted under the guidelines and in accordance with the *
12 * most current version of the MIT License. *
13 * https://www.opensource.org/licenses/MIT *
14 * SPDX-License-Identifier: MIT *
15 * *
16 **************************************************************
17*/
18
19
20#include <cstdio>
21#include <string>
22
23#include "exprtk.hpp"
24
25
26template <typename T>
27inline T putch(T v)
28{
29 printf("%c",static_cast<int>(v));
30 return T(0);
31}
32
33template <typename T>
35{
36 typedef exprtk::symbol_table<T> symbol_table_t;
37 typedef exprtk::expression<T> expression_t;
38 typedef exprtk::parser<T> parser_t;
39
40 const std::string mandelbrot_program =
41 " width := 118; "
42 " height := 41; "
43 " imag_max := +1; "
44 " imag_min := -1; "
45 " real_max := +1; "
46 " real_min := -2.5; "
47 " x_step := (real_max - real_min) / width; "
48 " y_step := (imag_max - imag_min) / height; "
49 " "
50 " for (var y := 0; y < height; y += 1) "
51 " { "
52 " imag := imag_min + (y_step * y); "
53 " "
54 " for (var x := 0; x < width; x += 1) "
55 " { "
56 " real := real_min + x_step * x; "
57 " z_real := real; "
58 " z_imag := imag; "
59 " "
60 " for (var n := 0; n < 30; n += 1) "
61 " { "
62 " a := z_real^2; "
63 " b := z_imag^2; "
64 " plot_value := n; "
65 " "
66 " if ((a + b) < 4) "
67 " { "
68 " z_imag := 2 * z_real * z_imag + imag; "
69 " z_real := a - b + real; "
70 " } "
71 " else "
72 " break; "
73 " }; "
74 " "
75 " putch(61 - plot_value); "
76 " }; "
77 " "
78 " println() "
79 " } ";
80
82
83 symbol_table_t symbol_table;
84 symbol_table.add_function("putch" , putch );
85 symbol_table.add_function("println", println);
86
87 expression_t expression;
88 expression.register_symbol_table(symbol_table);
89
90 parser_t parser;
91 parser.enable_unknown_symbol_resolver();
92 parser.compile(mandelbrot_program,expression);
93
94 expression.value();
95}
96
97int main()
98{
99 mandelbrot<double>();
100 return 0;
101}
void mandelbrot()
int main()