diff options
| author | Syndamia <kamen.d.mladenov@protonmail.com> | 2020-11-24 17:09:53 +0200 |
|---|---|---|
| committer | Syndamia <kamen.d.mladenov@protonmail.com> | 2020-11-24 17:09:53 +0200 |
| commit | 7f6d3846465cbf0e9ea08b645fe9ad9a85b24ce1 (patch) | |
| tree | 31d6c7fa21540b15806a8fbd8125304575dbee3f | |
| parent | 733af501fa0c1adb4a46dd1d3e843cbb8e84f75e (diff) | |
| download | algorithms-7f6d3846465cbf0e9ea08b645fe9ad9a85b24ce1.tar algorithms-7f6d3846465cbf0e9ea08b645fe9ad9a85b24ce1.tar.gz algorithms-7f6d3846465cbf0e9ea08b645fe9ad9a85b24ce1.zip | |
Added a C++ equation grapher
| -rw-r--r-- | C++/QuadraticEquationGrapher.cpp | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/C++/QuadraticEquationGrapher.cpp b/C++/QuadraticEquationGrapher.cpp new file mode 100644 index 0000000..7dfbe64 --- /dev/null +++ b/C++/QuadraticEquationGrapher.cpp @@ -0,0 +1,63 @@ +#include <iostream> +#include <math.h> + +using namespace std; + +/* Program that graphs a quadratic equation on the standard output */ + +/* Equation precision - how precise the quadratic equation result should be + * - higher values are more precise, but they could not show up on the graph + * - needs to be balanced with step + * Step - size of each unit (character) + */ +const int eqPrec = 1; +const double step = 1; + +const char line = '*'; +const char verLine = '|'; +const char horLine = '-'; +const char empty = ' '; + +double quadr(double x, double a, double b, double c) { + return double( round( ( (a*pow(x, 2)) + (b*x) + c) * eqPrec ) / eqPrec); +} + +void printQadr(int startx, int endx, int starty, int endy, double a, double b, double c) { + double currY, currX; + for (int y = max(starty, endy); y >= min(starty, endy); y--) { + for (int x = startx; x <= endx; x++) { + currY = y * step; + currX = x * step; + + if (quadr(currX, a, b, c) == currY + || ((quadr(currX, a, b, c) < currY + || (quadr(currX - step, a, b, c) == 0 || quadr(currX + step, a, b, c) == 0)) // Wide lines + && (quadr(currX + step, a, b, c) > currY != quadr(currX - step, a, b, c) > currY)) // Long lines + ) { + cout << line; + } else if (x == 0) { + cout << verLine; + } else if (y == 0) { + cout << horLine; + } else { + cout << empty; + } + } + cout << endl; + } +} + +void printQadr(int start, int end, double a, double b, double c) { + printQadr(start, end, start, end, a, b, c); +} + +int main() +{ + double a = 0.2; + double b = 2; + double c = -10; + + printQadr(-25, 15, 20, -15, a, b, c); + + return 0; +} |
