This header provides functionality for producing simple graphics on the terminal.
NOTE: the graphics are produced using the sixel protocol, which is not supported by many terminals. At the time of writing, the following terminals are known to have the necessary capabilities:
To use in your code, place this file alongside your own code, and #include the file where necessary:
At this point, you can make use of the functions. All functions are enclosed in the TG namespace
By default, the colours are set up for use on a terminal with a dark background. When using a light background, set the WHITEBG
environment variable. For example, in bash
:
#include <random>
#include <cmath>
#include <vector>
#include <stdexcept>
#include <iostream>
#include <fstream>
#include <format>
#include "terminal_graphics.h"
#include "load_pgm.h"
int main (int argc, char* argv[])
{
try {
const std::string image_filename = "brain.pgm";
const auto image = load_pgm (image_filename);
std::cout << std::format ("Showing image \"{}\", size: {} x {}\n", image_filename, image.width(), image.height());
std::cout << "Same image magnified by a factor of 2, with transparency:\n";
std::vector<float> x (50);
std::vector<float> y (50);
for (unsigned int x = 0; x < y.size(); ++x)
y[x] = exp (-0.1*x) - 1.5*exp (-0.4*x);
std::cout << "A simple one-line plot:\n";
for (std::size_t n = 0; n < x.size();++n) {
y[n] = std::sin (0.2*n) + 0.3*std::cos (0.33*n);
x[n] = 20.0+10.0*std::cos (0.41*n) + 5.0*std::sin(0.21*n);
}
std::cout << "Plotting arbitrary lines, without transparency:\n";
.
add_text (
"sinusoids", (x.size()-1)/2.0, 1.2, 0.5, 0.0, 6);
std::random_device rd;
std::mt19937 gen (rd());
std::normal_distribution normal (5.0, 2.0);
std::vector<float> noise (256);
for (auto& x : noise)
x = normal (gen);
std::cout << "Plotting Normally distributed random variables:\n";
}
catch (std::exception& e) {
std::cerr << "error: " << e.what() << std::endl;
return 1;
}
return 0;
}
Plot & set_ylim(float min, float max, float expand_by=0.0)
set the range along the y-axis
Definition terminal_graphics.h:895
Plot & add_line(float x0, float y0, float x1, float y1, int colour_index=2, int stiple=0, float stiple_frac=0.5)
add a single line connection point (x0,y0) to (x1,y1).
Definition terminal_graphics.h:916
Plot & add_text(const std::string &text, float x, float y, float anchor_x=0.5, float anchor_y=0.5, int colour_index=1)
add text at the location specified
Definition terminal_graphics.h:990
Plot & disable_transparency()
disable transparent background for plot
Definition terminal_graphics.h:875
Plot & set_grid(float x_interval, float y_interval)
set the interval of the gridlines, centered around zero
Definition terminal_graphics.h:909
Adapter class to magnify an image.
Definition terminal_graphics.h:203
void imshow(const ImageType &image, const ColourMap &cmap, const bool zero_is_transparent=false)
Display an indexed image to the terminal, according to the colourmap supplied.
Definition terminal_graphics.h:727
Plot plot(int width=512, int height=256)
Convenience function to use for immediate rendering.
Definition terminal_graphics.h:461