-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain.cpp
More file actions
89 lines (70 loc) · 2.48 KB
/
main.cpp
File metadata and controls
89 lines (70 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <cxxplot/cxxplot>
#include <cmath>
#include <filesystem>
#include <numbers>
#include <ranges>
#include <sstream>
namespace plt = cxxplot;
template< class F >
auto lazy_func_data( F&& f, const double x0, const double xn, const std::size_t num_points = 100 )
{
using namespace std::ranges;
double scale = ( xn - x0 ) / ( num_points - 1 );
// clang-format off
return
iota_view{ std::size_t( 0 ), num_points } |
std::ranges::views::transform( [f, x0, scale]( const auto &i ) { return f( x0 + scale*i ); } );
// clang-format on
}
int main( int argc, char* argv[] )
{
return plt::exec( argc, argv, [ & ]( ) {
using namespace plt::named_parameters;
using namespace std::ranges;
using namespace std::numbers;
using namespace std::chrono;
// clang-format off
auto x = []( const double &t, const double &a ) { return a/20.0*cos(t) + sin(2.0*t)*cos(a*t); };
auto y = []( const double &t, const double &a ) { return 2.0*sin((60.0-a)/12.5*t) + 0.5*sin(a*t); };
const int n = 2000;
auto fx =[x]( const double &a ) {
return lazy_func_data( [a,x]( auto t ) { return x( t, a ); } , 0, 2.0*pi, n );
};
auto fy =[y]( const double &a )
{
return lazy_func_data( [a,y]( auto t ) { return y( t, a ); } , 0, 2.0*pi, n );
};
const auto a = 35.0;
auto w = plt::plot( fx( a ), fy( a ),
window_title_ = "cxxplot animation example",
line_width_ = 1.0,
xlim_ = {-5.0, 5.0},
ylim_ = {-3.0, 3.0},
line_color_ = plt::color::cxxplot::green,
axes_aspect_ratio_ = 1.0 // TODO: There is an issue if we provide an int here
);
auto &f = w.figure(0);
auto &g = f.graph(0);
std::size_t frames = 0;
double duration = 0;
for (auto i = 1 ; i != 10000; i++)
{
auto start = steady_clock::now();
g.set_data( fx( a + i*0.005 ), fy( a + i*0.005 ) );
auto finish = steady_clock::now();
auto t = double(duration_cast<microseconds>( finish - start ).count()) / 1.0e6;
duration +=t;
frames++;
if ( duration > 0.5 )
{
std::stringstream ss;
ss << std::round( 1.0 / duration * frames ) << " updates per sec (including dropped)";
w.set_title( ss.str() );
duration = 0;
frames = 0;
}
}
// clang-format on
return 0;
} );
}