modern.cpp 864 B

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