C++ Mathematical Expression Toolkit (ExprTk)
release
Toggle main menu visibility
Loading...
Searching...
No Matches
exprtk
exprtk_chladni_contour.cpp
Go to the documentation of this file.
1
/*
2
**************************************************************
3
* C++ Mathematical Expression Toolkit Library *
4
* *
5
* ExprTk Chladni Contour Graphing Example *
6
* Author: Arash Partow (1999-2025) *
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
29
template
<
typename
T>
30
void
chladni_contour_graphing_example
()
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
exprtk::rtl::io::println<T>
println;
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_chladni_contour"
)
71
.var(
"t"
)
72
.expression
73
(
74
" var min_x := -40; "
75
" var min_y := -30; "
76
" var max_x := +40; "
77
" var max_y := +30; "
78
" var x_incr := +1.0 / 1.7; "
79
" var y_incr := +1; "
80
" var a := 10.0 sin(t); "
81
" var b := 10.0 sin(t + 1.0); "
82
" var c := 0.9 sin(t); "
83
" "
84
" for (var y := min_y; y < max_y; y += y_incr) "
85
" { "
86
" for (var x := min_x; x < max_x; x += x_incr) "
87
" { "
88
" if (sin(x / 3 - a) + sin(y / 5 - b) <= c) "
89
" print('*'); "
90
" else "
91
" print(' '); "
92
" }; "
93
" "
94
" println(); "
95
" } "
96
" "
97
));
98
99
const
std::string chladni_contour_graphing_driver =
100
" var t := 0; "
101
" for (var frame := 0; frame < 1000; frame += 1) "
102
" { "
103
" t += 0.03; "
104
" "
105
" clear(frame % 100 == 0); "
106
" draw_chladni_contour(t); "
107
" sleep(40); "
108
" }; "
;
109
110
expression_t expression;
111
expression.register_symbol_table(symbol_table);
112
113
parser_t parser;
114
parser.compile(chladni_contour_graphing_driver,expression);
115
116
expression.value();
117
}
118
119
int
main
()
120
{
121
chladni_contour_graphing_example<double>
();
122
return
0;
123
}
exprtk::expression
Definition
exprtk.hpp:21832
exprtk::function_compositor
Definition
exprtk.hpp:43057
exprtk::parser
Definition
exprtk.hpp:22525
exprtk::symbol_table
Definition
exprtk.hpp:20090
exprtk.hpp
main
int main()
Definition
exprtk_chladni_contour.cpp:119
chladni_contour_graphing_example
void chladni_contour_graphing_example()
Definition
exprtk_chladni_contour.cpp:30
exprtk::rtl::io::println
Definition
exprtk.hpp:44273
Generated by
1.17.0