Root Finder  1.0
Using various numerical methods
Loading...
Searching...
No Matches
plotting.cpp
Go to the documentation of this file.
1/*
2@Author: Gilbert Young
3@Time: 2024/09/19 01:47
4@File_name: plotting.cpp
5@IDE: VSCode
6@Formatter: Clang-Format
7@Description: Definition of function to plot f(x) on a grid.
8*/
9
10#include "plotting.h"
11#include "functions.h"
12#include <iostream>
13#include <vector>
14#include <string>
15#include <cmath>
16#include <iomanip>
17#include <sstream>
18
19// Function to plot f(x) on a grid
20void plot_function(long double x_min, long double x_max, long double y_min, long double y_max,
21 int width, int height, long double label_interval)
22{
23 std::vector<std::string> grid(height, std::string(width, ' '));
24 int x_axis = -1, y_axis = -1;
25
26 // Determine x-axis position
27 if (y_min <= 0 && y_max >= 0)
28 {
29 x_axis = static_cast<int>(round((0 - y_min) / (y_max - y_min) * (height - 1)));
30 }
31
32 // Determine y-axis position
33 if (x_min <= 0 && x_max >= 0)
34 {
35 y_axis = static_cast<int>(round((0 - x_min) / (x_max - x_min) * (width - 1)));
36 }
37
38 // Plot the function
39 for (int i = 0; i < width; ++i)
40 {
41 long double x = x_min + i * (x_max - x_min) / (width - 1);
42 long double y = f(x);
43 if (y < y_min || y > y_max)
44 continue;
45 int j = static_cast<int>(round((y - y_min) / (y_max - y_min) * (height - 1)));
46 if (j >= 0 && j < height)
47 {
48 grid[height - 1 - j][i] = '*';
49 }
50 }
51
52 // Draw x-axis
53 if (x_axis != -1)
54 {
55 for (int i = 0; i < width; ++i)
56 {
57 if (grid[x_axis][i] == ' ')
58 grid[x_axis][i] = '-';
59 }
60 }
61
62 // Draw y-axis
63 if (y_axis != -1)
64 {
65 for (int i = 0; i < height; ++i)
66 {
67 if (grid[i][y_axis] == ' ')
68 grid[i][y_axis] = '|';
69 }
70 }
71
72 // Draw origin
73 if (x_axis != -1 && y_axis != -1)
74 {
75 grid[x_axis][y_axis] = '+';
76 }
77
78 // Print the grid
79 std::cout << "\nFunction Plot:\n";
80 for (const auto &row : grid)
81 {
82 std::cout << row << '\n';
83 }
84
85 // Print x-axis labels
86 std::string label_line(width, ' ');
87 for (int label = static_cast<int>(ceil(x_min / label_interval)) * static_cast<int>(label_interval);
88 label <= static_cast<int>(floor(x_max / label_interval)) * static_cast<int>(label_interval);
89 label += static_cast<int>(label_interval))
90 {
91 double relative_pos = (static_cast<double>(label) - x_min) / (x_max - x_min);
92 int pos = static_cast<int>(round(relative_pos * (width - 1)));
93 std::ostringstream oss_label;
94 oss_label << std::fixed << std::setprecision(0) << label;
95 std::string label_str = oss_label.str();
96 int start_pos = pos - static_cast<int>(label_str.length() / 2);
97 if (start_pos < 0)
98 start_pos = 0;
99 if (start_pos + static_cast<int>(label_str.length()) > width)
100 continue;
101 for (size_t i = 0; i < label_str.length(); ++i)
102 {
103 label_line[start_pos + i] = label_str[i];
104 }
105 }
106 std::cout << label_line << std::endl;
107 std::cout << "x range: [" << x_min << ", " << x_max << "]\n";
108 std::cout << "y range: [" << y_min << ", " << y_max << "]\n\n";
109}
long double f(long double x)
Definition functions.cpp:13
void plot_function(long double x_min, long double x_max, long double y_min, long double y_max, int width, int height, long double label_interval)
Definition plotting.cpp:20