|
1 | 1 | #define _USE_MATH_DEFINES |
2 | | -#include <cmath> |
3 | 2 | #include "../matplotlibcpp.h" |
| 3 | +#include <cmath> |
4 | 4 |
|
5 | 5 | using namespace std; |
6 | 6 | namespace plt = matplotlibcpp; |
7 | 7 |
|
8 | | -int main() |
9 | | -{ |
10 | | - // plot(y) - the x-coordinates are implicitly set to [0,1,...,n) |
11 | | - //plt::plot({1,2,3,4}); |
12 | | - |
13 | | - // Prepare data for parametric plot. |
14 | | - int n = 5000; // number of data points |
15 | | - vector<double> x(n),y(n); |
16 | | - for(int i=0; i<n; ++i) { |
17 | | - double t = 2*M_PI*i/n; |
18 | | - x.at(i) = 16*sin(t)*sin(t)*sin(t); |
19 | | - y.at(i) = 13*cos(t) - 5*cos(2*t) - 2*cos(3*t) - cos(4*t); |
20 | | - } |
| 8 | +int main() { |
| 9 | + // plot(y) - the x-coordinates are implicitly set to [0,1,...,n) |
| 10 | + // plt::plot({1,2,3,4}); |
21 | 11 |
|
22 | | - // plot() takes an arbitrary number of (x,y,format)-triples. |
23 | | - // x must be iterable (that is, anything providing begin(x) and end(x)), |
24 | | - // y must either be callable (providing operator() const) or iterable. |
25 | | - plt::plot(x, y, "r-", x, [](double d) { return 12.5+abs(sin(d)); }, "k-"); |
| 12 | + // Prepare data for parametric plot. |
| 13 | + int n = 5000; // number of data points |
| 14 | + vector<double> x(n), y(n), z(n); |
| 15 | + for (int i = 0; i < n; ++i) { |
| 16 | + double t = 2 * M_PI * i / n; |
| 17 | + x.at(i) = 16 * sin(t) * sin(t) * sin(t); |
| 18 | + y.at(i) = 13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t); |
| 19 | + z.at(i) = 12.5 + abs(sin(x.at(i))); |
| 20 | + } |
26 | 21 |
|
| 22 | + // plot() takes an arbitrary number of (x,y,format)-triples. |
| 23 | + // x must be iterable (that is, anything providing begin(x) and end(x)), |
| 24 | + // y must either be callable (providing operator() const) or iterable. |
| 25 | + plt::plot(x, z, "k-", x, y, "r-"); |
27 | 26 |
|
28 | | - // show plots |
29 | | - plt::show(); |
| 27 | + // show plots |
| 28 | + plt::show(); |
30 | 29 | } |
0 commit comments