blob: 64f49067bb23bd5be11354a10b74f3728bbc9cf5 [file] [log] [blame]
Austin Schuhab802d52020-07-03 18:11:11 -07001#define _USE_MATH_DEFINES
2#include <cmath>
3#include "../matplotlibcpp.h"
4#include <chrono>
5
6namespace plt = matplotlibcpp;
7
8void update_window(const double x, const double y, const double t,
9 std::vector<double> &xt, std::vector<double> &yt)
10{
11 const double target_length = 300;
12 const double half_win = (target_length/(2.*sqrt(1.+t*t)));
13
14 xt[0] = x - half_win;
15 xt[1] = x + half_win;
16 yt[0] = y - half_win*t;
17 yt[1] = y + half_win*t;
18}
19
20
21int main()
22{
23 size_t n = 1000;
24 std::vector<double> x, y;
25
26 const double w = 0.05;
27 const double a = n/2;
28
29 for (size_t i=0; i<n; i++) {
30 x.push_back(i);
31 y.push_back(a*sin(w*i));
32 }
33
34 std::vector<double> xt(2), yt(2);
35
36 plt::title("Tangent of a sine curve");
37 plt::xlim(x.front(), x.back());
38 plt::ylim(-a, a);
39 plt::axis("equal");
40
41 // Plot sin once and for all.
42 plt::named_plot("sin", x, y);
43
44 // Prepare plotting the tangent.
45 plt::Plot plot("tangent");
46
47 plt::legend();
48
49 for (size_t i=0; i<n; i++) {
50 if (i % 10 == 0) {
51 update_window(x[i], y[i], a*w*cos(w*x[i]), xt, yt);
52
53 // Just update data for this plot.
54 plot.update(xt, yt);
55
56 // Small pause so the viewer has a chance to enjoy the animation.
57 plt::pause(0.1);
58 }
59 }
60}