Gobelijn API documentation  - generated for commit a0cbea7
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
plotter.cpp
Go to the documentation of this file.
1 #include "plotter.h"
2 
3 #include <cmath>
4 #include <limits>
5 #include <string>
6 #include <vector>
7 
8 using std::numeric_limits;
9 
10 template <typename T>
11 std::string Plotter<T>::plot(const std::function<T(T)>& f, T from, T to, unsigned int n) const
12 {
13  std::vector<std::pair<T, T>> lines;
14  T delta = (to - from) / n;
15  for (unsigned int i = 0; i < n; ++i) {
16  T x = from + static_cast<T>(i) * delta;
17  lines.push_back(std::make_pair(x, f(x)));
18  }
19 
20  T mininf = -numeric_limits<double>::infinity();
21  T maxinf = numeric_limits<double>::infinity();
22  T maxx = mininf, maxy = mininf, minx = maxinf, miny = maxinf;
23  for (auto& p : lines) {
24  if (p.first < minx)
25  minx = p.first;
26  if (p.first > maxx)
27  maxx = p.first;
28  if (p.second < miny)
29  miny = p.second;
30  if (p.second > maxy)
31  maxy = p.second;
32  }
33  T size = 1000;
34  T maxrange = (maxx - minx) < (maxy - miny) ? (maxy - miny) : (maxx - minx);
35  T imagex = size * (maxx - minx) / maxrange;
36  T imagey = size * (maxy - miny) / maxrange;
37  T scale = 0.95 * (imagex / (maxx - minx));
38  T DCx = scale * ((minx + maxx) / 2);
39  T DCy = scale * ((miny + maxy) / 2);
40  T dx = (imagex / 2) - DCx;
41  T dy = (imagey / 2) - DCy;
42 
43  std::string result;
44  for (size_t i = 0; i < lines.size(); i++) {
45  result += "(" + std::to_string(lines[i].first * scale + dx) + "," +
46  std::to_string(lines[i].second * scale + dy) + ")\n";
47  }
48  return result;
49 }
50 
51 template <typename T>
52 std::string Plotter<T>::operator()(const std::function<T(T)>& f, T from, T to, unsigned int n) const
53 {
54  return plot(f, from, to, n);
55 }
56 
57 template class Plotter<double>;
Definition: plotter.h:6
std::string operator()(const std::function< T(T)> &f, T from=0.0, T to=1.0, unsigned int n=10) const
Definition: plotter.cpp:52
std::string plot(const std::function< T(T)> &f, T from=0.0, T to=1.0, unsigned int n=10) const
Definition: plotter.cpp:11