C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_radial_contour.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * ExprTk Radial Contour Graphing 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 <chrono>
21#include <cmath>
22#include <cstdio>
23#include <string>
24#include <thread>
25
26#include "exprtk.hpp"
27
28
29template <typename T>
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
39 exprtk::rtl::io::print <T> print;
40
41 symbol_table_t symbol_table;
42
43 symbol_table.add_function("println", println);
44 symbol_table.add_function("print" , print );
45
46 symbol_table.
47 add_function("sleep",
48 [](T time_ms) -> T
49 {
50 std::this_thread::sleep_for(
51 std::chrono::milliseconds(static_cast<std::size_t>(time_ms)));
52 return T(1);
53 });
54
55 symbol_table.
56 add_function("clear",
57 [](T full) -> T
58 {
59 printf("%s\033[H", full == T(1) ? "" : "\033[2J");
60 std::fflush(stdout);
61 return T(1);
62 });
63
64 compositor_t compositor(symbol_table);
65
66 compositor.load_variables(true);
67 compositor.load_vectors (true);
68
69 compositor.add(
70 function_t("draw_contour")
71 .var("t")
72 .expression
73 (
74 " var min_x := -30; "
75 " var min_y := -20; "
76 " var max_x := +30; "
77 " var max_y := +20; "
78 " var x_incr := +1.0 / 1.7; "
79 " var y_incr := +1; "
80 " var c := 0.9 sin(t); "
81 " "
82 " for (var y := min_y; y < max_y; y += y_incr) "
83 " { "
84 " for (var x := min_x; x < max_x; x += x_incr) "
85 " { "
86 " if (sin((x^2 + y^2)^(1/2)) <= c) "
87 " print('*'); "
88 " else "
89 " print(' '); "
90 " }; "
91 " "
92 " println(); "
93 " } "
94 " "
95 ));
96
97 const std::string radial_contour_graphing_driver =
98 " var t := 0; "
99 " for (var frame := 0; frame < 1000; frame += 1) "
100 " { "
101 " t += 0.08; "
102 " "
103 " clear(frame % 100 == 0); "
104 " draw_contour(t); "
105 " sleep(40); "
106 " }; ";
107
108 expression_t expression;
109 expression.register_symbol_table(symbol_table);
110
111 parser_t parser;
112 parser.compile(radial_contour_graphing_driver,expression);
113
114 expression.value();
115}
116
117int main()
118{
119 radial_contour_graphing_example<double>();
120 return 0;
121}
void radial_contour_graphing_example()