gctl/lib/graphic/cliplot.h

206 lines
6.6 KiB
C
Raw Normal View History

2025-02-24 12:09:53 +08:00
/********************************************************
*
*
*
*
*
*
* Geophysical Computational Tools & Library (GCTL)
*
* Copyright (c) 2023 Yi Zhang (yizhang-geo@zju.edu.cn)
*
* GCTL is distributed under a dual licensing scheme. You can redistribute
* it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 2
* of the License, or (at your option) any later version. You should have
* received a copy of the GNU Lesser General Public License along with this
* program. If not, see <http://www.gnu.org/licenses/>.
*
* If the terms and conditions of the LGPL v.2. would prevent you from using
* the GCTL, please consider the option to obtain a commercial license for a
* fee. These licenses are offered by the GCTL's original author. As a rule,
* licenses are provided "as-is", unlimited in time for a one time fee. Please
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
* to include some description of your company and the realm of its activities.
* Also add information on how to contact you by electronic and paper mail.
******************************************************/
#ifndef _GCTL_CLIPLOT_H
#define _GCTL_CLIPLOT_H
#include <sys/ioctl.h>
#include <unistd.h>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <random>
#include <algorithm>
2025-04-23 12:39:44 +08:00
#include "../io/term_io.h"
2025-02-24 12:09:53 +08:00
namespace gctl
{
class cliplot
{
public:
/**
* @brief
*
* @param width
* @param height
* @param xmin x轴最小值
* @param xmax x轴最大值
* @param ymin y轴最小值
* @param ymax y轴最大值
*/
cliplot(size_t width, size_t height, double xmin, double xmax, double ymin, double ymax);
2025-02-24 21:45:40 +08:00
virtual ~cliplot(); ///< 析构函数
2025-02-24 12:09:53 +08:00
/**
* @brief
*/
void clear();
/**
* @brief
*
* @param w
* @param h
* @param sym
* @param att
*/
void set(size_t w, size_t h, char sym, const std::string &att);
2025-02-24 21:45:40 +08:00
/**
* @brief
*
* @param xt_num x轴刻度数量
* @param yt_num y轴刻度数量
*/
2025-02-24 12:09:53 +08:00
void set_axis(int xt_num, int yt_num);
2025-02-24 21:45:40 +08:00
/**
* @brief
*
* @param digs
*/
2025-02-24 12:09:53 +08:00
void set_digs(int digs);
2025-02-24 21:45:40 +08:00
/**
* @brief 使
*
* @param new_screen 使
*/
2025-02-24 12:09:53 +08:00
void set_new_screen(bool new_screen);
2025-02-24 21:45:40 +08:00
/**
* @brief
*
* @param wname x轴名称
*/
2025-02-24 12:09:53 +08:00
void set_wname(const std::string &wname);
2025-02-24 21:45:40 +08:00
/**
* @brief
*
* @param hname y轴名称
*/
2025-02-24 12:09:53 +08:00
void set_hname(const std::string &hname);
2025-02-24 21:45:40 +08:00
/**
* @brief 线
*
* @param x1 线x坐标
* @param x2 线x坐标
* @param y1 线y坐标
* @param y2 线y坐标
* @param s 线
* @param t 线
*/
2025-02-24 12:09:53 +08:00
void plot_line(double x1, double x2, double y1, double y2, char s = '.', const std::string &t = GCTL_BOLDGREEN);
2025-02-24 21:45:40 +08:00
/**
* @brief
*
* @param func
* @param s
* @param t
*/
2025-02-24 12:09:53 +08:00
template <typename FuncOp>
void plot_func(FuncOp func, char s = '.', const std::string &t = GCTL_BOLDGREEN);
2025-02-24 21:45:40 +08:00
/**
* @brief
*
* @param x x坐标
* @param y y坐标
* @param s
* @param t
*/
2025-02-24 12:09:53 +08:00
void plot_data(const std::vector<double> &x, const std::vector<double> &y, char s = '.', const std::string &t = GCTL_BOLDGREEN);
2025-02-24 21:45:40 +08:00
/**
* @brief
*
* @param os
*/
2025-02-24 12:09:53 +08:00
void display(std::ostream &os = std::cout);
2025-02-24 21:45:40 +08:00
private:
/**
* @brief
*
* @param num
* @param digs
* @param odr 10
* @return std::string
*/
std::string axis_label(double num, int digs, int &odr);
/**
* @brief
*/
void plot_axis();
2025-02-24 12:09:53 +08:00
private:
bool new_screen_;
int digs_; // 数字位数(包括小数点)
int xl_num_; // x轴刻度数量
int yl_num_; // y轴刻度数量
double xmin_; // x轴最小值
double xmax_; // x轴最大值
double ymin_; // y轴最小值
double ymax_; // y轴最大值
double dx_, dy_; // x轴与y轴的刻度间隔
size_t w0_, h0_; // 原点的行与列位置
size_t width_; // 画布宽度
size_t height_; // 画布高度
std::vector<char> sym_; // 画布字符
std::vector<std::string> att_; // 画布字符属性
std::string wname_;
std::string hname_;
};
template <typename FuncOp>
void gctl::cliplot::plot_func(FuncOp func, char s, const std::string &t)
{
for (size_t w = w0_; w < width_; w++)
{
double y, x = xmin_ + (w - w0_)*(xmax_ - xmin_)/(width_ - 1 - w0_);
y = func(x - 0.25*dx_);
int h1 = round(h0_ - 1 - (y - ymin_)*(h0_ - 1)/(ymax_ - ymin_));
y = func(x + 0.25*dx_);
int h2 = round(h0_ - 1 - (y - ymin_)*(h0_ - 1)/(ymax_ - ymin_));
if (h1 > h2) std::swap(h1, h2);
for (size_t h = h1; h <= h2; h++)
set(w, h, s, t);
}
return;
}
};
#endif // _GCTL_CLIPLOT_H