Skip to content

Instantly share code, notes, and snippets.

@jniemann66
Created December 21, 2019 12:43
Show Gist options
  • Save jniemann66/af1e29dd7ea93856e66d1a60f826dc5a to your computer and use it in GitHub Desktop.
Save jniemann66/af1e29dd7ea93856e66d1a60f826dc5a to your computer and use it in GitHub Desktop.
#ifndef UGLYPLOT_H
#define UGLYPLOT_H
#include <iostream>
#include <cmath>
class UglyPlot
{
public:
static void plot(const double* p, int length)
{
constexpr int cols = 80;
constexpr int center = cols / 2;
double peak = 0.0;
for(int n = 0; n < length; n++) {
peak = std::max(std::abs(p[n]), peak);
}
double scale = center / peak;
for(int r = 0; r < length; r++) {
int a = std::round(center + scale * p[r]);
int b = cols - a;
std::string s(a, ' ');
std::string t(b, ' ');
std::cout << s << "*" << t << p[r] << "\n";
}
std::cout << std::endl;
}
};
#endif // UGLYPLOT_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment