2019-01-04 17:29:55 +08:00
|
|
|
|
// Teal Dulcet, CS546
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
#include <cmath>
|
|
|
|
|
#include <cfloat>
|
|
|
|
|
#include <iomanip>
|
|
|
|
|
#include <cwchar>
|
|
|
|
|
#include <clocale>
|
2019-01-04 17:50:15 +08:00
|
|
|
|
#include <cstdlib>
|
2022-07-14 16:33:38 +08:00
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <array>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <iterator>
|
|
|
|
|
#include <numeric>
|
2022-07-14 17:18:58 +08:00
|
|
|
|
#include <functional>
|
2023-05-11 22:03:53 +08:00
|
|
|
|
#include <chrono>
|
2019-01-04 17:29:55 +08:00
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
namespace graphs
|
|
|
|
|
{
|
|
|
|
|
using namespace std;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
enum style_type
|
|
|
|
|
{
|
|
|
|
|
style_ASCII,
|
|
|
|
|
style_basic,
|
|
|
|
|
style_light,
|
|
|
|
|
style_heavy,
|
|
|
|
|
style_double,
|
2023-08-30 20:57:03 +08:00
|
|
|
|
style_arc,
|
2023-03-10 21:15:01 +08:00
|
|
|
|
style_light_dashed,
|
|
|
|
|
style_heavy_dashed
|
|
|
|
|
};
|
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
|
enum style_type const style_types[] = {style_ASCII, style_basic, style_light, style_heavy, style_double, style_arc, style_light_dashed, style_heavy_dashed};
|
2023-03-10 21:15:01 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
const char *const styles[][11] = {
|
|
|
|
|
{"-", "|", "+", "+", "+", "+", "+", "+", "+", "+", "+"}, // ASCII
|
|
|
|
|
{"—", "|", "+", "+", "+", "+", "+", "+", "+", "+", "+"}, // Basic
|
|
|
|
|
{"─", "│", "┌", "┬", "┐", "├", "┼", "┤", "└", "┴", "┘"}, // Light
|
|
|
|
|
{"━", "┃", "┏", "┳", "┓", "┣", "╋", "┫", "┗", "┻", "┛"}, // Heavy
|
|
|
|
|
{"═", "║", "╔", "╦", "╗", "╠", "╬", "╣", "╚", "╩", "╝"}, // Double
|
2023-08-30 20:57:03 +08:00
|
|
|
|
{"─", "│", "╭", "┬", "╮", "├", "┼", "┤", "╰", "┴", "╯"}, // Light Arc
|
2023-03-13 22:29:43 +08:00
|
|
|
|
{"╌", "┊", "┌", "┬", "┐", "├", "┼", "┤", "└", "┴", "┘"}, // Light Dashed
|
|
|
|
|
{"╍", "┋", "┏", "┳", "┓", "┣", "╋", "┫", "┗", "┻", "┛"} // Heavy Dashed
|
|
|
|
|
// {" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "} // No border
|
2022-07-14 16:44:17 +08:00
|
|
|
|
};
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
enum color_type
|
|
|
|
|
{
|
|
|
|
|
color_default,
|
|
|
|
|
color_black,
|
|
|
|
|
color_red,
|
|
|
|
|
color_green,
|
|
|
|
|
color_yellow,
|
|
|
|
|
color_blue,
|
2023-03-13 22:29:43 +08:00
|
|
|
|
color_magenta,
|
2023-03-10 21:15:01 +08:00
|
|
|
|
color_cyan,
|
2024-11-12 21:03:10 +08:00
|
|
|
|
color_white,
|
|
|
|
|
color_gray,
|
|
|
|
|
color_bright_red,
|
|
|
|
|
color_bright_green,
|
|
|
|
|
color_bright_yellow,
|
|
|
|
|
color_bright_blue,
|
|
|
|
|
color_bright_magenta,
|
|
|
|
|
color_bright_cyan,
|
|
|
|
|
color_bright_white
|
2023-03-10 21:15:01 +08:00
|
|
|
|
};
|
|
|
|
|
|
2024-11-12 21:03:10 +08:00
|
|
|
|
enum color_type const color_types[] = {color_default, color_black, color_red, color_green, color_yellow, color_blue, color_magenta, color_cyan, color_white, color_gray, color_bright_red, color_bright_green, color_bright_yellow, color_bright_blue, color_bright_magenta, color_bright_cyan, color_bright_white};
|
2023-03-10 21:15:01 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
const char *const colors[] = {"\e[39m", "\e[30m", "\e[31m", "\e[32m", "\e[33m", "\e[34m", "\e[35m", "\e[36m", "\e[37m", "\e[90m", "\e[91m", "\e[92m", "\e[93m", "\e[94m", "\e[95m", "\e[96m", "\e[97m"};
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
const char *const dots[] = {"⠀", "⠁", "⠂", "⠃", "⠄", "⠅", "⠆", "⠇", "⠈", "⠉", "⠊", "⠋", "⠌", "⠍", "⠎", "⠏", "⠐", "⠑", "⠒", "⠓", "⠔", "⠕", "⠖", "⠗", "⠘", "⠙", "⠚", "⠛", "⠜", "⠝", "⠞", "⠟", "⠠", "⠡", "⠢", "⠣", "⠤", "⠥", "⠦", "⠧", "⠨", "⠩", "⠪", "⠫", "⠬", "⠭", "⠮", "⠯", "⠰", "⠱", "⠲", "⠳", "⠴", "⠵", "⠶", "⠷", "⠸", "⠹", "⠺", "⠻", "⠼", "⠽", "⠾", "⠿", "⡀", "⡁", "⡂", "⡃", "⡄", "⡅", "⡆", "⡇", "⡈", "⡉", "⡊", "⡋", "⡌", "⡍", "⡎", "⡏", "⡐", "⡑", "⡒", "⡓", "⡔", "⡕", "⡖", "⡗", "⡘", "⡙", "⡚", "⡛", "⡜", "⡝", "⡞", "⡟", "⡠", "⡡", "⡢", "⡣", "⡤", "⡥", "⡦", "⡧", "⡨", "⡩", "⡪", "⡫", "⡬", "⡭", "⡮", "⡯", "⡰", "⡱", "⡲", "⡳", "⡴", "⡵", "⡶", "⡷", "⡸", "⡹", "⡺", "⡻", "⡼", "⡽", "⡾", "⡿", "⢀", "⢁", "⢂", "⢃", "⢄", "⢅", "⢆", "⢇", "⢈", "⢉", "⢊", "⢋", "⢌", "⢍", "⢎", "⢏", "⢐", "⢑", "⢒", "⢓", "⢔", "⢕", "⢖", "⢗", "⢘", "⢙", "⢚", "⢛", "⢜", "⢝", "⢞", "⢟", "⢠", "⢡", "⢢", "⢣", "⢤", "⢥", "⢦", "⢧", "⢨", "⢩", "⢪", "⢫", "⢬", "⢭", "⢮", "⢯", "⢰", "⢱", "⢲", "⢳", "⢴", "⢵", "⢶", "⢷", "⢸", "⢹", "⢺", "⢻", "⢼", "⢽", "⢾", "⢿", "⣀", "⣁", "⣂", "⣃", "⣄", "⣅", "⣆", "⣇", "⣈", "⣉", "⣊", "⣋", "⣌", "⣍", "⣎", "⣏", "⣐", "⣑", "⣒", "⣓", "⣔", "⣕", "⣖", "⣗", "⣘", "⣙", "⣚", "⣛", "⣜", "⣝", "⣞", "⣟", "⣠", "⣡", "⣢", "⣣", "⣤", "⣥", "⣦", "⣧", "⣨", "⣩", "⣪", "⣫", "⣬", "⣭", "⣮", "⣯", "⣰", "⣱", "⣲", "⣳", "⣴", "⣵", "⣶", "⣷", "⣸", "⣹", "⣺", "⣻", "⣼", "⣽", "⣾", "⣿"};
|
2023-08-30 20:57:03 +08:00
|
|
|
|
const int dotvalues[][4] = {{0x1, 0x2, 0x4, 0x40}, {0x8, 0x10, 0x20, 0x80}};
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
|
const char *const blocks[] = {" ", "▖", "▗", "▄", "▘", "▌", "▚", "▙", "▝", "▞", "▐", "▟", "▀", "▛", "▜", "█"};
|
|
|
|
|
const int blockvalues[][2] = {{4, 1}, {8, 2}};
|
|
|
|
|
|
|
|
|
|
const char *const bars[] = {" ", "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"};
|
|
|
|
|
|
|
|
|
|
enum type_type
|
|
|
|
|
{
|
|
|
|
|
type_braille,
|
|
|
|
|
type_block,
|
|
|
|
|
type_histogram // Set automatically by the histogram() function
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum type_type const type_types[] = {type_braille, type_block /* , type_histogram */};
|
|
|
|
|
|
|
|
|
|
enum plot_type
|
|
|
|
|
{
|
|
|
|
|
plot_scatter,
|
|
|
|
|
plot_line
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum plot_type const plot_types[] = {plot_scatter, plot_line};
|
|
|
|
|
|
|
|
|
|
const short marks[][8][2] = {{{0, 0}}, {{0, 1}, {-1, 0}, {0, 0}, {1, 0}, {0, -1}}, {{-1, 1}, {0, 1}, {1, 1}, {-1, 0}, {1, 0}, {-1, -1}, {0, -1}, {1, -1}}};
|
|
|
|
|
|
|
|
|
|
enum mark_type
|
|
|
|
|
{
|
|
|
|
|
mark_dot,
|
|
|
|
|
mark_plus,
|
|
|
|
|
mark_square
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum mark_type const mark_types[] = {mark_dot, mark_plus, mark_square};
|
|
|
|
|
|
|
|
|
|
enum graph_type
|
|
|
|
|
{
|
|
|
|
|
graph_dot,
|
|
|
|
|
graph_shade_above,
|
|
|
|
|
graph_shade_below
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum graph_type const graph_types[] = {graph_dot, graph_shade_above, graph_shade_below};
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
const char *const fractions[] = {"¼", "½", "¾", "⅐", "⅑", "⅒", "⅓", "⅔", "⅕", "⅖", "⅗", "⅘", "⅙", "⅚", "⅛", "⅜", "⅝", "⅞"};
|
|
|
|
|
const long double fractionvalues[] = {1.0L / 4.0L, 1.0L / 2.0L, 3.0L / 4.0L, 1.0L / 7.0L, 1.0L / 9.0L, 1.0L / 10.0L, 1.0L / 3.0L, 2.0L / 3.0L, 1.0L / 5.0L, 2.0L / 5.0L, 3.0L / 5.0L, 4.0L / 5.0L, 1.0L / 6.0L, 5.0L / 6.0L, 1.0L / 8.0L, 3.0L / 8.0L, 5.0L / 8.0L, 7.0L / 8.0L};
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
const char *const constants[] = {"π", "e"};
|
|
|
|
|
const long double constantvalues[] = {M_PI, M_E};
|
|
|
|
|
|
2023-05-11 22:03:53 +08:00
|
|
|
|
enum units_type
|
|
|
|
|
{
|
|
|
|
|
units_number,
|
|
|
|
|
units_scale_none,
|
|
|
|
|
units_scale_SI,
|
|
|
|
|
units_scale_IEC,
|
|
|
|
|
units_scale_IEC_I,
|
|
|
|
|
units_fracts,
|
|
|
|
|
units_percent,
|
|
|
|
|
units_date,
|
|
|
|
|
units_time,
|
|
|
|
|
units_monetary
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum units_type const units_types[] = {units_number, units_scale_SI, units_scale_IEC, units_scale_IEC_I, units_fracts, units_percent, units_date, units_time, units_monetary};
|
|
|
|
|
|
|
|
|
|
const char *const suffix_power_char[] = {"", "K", "M", "G", "T", "P", "E", "Z", "Y", "R", "Q"};
|
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
const long double max_bit = scalbn(1.0L, LDBL_MANT_DIG - 1);
|
|
|
|
|
const long double MAX = max_bit + (max_bit - 1);
|
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
struct options
|
|
|
|
|
{
|
2023-03-13 22:29:43 +08:00
|
|
|
|
bool border = false;
|
|
|
|
|
bool axis = true;
|
2022-07-14 16:44:17 +08:00
|
|
|
|
bool axislabel = true;
|
2023-03-13 22:29:43 +08:00
|
|
|
|
bool axistick = true;
|
2022-07-14 16:44:17 +08:00
|
|
|
|
bool axisunitslabel = true;
|
2023-05-11 22:03:53 +08:00
|
|
|
|
units_type xunits = units_fracts;
|
|
|
|
|
units_type yunits = units_fracts;
|
2023-08-30 20:57:03 +08:00
|
|
|
|
type_type type = type_braille;
|
|
|
|
|
mark_type mark = mark_dot;
|
2022-07-14 16:44:17 +08:00
|
|
|
|
const char *title = nullptr;
|
2023-03-10 21:15:01 +08:00
|
|
|
|
style_type style = style_light;
|
|
|
|
|
color_type color = color_red;
|
|
|
|
|
bool check = true;
|
2022-07-14 16:44:17 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const options defaultoptions;
|
2023-08-30 20:57:03 +08:00
|
|
|
|
options histogram_defaultoptions;
|
2022-07-14 16:44:17 +08:00
|
|
|
|
|
2023-03-10 18:07:00 +08:00
|
|
|
|
template <typename T>
|
2023-03-10 21:15:01 +08:00
|
|
|
|
constexpr size_t size(const T &array)
|
2023-03-10 18:07:00 +08:00
|
|
|
|
{
|
2024-06-01 23:59:28 +08:00
|
|
|
|
return distance(cbegin(array), cend(array));
|
2023-03-10 18:07:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
// Number of columns needed to represent the string
|
|
|
|
|
// Adapted from: https://stackoverflow.com/a/31124065
|
|
|
|
|
int strcol(const char *const str)
|
|
|
|
|
{
|
|
|
|
|
size_t length = strlen(str);
|
|
|
|
|
for (size_t i = 0; i < length; ++i)
|
|
|
|
|
if (iscntrl(str[i]))
|
|
|
|
|
{
|
|
|
|
|
cerr << "\nError! Control character in string.\n";
|
2023-05-11 22:03:53 +08:00
|
|
|
|
cout << "Control character: " << (int)str[i] << '\n';
|
2022-07-14 16:44:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
length = mbstowcs(nullptr, str, 0);
|
|
|
|
|
if (length == static_cast<size_t>(-1))
|
2019-01-04 17:29:55 +08:00
|
|
|
|
{
|
2022-07-14 16:44:17 +08:00
|
|
|
|
cerr << "\nError! mbstowcs failed. Invalid multibyte character.\n";
|
|
|
|
|
exit(1);
|
2019-01-04 17:29:55 +08:00
|
|
|
|
}
|
2022-07-14 16:44:17 +08:00
|
|
|
|
++length;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
auto *wcstring = new wchar_t[length];
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (mbstowcs(wcstring, str, length) == static_cast<size_t>(-1))
|
|
|
|
|
{
|
2023-03-10 21:15:01 +08:00
|
|
|
|
if (wcstring)
|
2022-07-14 16:44:17 +08:00
|
|
|
|
delete[] wcstring;
|
|
|
|
|
|
|
|
|
|
cerr << "\nError! mbstowcs failed. Invalid multibyte character.\n";
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-01 23:59:28 +08:00
|
|
|
|
const int width = wcswidth(wcstring, length);
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (width == -1)
|
|
|
|
|
{
|
|
|
|
|
cerr << "\nError! wcswidth failed. Nonprintable wide character.\n";
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
if (wcstring)
|
2019-01-04 17:29:55 +08:00
|
|
|
|
delete[] wcstring;
|
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
return width;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
// Word wrap
|
|
|
|
|
// Source: https://gist.github.com/tdulcet/819821ca69501822ad3f84a060c640a0
|
|
|
|
|
// Adapted from: https://stackoverflow.com/a/42016346 and https://stackoverflow.com/a/13094734
|
|
|
|
|
string wrap(const char *const str, const size_t line_length)
|
2019-01-04 17:29:55 +08:00
|
|
|
|
{
|
2023-03-10 21:15:01 +08:00
|
|
|
|
string words = str;
|
2022-07-14 16:44:17 +08:00
|
|
|
|
string wrapped;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
size_t index = 0;
|
|
|
|
|
size_t linelen = 0;
|
|
|
|
|
while (words[index] != '\0')
|
2019-01-04 17:29:55 +08:00
|
|
|
|
{
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (words[index] == '\n')
|
2019-01-04 17:29:55 +08:00
|
|
|
|
{
|
2022-07-14 16:44:17 +08:00
|
|
|
|
linelen = 0;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
}
|
2022-07-14 16:44:17 +08:00
|
|
|
|
else if (isspace(words[index]))
|
|
|
|
|
{
|
|
|
|
|
size_t tempindex = index + 1;
|
|
|
|
|
size_t templinelen = linelen;
|
|
|
|
|
while (!isspace(words[tempindex]) and words[tempindex] != '\0')
|
|
|
|
|
{
|
|
|
|
|
++templinelen;
|
|
|
|
|
|
|
|
|
|
++tempindex;
|
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2024-06-01 23:59:28 +08:00
|
|
|
|
const string temp = words.substr(index - linelen, templinelen);
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2024-06-01 23:59:28 +08:00
|
|
|
|
const size_t width = strcol(temp.c_str());
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (width >= line_length)
|
|
|
|
|
{
|
|
|
|
|
words[index] = '\n';
|
|
|
|
|
linelen = 0;
|
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (words[index] == '\t')
|
|
|
|
|
linelen += 8 - (linelen % 8);
|
|
|
|
|
else if (words[index] != '\n')
|
|
|
|
|
++linelen;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
++index;
|
|
|
|
|
}
|
|
|
|
|
wrapped = words;
|
|
|
|
|
return wrapped;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-11 22:03:53 +08:00
|
|
|
|
// Auto-scale number to unit
|
|
|
|
|
// Adapted from: https://github.com/coreutils/coreutils/blob/master/src/numfmt.c
|
|
|
|
|
void outputunit(long double number, const units_type scale, ostringstream &strm)
|
|
|
|
|
{
|
2023-09-01 20:54:13 +08:00
|
|
|
|
unsigned x = 0;
|
2023-05-11 22:03:53 +08:00
|
|
|
|
long double val = number;
|
|
|
|
|
if (val >= -LDBL_MAX and val <= LDBL_MAX)
|
|
|
|
|
{
|
|
|
|
|
while (abs(val) >= 10)
|
|
|
|
|
{
|
|
|
|
|
++x;
|
|
|
|
|
val /= 10;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (scale == units_scale_none)
|
|
|
|
|
{
|
|
|
|
|
if (x > LDBL_DIG)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
strm << setprecision(LDBL_DIG) << number;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (x > 33 - 1)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
double scale_base;
|
|
|
|
|
|
|
|
|
|
switch (scale)
|
|
|
|
|
{
|
|
|
|
|
case units_scale_IEC:
|
|
|
|
|
case units_scale_IEC_I:
|
|
|
|
|
scale_base = 1024;
|
|
|
|
|
break;
|
|
|
|
|
case units_scale_none:
|
|
|
|
|
case units_scale_SI:
|
|
|
|
|
default:
|
|
|
|
|
scale_base = 1000;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-01 20:54:13 +08:00
|
|
|
|
unsigned power = 0;
|
2023-05-11 22:03:53 +08:00
|
|
|
|
if (number >= -LDBL_MAX and number <= LDBL_MAX)
|
|
|
|
|
{
|
|
|
|
|
while (abs(number) >= scale_base)
|
|
|
|
|
{
|
|
|
|
|
++power;
|
|
|
|
|
number /= scale_base;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long double anumber = abs(number);
|
|
|
|
|
anumber += anumber < 10 ? 0.0005 : anumber < 100 ? 0.005
|
|
|
|
|
: anumber < 1000 ? 0.05
|
|
|
|
|
: 0.5;
|
|
|
|
|
|
2023-12-31 23:57:30 +08:00
|
|
|
|
if (number and anumber < 1000 and power > 0)
|
2023-05-11 22:03:53 +08:00
|
|
|
|
{
|
|
|
|
|
strm << setprecision(LDBL_DIG) << number;
|
2024-06-01 23:59:28 +08:00
|
|
|
|
const string str = strm.str();
|
2023-05-11 22:03:53 +08:00
|
|
|
|
|
2023-09-01 20:54:13 +08:00
|
|
|
|
const unsigned length = 5 + (number < 0 ? 1 : 0);
|
2023-05-11 22:03:53 +08:00
|
|
|
|
if (str.length() > length)
|
|
|
|
|
{
|
|
|
|
|
const int prec = anumber < 10 ? 3 : anumber < 100 ? 2
|
|
|
|
|
: 1;
|
|
|
|
|
strm.str("");
|
|
|
|
|
strm << setprecision(prec) << fixed << number;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
strm << setprecision(0) << fixed << number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
strm << (power < graphs::size(suffix_power_char) ? suffix_power_char[power] : "(error)");
|
|
|
|
|
|
|
|
|
|
if (scale == units_scale_IEC_I and power > 0)
|
|
|
|
|
strm << "i";
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
// Convert fractions and constants to Unicode characters
|
2023-05-11 22:03:53 +08:00
|
|
|
|
void outputfraction(const long double number, ostringstream &strm)
|
2022-07-14 16:44:17 +08:00
|
|
|
|
{
|
|
|
|
|
bool output = false;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
const long double n = abs(number);
|
|
|
|
|
if (n <= MAX)
|
2019-01-04 17:29:55 +08:00
|
|
|
|
{
|
2023-03-10 21:15:01 +08:00
|
|
|
|
long double intpart = 0;
|
2024-06-01 23:59:28 +08:00
|
|
|
|
const long double fractionpart = abs(modf(number, &intpart));
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
for (size_t i = 0; i < graphs::size(fractions) and !output; ++i)
|
2022-07-14 16:44:17 +08:00
|
|
|
|
{
|
2023-03-10 21:15:01 +08:00
|
|
|
|
if (abs(fractionpart - fractionvalues[i]) <= DBL_EPSILON * n)
|
|
|
|
|
{
|
|
|
|
|
if (intpart == 0 and number < 0)
|
|
|
|
|
strm << "-";
|
|
|
|
|
else if (intpart != 0)
|
|
|
|
|
strm << setprecision(LDBL_DIG) << intpart;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
strm << fractions[i];
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
output = true;
|
|
|
|
|
}
|
2022-07-14 16:44:17 +08:00
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
if (n > DBL_EPSILON)
|
|
|
|
|
{
|
|
|
|
|
for (size_t i = 0; i < graphs::size(constants) and !output; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (abs(fmod(number, constantvalues[i])) <= DBL_EPSILON * n)
|
|
|
|
|
{
|
|
|
|
|
intpart = number / constantvalues[i];
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
if (intpart == -1)
|
|
|
|
|
strm << "-";
|
|
|
|
|
else if (intpart != 1)
|
|
|
|
|
strm << setprecision(LDBL_DIG) << intpart;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
strm << constants[i];
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
output = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-14 16:44:17 +08:00
|
|
|
|
}
|
2020-08-18 20:42:12 +08:00
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (!output)
|
2023-03-10 21:15:01 +08:00
|
|
|
|
strm << number;
|
2023-05-11 22:03:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t outputlabel(const long double label, const units_type units, ostringstream &strm)
|
|
|
|
|
{
|
|
|
|
|
strm.imbue(locale(""));
|
|
|
|
|
|
|
|
|
|
switch (units)
|
|
|
|
|
{
|
|
|
|
|
case units_number:
|
|
|
|
|
strm << label;
|
|
|
|
|
break;
|
|
|
|
|
case units_scale_none:
|
|
|
|
|
case units_scale_SI:
|
|
|
|
|
case units_scale_IEC:
|
|
|
|
|
case units_scale_IEC_I:
|
|
|
|
|
outputunit(label, units, strm);
|
|
|
|
|
break;
|
|
|
|
|
case units_fracts:
|
|
|
|
|
outputfraction(label, strm);
|
|
|
|
|
break;
|
|
|
|
|
case units_percent:
|
|
|
|
|
strm << label * 100 << '%';
|
|
|
|
|
break;
|
|
|
|
|
case units_date:
|
|
|
|
|
{
|
|
|
|
|
// const time_t t = chrono::system_clock::to_time_t(chrono::sys_seconds(chrono::duration_cast<chrono::seconds>(chrono::duration<double>(label))));
|
|
|
|
|
const time_t t = chrono::system_clock::to_time_t(chrono::system_clock::time_point(chrono::duration_cast<chrono::seconds>(chrono::duration<long double>(label))));
|
|
|
|
|
const tm atm = *localtime(&t);
|
|
|
|
|
strm << put_time(&atm, "%x");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case units_time:
|
|
|
|
|
{
|
|
|
|
|
// const time_t t = chrono::system_clock::to_time_t(chrono::sys_seconds(chrono::duration_cast<chrono::seconds>(chrono::duration<double>(label))));
|
|
|
|
|
const time_t t = chrono::system_clock::to_time_t(chrono::system_clock::time_point(chrono::duration_cast<chrono::seconds>(chrono::duration<long double>(label))));
|
|
|
|
|
const tm atm = *localtime(&t);
|
|
|
|
|
strm << put_time(&atm, "%X");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case units_monetary:
|
|
|
|
|
strm << showbase << put_money(label);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2024-06-01 23:59:28 +08:00
|
|
|
|
const size_t length = strcol(strm.str().c_str());
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
return length;
|
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
// Output graph
|
|
|
|
|
int graph(const size_t height, const size_t width, const long double xmin, const long double xmax, const long double ymin, const long double ymax, const vector<vector<unsigned short>> &array, const options &aoptions)
|
|
|
|
|
{
|
2023-03-10 18:07:00 +08:00
|
|
|
|
if (!graphs::size(array))
|
2022-07-14 16:44:17 +08:00
|
|
|
|
return 1;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
const bool border = aoptions.border;
|
2023-03-13 22:29:43 +08:00
|
|
|
|
const bool axis = aoptions.axis;
|
2022-07-14 16:44:17 +08:00
|
|
|
|
const bool axislabel = aoptions.axislabel;
|
2023-03-13 22:29:43 +08:00
|
|
|
|
const bool axistick = aoptions.axistick;
|
2022-07-14 16:44:17 +08:00
|
|
|
|
const bool axisunitslabel = aoptions.axisunitslabel;
|
2023-08-30 20:57:03 +08:00
|
|
|
|
const type_type type = aoptions.type;
|
2022-07-14 16:44:17 +08:00
|
|
|
|
const char *const title = aoptions.title;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (height == 0)
|
|
|
|
|
return 1;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (width == 0)
|
|
|
|
|
return 1;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
struct winsize w;
|
|
|
|
|
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
|
const size_t ai = type == type_histogram ? 8 : type == type_block ? 2
|
|
|
|
|
: 4;
|
|
|
|
|
const size_t aj = type == type_histogram ? 1 : 2;
|
|
|
|
|
|
|
|
|
|
const size_t aheight = height / ai;
|
|
|
|
|
const size_t awidth = width / aj;
|
2023-03-13 22:29:43 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
if (aoptions.check)
|
2022-07-14 16:44:17 +08:00
|
|
|
|
{
|
2023-03-10 21:15:01 +08:00
|
|
|
|
if (aheight > w.ws_row)
|
|
|
|
|
{
|
|
|
|
|
cerr << "The height of the graph (" << aheight << ") is greater then the height of the terminal (" << w.ws_row << ").\n";
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (awidth > w.ws_col)
|
|
|
|
|
{
|
|
|
|
|
cerr << "The width of the graph (" << awidth << ") is greater then the width of the terminal (" << w.ws_col << ").\n";
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2022-07-14 16:44:17 +08:00
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (xmin >= xmax)
|
|
|
|
|
{
|
|
|
|
|
cerr << "xmin must be less than xmax.\n";
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (ymin >= ymax)
|
|
|
|
|
{
|
|
|
|
|
cerr << "ymin must be less than ymax.\n";
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
const long double xstep = (xmax - xmin) / width;
|
|
|
|
|
const long double ystep = (ymax - ymin) / height;
|
|
|
|
|
const long double xaxis = xmin > 0 ? 0 : xmax < 0 ? width
|
|
|
|
|
: width - (xmax / xstep);
|
|
|
|
|
const long double yaxis = ymin > 0 ? height : ymax < 0 ? 0
|
|
|
|
|
: ymax / ystep;
|
2023-08-30 20:57:03 +08:00
|
|
|
|
const int xdivisor = 4 * aj * ((((2 * width) / aj) / 160) + 2);
|
|
|
|
|
const int ydivisor = 2 * ai * ((((4 * height) / ai) / 160) + 2);
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
setlocale(LC_ALL, "");
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
if (title and title[0] != '\0')
|
2023-05-11 22:03:53 +08:00
|
|
|
|
cout << wrap(title, awidth) << '\n';
|
2023-03-13 22:29:43 +08:00
|
|
|
|
|
2023-12-31 23:57:30 +08:00
|
|
|
|
const char *const *astyle = styles[aoptions.style];
|
|
|
|
|
|
2023-03-13 22:29:43 +08:00
|
|
|
|
if (border)
|
|
|
|
|
{
|
2023-12-31 23:57:30 +08:00
|
|
|
|
cout << astyle[2];
|
2023-03-13 22:29:43 +08:00
|
|
|
|
|
|
|
|
|
for (size_t k = 0; k < awidth; ++k)
|
2023-12-31 23:57:30 +08:00
|
|
|
|
cout << astyle[0];
|
2023-03-13 22:29:43 +08:00
|
|
|
|
|
2023-12-31 23:57:30 +08:00
|
|
|
|
cout << astyle[4] << '\n';
|
2023-03-13 22:29:43 +08:00
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
|
for (size_t i = 0; i < height; i += ai)
|
2019-01-04 17:29:55 +08:00
|
|
|
|
{
|
2023-08-30 20:57:03 +08:00
|
|
|
|
const bool ayaxis = yaxis <= (height - ai) ? i <= yaxis and (i + ai) > yaxis : i < yaxis and (i + ai) >= yaxis;
|
|
|
|
|
const bool yaxislabel = yaxis <= (height - ai) ? i <= (yaxis + ai) and (i + ai) > (yaxis + ai) : i < (yaxis - ai) and (i + ai) >= (yaxis - ai);
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
ostringstream ylabelstrm;
|
|
|
|
|
size_t ylabellength = 0;
|
|
|
|
|
|
2023-05-11 22:03:53 +08:00
|
|
|
|
if (axis and axistick and axisunitslabel and yaxis >= 0 and yaxis <= height)
|
2019-01-04 17:29:55 +08:00
|
|
|
|
{
|
2022-07-14 16:44:17 +08:00
|
|
|
|
bool output = false;
|
|
|
|
|
long double label = 0;
|
2024-06-01 23:59:28 +08:00
|
|
|
|
const int adivisor = i < yaxis ? -ydivisor : ydivisor;
|
2022-07-14 16:44:17 +08:00
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
|
for (long double k = yaxis + adivisor; (i < yaxis ? k >= i : k < (i + ai)) and i >= ai and !output; k += adivisor)
|
2019-01-04 17:29:55 +08:00
|
|
|
|
{
|
2023-08-30 20:57:03 +08:00
|
|
|
|
if (i <= k and (i + ai) > k)
|
2022-07-14 16:44:17 +08:00
|
|
|
|
{
|
2023-03-10 21:15:01 +08:00
|
|
|
|
label = ymax - ((k > height ? height : k) * ystep);
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
output = true;
|
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (output)
|
|
|
|
|
{
|
2023-05-11 22:03:53 +08:00
|
|
|
|
ylabellength = outputlabel(label, aoptions.yunits, ylabelstrm);
|
2023-08-30 20:57:03 +08:00
|
|
|
|
ylabellength *= aj;
|
2022-07-14 16:44:17 +08:00
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-13 22:29:43 +08:00
|
|
|
|
if (border)
|
2023-12-31 23:57:30 +08:00
|
|
|
|
cout << astyle[1];
|
2023-03-13 22:29:43 +08:00
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
|
for (size_t j = 0; j < width; j += aj)
|
2022-07-14 16:44:17 +08:00
|
|
|
|
{
|
2023-08-30 20:57:03 +08:00
|
|
|
|
const bool axaxis = xaxis >= aj ? j < xaxis and (j + aj) >= xaxis : j <= xaxis and (j + aj) > xaxis;
|
|
|
|
|
const bool xaxislabel = xaxis >= aj ? j < (xaxis - aj) and (j + aj) >= (xaxis - aj) : j <= (xaxis + aj) and (j + aj) > (xaxis + aj);
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
bool output = false;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-13 22:29:43 +08:00
|
|
|
|
if (axis)
|
2019-01-04 17:29:55 +08:00
|
|
|
|
{
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (axaxis and ayaxis)
|
2019-01-04 17:29:55 +08:00
|
|
|
|
{
|
2023-12-31 23:57:30 +08:00
|
|
|
|
cout << astyle[6];
|
2022-07-14 16:44:17 +08:00
|
|
|
|
output = true;
|
|
|
|
|
}
|
|
|
|
|
else if (axaxis)
|
|
|
|
|
{
|
2023-03-10 21:15:01 +08:00
|
|
|
|
if (i == 0)
|
2019-01-04 17:29:55 +08:00
|
|
|
|
{
|
2023-12-31 23:57:30 +08:00
|
|
|
|
cout << astyle[4];
|
2023-03-10 21:15:01 +08:00
|
|
|
|
output = true;
|
|
|
|
|
}
|
2023-08-30 20:57:03 +08:00
|
|
|
|
else if (i >= (height - ai))
|
2023-03-10 21:15:01 +08:00
|
|
|
|
{
|
2023-12-31 23:57:30 +08:00
|
|
|
|
cout << astyle[10];
|
2023-03-10 21:15:01 +08:00
|
|
|
|
output = true;
|
|
|
|
|
}
|
2023-05-11 22:03:53 +08:00
|
|
|
|
else if (axistick)
|
2023-03-10 21:15:01 +08:00
|
|
|
|
{
|
2024-06-01 23:59:28 +08:00
|
|
|
|
const int adivisor = i < yaxis ? -ydivisor : ydivisor;
|
2022-07-14 16:44:17 +08:00
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
|
for (long double k = yaxis + adivisor; (i < yaxis ? k >= i : k < (i + ai)) and i >= ai and !output; k += adivisor)
|
2019-01-04 17:29:55 +08:00
|
|
|
|
{
|
2023-08-30 20:57:03 +08:00
|
|
|
|
if (i <= k and (i + ai) > k)
|
2022-07-14 16:44:17 +08:00
|
|
|
|
{
|
2023-12-31 23:57:30 +08:00
|
|
|
|
cout << astyle[xaxis >= aj ? 7 : 5];
|
2022-07-14 16:44:17 +08:00
|
|
|
|
output = true;
|
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (!output)
|
|
|
|
|
{
|
2023-12-31 23:57:30 +08:00
|
|
|
|
cout << astyle[1];
|
2022-07-14 16:44:17 +08:00
|
|
|
|
output = true;
|
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
}
|
2022-07-14 16:44:17 +08:00
|
|
|
|
else if (ayaxis)
|
2019-01-04 17:29:55 +08:00
|
|
|
|
{
|
2023-03-10 21:15:01 +08:00
|
|
|
|
if (j == 0)
|
2019-01-04 17:29:55 +08:00
|
|
|
|
{
|
2023-12-31 23:57:30 +08:00
|
|
|
|
cout << astyle[2];
|
2023-03-10 21:15:01 +08:00
|
|
|
|
output = true;
|
|
|
|
|
}
|
2023-08-30 20:57:03 +08:00
|
|
|
|
else if (j >= (width - aj))
|
2023-03-10 21:15:01 +08:00
|
|
|
|
{
|
2023-12-31 23:57:30 +08:00
|
|
|
|
cout << astyle[4];
|
2023-03-10 21:15:01 +08:00
|
|
|
|
output = true;
|
|
|
|
|
}
|
2023-05-11 22:03:53 +08:00
|
|
|
|
else if (axistick)
|
2023-03-10 21:15:01 +08:00
|
|
|
|
{
|
2024-06-01 23:59:28 +08:00
|
|
|
|
const int adivisor = j < xaxis ? -xdivisor : xdivisor;
|
2022-07-14 16:44:17 +08:00
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
|
for (long double k = xaxis + adivisor; (j < xaxis ? k >= j : k < (j + aj)) and j < (width - (aj * 2)) and !output; k += adivisor)
|
2019-01-04 17:29:55 +08:00
|
|
|
|
{
|
2023-08-30 20:57:03 +08:00
|
|
|
|
if (j <= k and (j + aj) > k)
|
2022-07-14 16:44:17 +08:00
|
|
|
|
{
|
2023-12-31 23:57:30 +08:00
|
|
|
|
cout << astyle[yaxis <= (height - ai) ? 3 : 9];
|
2022-07-14 16:44:17 +08:00
|
|
|
|
output = true;
|
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (!output)
|
|
|
|
|
{
|
2023-12-31 23:57:30 +08:00
|
|
|
|
cout << astyle[0];
|
2022-07-14 16:44:17 +08:00
|
|
|
|
output = true;
|
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
}
|
2023-05-11 22:03:53 +08:00
|
|
|
|
else if (yaxislabel and xaxislabel and axistick and axisunitslabel and ymin <= 0 and ymax >= 0 and xmin <= 0 and xmax >= 0)
|
2019-01-04 17:29:55 +08:00
|
|
|
|
{
|
2023-12-31 23:57:30 +08:00
|
|
|
|
cout << '0';
|
2019-01-04 17:29:55 +08:00
|
|
|
|
output = true;
|
|
|
|
|
}
|
2023-08-30 20:57:03 +08:00
|
|
|
|
else if ((xaxis <= (width - aj) ? j >= (width - aj) : j == 0) and yaxislabel and axislabel)
|
2019-01-04 17:29:55 +08:00
|
|
|
|
{
|
2023-12-31 23:57:30 +08:00
|
|
|
|
cout << 'x';
|
2022-07-14 16:44:17 +08:00
|
|
|
|
output = true;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
}
|
2023-05-11 22:03:53 +08:00
|
|
|
|
else if (yaxislabel and axistick and axisunitslabel)
|
2019-01-04 17:29:55 +08:00
|
|
|
|
{
|
2022-07-14 16:44:17 +08:00
|
|
|
|
long double label = 0;
|
2024-06-01 23:59:28 +08:00
|
|
|
|
const int adivisor = j < xaxis ? -xdivisor : xdivisor;
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (j < xaxis)
|
2023-08-30 20:57:03 +08:00
|
|
|
|
j += aj;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
|
for (long double k = xaxis + adivisor; (j < xaxis ? k >= j : k < (j + aj)) and j < (width - aj) and !output; k += adivisor)
|
2022-07-14 16:44:17 +08:00
|
|
|
|
{
|
2023-08-30 20:57:03 +08:00
|
|
|
|
if (j <= k and (j + aj) > k)
|
2022-07-14 16:44:17 +08:00
|
|
|
|
{
|
2023-03-10 21:15:01 +08:00
|
|
|
|
label = ((k > width ? width : k) * xstep) + xmin;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
output = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (adivisor < 0)
|
2023-08-30 20:57:03 +08:00
|
|
|
|
j -= aj;
|
2022-07-14 16:44:17 +08:00
|
|
|
|
|
|
|
|
|
if (output)
|
2019-01-04 17:29:55 +08:00
|
|
|
|
{
|
2022-07-14 16:44:17 +08:00
|
|
|
|
output = false;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
ostringstream strm;
|
2023-05-11 22:03:53 +08:00
|
|
|
|
size_t length = outputlabel(label, aoptions.xunits, strm);
|
2023-08-30 20:57:03 +08:00
|
|
|
|
length *= aj;
|
|
|
|
|
if ((j >= xaxis or (j + length) < (ymin <= 0 and ymax >= 0 and xmin <= 0 and xmax >= 0 ? xaxis - ai : xaxis)) and (j + length) < (width - aj) and (xaxis <= (width - aj) or j > aj))
|
2022-07-14 16:44:17 +08:00
|
|
|
|
{
|
|
|
|
|
cout << strm.str();
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
|
if (length > aj)
|
|
|
|
|
j += length - aj;
|
2022-07-14 16:44:17 +08:00
|
|
|
|
|
|
|
|
|
if (adivisor < 0)
|
|
|
|
|
output = true;
|
|
|
|
|
else
|
2023-08-30 20:57:03 +08:00
|
|
|
|
j += aj;
|
2022-07-14 16:44:17 +08:00
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-30 20:57:03 +08:00
|
|
|
|
else if ((yaxis >= ai ? i == 0 : i >= (height - ai)) and xaxislabel and axislabel)
|
2022-07-14 16:44:17 +08:00
|
|
|
|
{
|
2023-12-31 23:57:30 +08:00
|
|
|
|
cout << 'y';
|
2022-07-14 16:44:17 +08:00
|
|
|
|
output = true;
|
|
|
|
|
}
|
2023-08-30 20:57:03 +08:00
|
|
|
|
else if (ylabellength and (xaxis < aj ? xaxislabel : j < (xaxis - ylabellength) and (j + aj) >= (xaxis - ylabellength)) and (yaxis >= ai or i < (height - ai)) and axistick and axisunitslabel)
|
2022-07-14 16:44:17 +08:00
|
|
|
|
{
|
|
|
|
|
cout << ylabelstrm.str();
|
|
|
|
|
output = true;
|
2023-08-30 20:57:03 +08:00
|
|
|
|
if (ylabellength > aj)
|
|
|
|
|
j += ylabellength - aj;
|
2022-07-14 16:44:17 +08:00
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (!output)
|
2019-01-04 17:29:55 +08:00
|
|
|
|
{
|
2023-03-10 21:15:01 +08:00
|
|
|
|
size_t dot = 0;
|
2022-07-14 16:44:17 +08:00
|
|
|
|
unsigned short color = 0;
|
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
|
for (size_t k = 0; k < aj and k < (width - j); ++k)
|
2019-01-04 17:29:55 +08:00
|
|
|
|
{
|
2023-08-30 20:57:03 +08:00
|
|
|
|
for (size_t l = 0; l < ai and l < (height - i); ++l)
|
2019-01-04 17:29:55 +08:00
|
|
|
|
{
|
2023-03-10 21:15:01 +08:00
|
|
|
|
const unsigned short value = array[j + k][i + l];
|
|
|
|
|
if (value)
|
2023-08-30 20:57:03 +08:00
|
|
|
|
{
|
|
|
|
|
if (type == type_histogram)
|
|
|
|
|
{
|
|
|
|
|
if (!dot)
|
|
|
|
|
dot = (graphs::size(bars) - l) - 1;
|
|
|
|
|
}
|
|
|
|
|
else if (type == type_block)
|
|
|
|
|
dot += blockvalues[k][l];
|
|
|
|
|
else
|
|
|
|
|
dot += dotvalues[k][l];
|
|
|
|
|
}
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (color)
|
|
|
|
|
{
|
2023-03-10 21:15:01 +08:00
|
|
|
|
if (value and color != value)
|
2022-07-14 16:44:17 +08:00
|
|
|
|
color = 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
2023-03-10 21:15:01 +08:00
|
|
|
|
color = value;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (color)
|
|
|
|
|
--color;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (color)
|
|
|
|
|
cout << colors[color];
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
|
cout << (type == type_histogram ? bars[dot] : type == type_block ? blocks[dot]
|
|
|
|
|
: dots[dot]);
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (color)
|
|
|
|
|
cout << colors[0];
|
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-13 22:29:43 +08:00
|
|
|
|
if (border)
|
2023-12-31 23:57:30 +08:00
|
|
|
|
cout << astyle[1];
|
2023-03-13 22:29:43 +08:00
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
|
if (i < (height - ai) or border)
|
2023-05-11 22:03:53 +08:00
|
|
|
|
cout << '\n';
|
2022-07-14 16:44:17 +08:00
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-13 22:29:43 +08:00
|
|
|
|
if (border)
|
|
|
|
|
{
|
2023-12-31 23:57:30 +08:00
|
|
|
|
cout << astyle[8];
|
2023-03-13 22:29:43 +08:00
|
|
|
|
|
|
|
|
|
for (size_t k = 0; k < awidth; ++k)
|
2023-12-31 23:57:30 +08:00
|
|
|
|
cout << astyle[0];
|
2023-03-13 22:29:43 +08:00
|
|
|
|
|
2023-12-31 23:57:30 +08:00
|
|
|
|
cout << astyle[10];
|
2023-03-13 22:29:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-01 23:59:28 +08:00
|
|
|
|
cout << '\n';
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
|
template <typename T>
|
|
|
|
|
int histogram(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const T &aarray, /* const */ options &aoptions = histogram_defaultoptions)
|
|
|
|
|
{
|
|
|
|
|
if (!graphs::size(aarray))
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
const color_type color = aoptions.color;
|
|
|
|
|
|
|
|
|
|
struct winsize w;
|
|
|
|
|
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
|
|
|
|
|
|
|
|
|
if (height == 0)
|
|
|
|
|
height = w.ws_row * 4;
|
|
|
|
|
|
|
|
|
|
if (width == 0)
|
|
|
|
|
width = w.ws_col * 2;
|
|
|
|
|
|
|
|
|
|
if (aoptions.check)
|
|
|
|
|
{
|
|
|
|
|
const size_t aheight = height / 4;
|
|
|
|
|
const size_t awidth = width / 2;
|
|
|
|
|
|
|
|
|
|
if (aheight > w.ws_row)
|
|
|
|
|
{
|
|
|
|
|
cerr << "The height of the graph (" << aheight << ") is greater then the height of the terminal (" << w.ws_row << ").\n";
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (awidth > w.ws_col)
|
|
|
|
|
{
|
|
|
|
|
cerr << "The width of the graph (" << awidth << ") is greater then the width of the terminal (" << w.ws_col << ").\n";
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
height *= 2;
|
|
|
|
|
width /= 2;
|
|
|
|
|
|
|
|
|
|
if (xmin == 0 and xmax == 0)
|
|
|
|
|
{
|
|
|
|
|
const auto &minmax = minmax_element(begin(aarray), end(aarray));
|
|
|
|
|
|
|
|
|
|
xmin = *minmax.first;
|
|
|
|
|
xmax = *minmax.second;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (xmin >= xmax)
|
|
|
|
|
{
|
|
|
|
|
cerr << "xmin must be less than xmax.\n";
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vector<size_t> histogram(width, 0);
|
|
|
|
|
|
|
|
|
|
const long double xstep = (xmax - xmin) / width;
|
|
|
|
|
|
|
|
|
|
for (const auto &x : aarray)
|
|
|
|
|
{
|
|
|
|
|
if (x >= xmin and x < xmax)
|
|
|
|
|
{
|
|
|
|
|
const size_t index = (x - xmin) / xstep;
|
|
|
|
|
++histogram[index];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ymin == 0 and ymax == 0)
|
|
|
|
|
{
|
|
|
|
|
const auto &minmax = minmax_element(histogram.begin(), histogram.end());
|
|
|
|
|
|
|
|
|
|
ymin = *minmax.first;
|
|
|
|
|
ymax = *minmax.second;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ymin >= ymax)
|
|
|
|
|
{
|
|
|
|
|
cerr << "ymin must be less than ymax.\n";
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const long double ystep = (ymax - ymin) / height;
|
|
|
|
|
const long double yaxis = ymax / ystep;
|
|
|
|
|
|
|
|
|
|
vector<vector<unsigned short>> aaarray(width, vector<unsigned short>(height, 0));
|
|
|
|
|
|
2023-09-01 20:54:13 +08:00
|
|
|
|
const unsigned acolor = color + 1;
|
2023-08-30 20:57:03 +08:00
|
|
|
|
|
|
|
|
|
for (size_t x = 0; x < graphs::size(histogram); ++x)
|
|
|
|
|
{
|
|
|
|
|
const size_t ay = histogram[x];
|
|
|
|
|
|
|
|
|
|
for (size_t y = ay >= ymax ? 0 : yaxis - (ay / ystep); y < yaxis and y < height; ++y)
|
|
|
|
|
aaarray[x][y] = acolor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
aoptions.type = type_histogram;
|
|
|
|
|
|
|
|
|
|
return graph(height, width, xmin, xmax, ymin, ymax, aaarray, aoptions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
int histogram(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const size_t rows, T *aarray, /* const */ options &aoptions = histogram_defaultoptions)
|
|
|
|
|
{
|
|
|
|
|
if (rows == 0)
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
vector<T> aaarray(rows);
|
|
|
|
|
copy(aarray, aarray + rows, aaarray.begin());
|
|
|
|
|
|
|
|
|
|
return histogram(height, width, xmin, xmax, ymin, ymax, aaarray, aoptions);
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
// Convert one or more arrays to graph and output
|
|
|
|
|
template <typename T>
|
2023-08-30 20:57:03 +08:00
|
|
|
|
int plots(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const T &arrays, const options &aoptions = defaultoptions)
|
2022-07-14 16:33:38 +08:00
|
|
|
|
{
|
2023-03-10 18:07:00 +08:00
|
|
|
|
if (!graphs::size(arrays))
|
2022-07-14 16:44:17 +08:00
|
|
|
|
return 1;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2024-06-01 23:59:28 +08:00
|
|
|
|
if (!all_of(cbegin(arrays), cend(arrays), [](const auto &array)
|
|
|
|
|
{ return all_of(cbegin(array), cend(array), [](const auto &x)
|
2023-03-10 18:07:00 +08:00
|
|
|
|
{ return graphs::size(x) == 2; }); }))
|
2022-07-14 16:44:17 +08:00
|
|
|
|
{
|
2023-03-10 21:15:01 +08:00
|
|
|
|
cerr << "Error: The arrays must have two columns.\n";
|
2022-07-14 16:44:17 +08:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
const color_type color = aoptions.color;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
struct winsize w;
|
|
|
|
|
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (height == 0)
|
|
|
|
|
height = w.ws_row * 4;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (width == 0)
|
|
|
|
|
width = w.ws_col * 2;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
if (aoptions.check)
|
2022-07-14 16:44:17 +08:00
|
|
|
|
{
|
2023-03-10 21:15:01 +08:00
|
|
|
|
const size_t aheight = height / 4;
|
|
|
|
|
const size_t awidth = width / 2;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
if (aheight > w.ws_row)
|
|
|
|
|
{
|
|
|
|
|
cerr << "The height of the graph (" << aheight << ") is greater then the height of the terminal (" << w.ws_row << ").\n";
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (awidth > w.ws_col)
|
|
|
|
|
{
|
|
|
|
|
cerr << "The width of the graph (" << awidth << ") is greater then the width of the terminal (" << w.ws_col << ").\n";
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2022-07-14 16:44:17 +08:00
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
|
if (aoptions.type == type_block)
|
|
|
|
|
height /= 2;
|
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (xmin == 0 and xmax == 0)
|
|
|
|
|
{
|
|
|
|
|
const auto compare = [](const auto &a, const auto &b)
|
|
|
|
|
{ return a[0] < b[0]; };
|
2023-08-30 20:57:03 +08:00
|
|
|
|
const auto &result = accumulate(begin(arrays), end(arrays), make_pair(arrays[0][0], arrays[0][0]), [compare](const auto ¤t, const auto &array)
|
|
|
|
|
{ const auto &minmax = minmax_element(begin(array), end(array), compare); return make_pair(min(current.first, *minmax.first, compare), max(current.second, *minmax.second, compare)); });
|
2022-07-14 16:44:17 +08:00
|
|
|
|
xmin = result.first[0];
|
|
|
|
|
xmax = result.second[0];
|
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (ymin == 0 and ymax == 0)
|
|
|
|
|
{
|
|
|
|
|
const auto compare = [](const auto &a, const auto &b)
|
|
|
|
|
{ return a[1] < b[1]; };
|
2023-08-30 20:57:03 +08:00
|
|
|
|
const auto &result = accumulate(begin(arrays), end(arrays), make_pair(arrays[0][0], arrays[0][0]), [compare](const auto ¤t, const auto &array)
|
|
|
|
|
{ const auto &minmax = minmax_element(begin(array), end(array), compare); return make_pair(min(current.first, *minmax.first, compare), max(current.second, *minmax.second, compare)); });
|
2022-07-14 16:44:17 +08:00
|
|
|
|
ymin = result.first[1];
|
|
|
|
|
ymax = result.second[1];
|
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (xmin >= xmax)
|
|
|
|
|
{
|
|
|
|
|
cerr << "xmin must be less than xmax.\n";
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (ymin >= ymax)
|
|
|
|
|
{
|
|
|
|
|
cerr << "ymin must be less than ymax.\n";
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
const long double xstep = (xmax - xmin) / width;
|
|
|
|
|
const long double ystep = (ymax - ymin) / height;
|
|
|
|
|
const long double xaxis = width - (xmax / xstep);
|
|
|
|
|
const long double yaxis = ymax / ystep;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
vector<vector<unsigned short>> aarray(width, vector<unsigned short>(height, 0));
|
2022-07-14 16:33:38 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
for (size_t j = 0; j < graphs::size(arrays); ++j)
|
2019-01-04 17:29:55 +08:00
|
|
|
|
{
|
2023-03-10 21:15:01 +08:00
|
|
|
|
const auto &array = arrays[j];
|
2023-12-31 23:57:30 +08:00
|
|
|
|
const unsigned acolor = graphs::size(arrays) == 1 ? color + 1 : (j % (graphs::size(colors) - 2)) + 3;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
for (size_t i = 0; i < graphs::size(array); ++i)
|
2022-07-14 16:44:17 +08:00
|
|
|
|
{
|
2023-03-10 21:15:01 +08:00
|
|
|
|
const auto &x = array[i][0], &y = array[i][1];
|
2023-03-13 22:29:43 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
if (x >= xmin and x < xmax and y >= ymin and y < ymax)
|
2022-07-14 16:33:38 +08:00
|
|
|
|
{
|
2023-03-10 21:15:01 +08:00
|
|
|
|
const size_t ax = (x / xstep) + xaxis;
|
|
|
|
|
const size_t ay = (yaxis - (y / ystep)) - 1;
|
2022-07-14 16:44:17 +08:00
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
|
for (const auto &mark : marks[aoptions.mark])
|
2022-07-14 16:44:17 +08:00
|
|
|
|
{
|
2023-08-30 20:57:03 +08:00
|
|
|
|
const size_t x = ax + mark[0];
|
|
|
|
|
const size_t y = ay + mark[1];
|
|
|
|
|
|
|
|
|
|
if (x < width and y < height)
|
|
|
|
|
{
|
|
|
|
|
if (aarray[x][y])
|
|
|
|
|
{
|
|
|
|
|
if (aarray[x][y] != acolor)
|
|
|
|
|
aarray[x][y] = 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
aarray[x][y] = acolor;
|
|
|
|
|
}
|
2022-07-14 16:44:17 +08:00
|
|
|
|
}
|
2022-07-14 16:33:38 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
return graph(height, width, xmin, xmax, ymin, ymax, aarray, aoptions);
|
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
// Convert single array to graph and output
|
|
|
|
|
template <typename T>
|
2023-08-30 20:57:03 +08:00
|
|
|
|
int plot(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const T &aarray, const options &aoptions = defaultoptions)
|
2022-07-14 16:44:17 +08:00
|
|
|
|
{
|
|
|
|
|
const std::array<T, 1> aaarray = {aarray};
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
|
return plots(height, width, xmin, xmax, ymin, ymax, aaarray, aoptions);
|
2022-07-14 16:44:17 +08:00
|
|
|
|
}
|
2022-07-14 16:33:38 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
// Convert single array to graph and output
|
|
|
|
|
template <typename T>
|
2023-08-30 20:57:03 +08:00
|
|
|
|
int plot(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const size_t rows, T **aarray, const options &aoptions = defaultoptions)
|
2022-07-14 16:44:17 +08:00
|
|
|
|
{
|
|
|
|
|
if (rows == 0)
|
|
|
|
|
return 1;
|
2022-07-14 16:33:38 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
const size_t columns = 2;
|
|
|
|
|
vector<std::array<T, columns>> aaarray(rows);
|
2023-03-10 21:15:01 +08:00
|
|
|
|
for (size_t i = 0; i < rows; ++i)
|
2022-07-14 16:44:17 +08:00
|
|
|
|
copy(aarray[i], aarray[i] + columns, aaarray[i].begin());
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
|
return plot(height, width, xmin, xmax, ymin, ymax, aaarray, aoptions);
|
2022-07-14 16:44:17 +08:00
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
// Convert one or more functions to graph and output
|
|
|
|
|
template <typename T>
|
|
|
|
|
int functions(size_t height, size_t width, const long double xmin, const long double xmax, const long double ymin, const long double ymax, const size_t numfunctions, function<T(T)> functions[], const options &aoptions = defaultoptions)
|
|
|
|
|
{
|
2023-03-10 21:15:01 +08:00
|
|
|
|
const color_type color = aoptions.color;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (numfunctions == 0)
|
|
|
|
|
return 1;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
struct winsize w;
|
|
|
|
|
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (height == 0)
|
|
|
|
|
height = w.ws_row * 4;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (width == 0)
|
|
|
|
|
width = w.ws_col * 2;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
if (aoptions.check)
|
2022-07-14 16:44:17 +08:00
|
|
|
|
{
|
2023-03-10 21:15:01 +08:00
|
|
|
|
const size_t aheight = height / 4;
|
|
|
|
|
const size_t awidth = width / 2;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
if (aheight > w.ws_row)
|
|
|
|
|
{
|
|
|
|
|
cerr << "The height of the graph (" << aheight << ") is greater then the height of the terminal (" << w.ws_row << ").\n";
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (awidth > w.ws_col)
|
|
|
|
|
{
|
|
|
|
|
cerr << "The width of the graph (" << awidth << ") is greater then the width of the terminal (" << w.ws_col << ").\n";
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2022-07-14 16:44:17 +08:00
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
|
if (aoptions.type == type_block)
|
|
|
|
|
height /= 2;
|
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (xmin >= xmax)
|
|
|
|
|
{
|
|
|
|
|
cerr << "xmin must be less than xmax.\n";
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (ymin >= ymax)
|
|
|
|
|
{
|
|
|
|
|
cerr << "ymin must be less than ymax.\n";
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
const size_t rows = width;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
const long double xstep = (xmax - xmin) / width;
|
|
|
|
|
const long double ystep = (ymax - ymin) / height;
|
|
|
|
|
const long double xaxis = width - (xmax / xstep);
|
|
|
|
|
const long double yaxis = ymax / ystep;
|
|
|
|
|
const size_t xres = 2;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
vector<vector<unsigned short>> array(width, vector<unsigned short>(height, 0));
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
for (size_t j = 0; j < numfunctions; ++j)
|
2019-01-04 17:29:55 +08:00
|
|
|
|
{
|
2024-06-01 23:59:28 +08:00
|
|
|
|
const unsigned short acolor = numfunctions == 1 ? color + 1 : (j % (graphs::size(colors) - 2)) + 3;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
for (size_t i = 0; i < rows * xres; ++i)
|
2019-01-04 17:29:55 +08:00
|
|
|
|
{
|
2023-03-10 21:15:01 +08:00
|
|
|
|
T x = ((i / (long double)xres) * xstep) + xmin;
|
2022-07-14 16:44:17 +08:00
|
|
|
|
T y = (functions[j])(x);
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
if (x >= xmin and x < xmax and y >= ymin and y < ymax)
|
2019-01-04 17:29:55 +08:00
|
|
|
|
{
|
2023-03-10 21:15:01 +08:00
|
|
|
|
const size_t ax = (x / xstep) + xaxis;
|
|
|
|
|
const size_t ay = (yaxis - (y / ystep)) - 1;
|
2022-07-14 16:44:17 +08:00
|
|
|
|
|
|
|
|
|
if (array[ax][ay])
|
|
|
|
|
{
|
|
|
|
|
if (array[ax][ay] != acolor)
|
|
|
|
|
array[ax][ay] = 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
array[ax][ay] = acolor;
|
2019-01-04 17:29:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-14 16:44:17 +08:00
|
|
|
|
|
|
|
|
|
return graph(height, width, xmin, xmax, ymin, ymax, array, aoptions);
|
2019-01-04 17:29:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
// Convert single function to function array and output
|
|
|
|
|
template <typename T>
|
|
|
|
|
int function(size_t height, size_t width, const long double xmin, const long double xmax, const long double ymin, const long double ymax, const function<T(T)> &afunction, const options &aoptions = defaultoptions)
|
|
|
|
|
{
|
|
|
|
|
std::function<T(T)> afunctions[] = {afunction};
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
return functions(height, width, xmin, xmax, ymin, ymax, 1, afunctions, aoptions);
|
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
// Convert single function to function array and output
|
|
|
|
|
template <typename T>
|
|
|
|
|
int function(size_t height, size_t width, const long double xmin, const long double xmax, const long double ymin, const long double ymax, T afunction(T), const options &aoptions = defaultoptions)
|
|
|
|
|
{
|
|
|
|
|
std::function<T(T)> afunctions[] = {afunction};
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
2022-07-14 16:44:17 +08:00
|
|
|
|
return functions(height, width, xmin, xmax, ymin, ymax, 1, afunctions, aoptions);
|
|
|
|
|
}
|
2019-01-04 17:29:55 +08:00
|
|
|
|
|
|
|
|
|
}
|