osmanip
Library with useful output stream tools like: color and style manipulators, progress bars and terminal graphics.
plot_2D.hpp
Go to the documentation of this file.
1 //====================================================
2 // File data
3 //====================================================
11 //====================================================
12 // Preprocessor settings
13 //====================================================
14 #pragma once
15 #ifndef OSMANIP_GRAPHICS_PLOT2DCANVAS_HPP
16 #define OSMANIP_GRAPHICS_PLOT2DCANVAS_HPP
17 
18 //====================================================
19 // Headers
20 //====================================================
21 
22 // My headers
24 
25 // STD headers
26 #include <cmath>
27 #include <cstdint>
28 #include <functional>
29 #include <string>
30 
31 namespace osm {
32 
33  //====================================================
34  // Plot2DCanvas
35  //====================================================
47  class Plot2DCanvas : public Canvas {
48  public:
49 
50  // Constructors
51  explicit Plot2DCanvas(uint32_t w, uint32_t h);
52 
53  // Setters
54  void setOffset(float xOff, float yOff);
55  void setScale(float xScale, float yScale);
56 
57  // Getters
58  float getOffsetX() const;
59  float getOffsetY() const;
60  float getScaleX() const;
61  float getScaleY() const;
62 
63  // Draw
75  template <typename Y, typename X>
76  inline void draw(std::function<Y(X)> function, char c, std::string_view feat = "") { //
77 
78  // Find correct boundaries for the loop
79  uint32_t x_min = std::max(0, static_cast<int>(std::floor((0 - offset_x_) / scale_x_)));
80  uint32_t x_max = std::min(width_, static_cast<uint32_t>(std::ceil((width_ - offset_x_) / scale_x_)));
81 
82  // Loop over the computed boundaries and draw
83  for (uint32_t x = x_min; x < x_max; x++) {
84  float real_x = offset_x_ + x * scale_x_;
85  Y real_y = function(real_x);
86  uint32_t y = static_cast<uint32_t>((real_y - offset_y_) / scale_y_);
87  if (y > 0 && y < height_) put(x, y, c, feat);
88  }
89  }
90 
91  private:
92 
93  // Members
94  float offset_x_, offset_y_, scale_x_, scale_y_;
95  };
96 } // namespace osm
97 
98 #endif
Instances of this class are used to draw in a limited 2D space. All the functions that modify the can...
Definition: canvas.hpp:52
uint32_t width_
Definition: canvas.hpp:100
uint32_t height_
Definition: canvas.hpp:100
void put(uint32_t x, uint32_t y, char c, std::string_view feat="")
Put a character in the canvas, given its coordinates and an optional feat. An out-of-bounds exception...
Definition: canvas.cpp:201
This class is used to plot mathematical functions R -> R. In addition to all the properties of a basi...
Definition: plot_2D.hpp:47
float getOffsetX() const
Get the offset_x of the canvas.
Definition: plot_2D.cpp:78
void setOffset(float xOff, float yOff)
Set the offset_x and offset_y of the canvas.
Definition: plot_2D.cpp:51
float getOffsetY() const
Get the offset_y of the canvas.
Definition: plot_2D.cpp:86
float getScaleY() const
Get the scale_y of the canvas.
Definition: plot_2D.cpp:102
void setScale(float xScale, float yScale)
Set the scale_x and scale_y of the canvas.
Definition: plot_2D.cpp:63
Plot2DCanvas(uint32_t w, uint32_t h)
Construct a new Plot2D:: Plot2D object. The same as its parent, the constructor requires the dimensio...
Definition: plot_2D.cpp:37
float getScaleX() const
Get the scale_x of the canvas.
Definition: plot_2D.cpp:94
void draw(std::function< Y(X)> function, char c, std::string_view feat="")
Plot a function that receives an argument of a numeric type X and returns a numeric value of type Y....
Definition: plot_2D.hpp:76
Definition: canvas.cpp:30
const std::string & feat(const std::unordered_map< std::string, std::string > &generic_map, const std::string &feat_string)
It takes an std::map object as the first argument and an std::string object (map key) as the second a...
Definition: common.cpp:41