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