From 2c9e137ea35af80be7acf70b60b9d771faa607b2 Mon Sep 17 00:00:00 2001 From: nedko Date: Mon, 12 Sep 2022 17:18:30 +0300 Subject: [PATCH] Initial commit with working code --- .gitignore | 2 ++ Makefile | 2 ++ main.cpp | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..471d180 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +polygon +polygon.exe diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e1a80ec --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +all: + g++ main.cpp -o polygon diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..e85ba3e --- /dev/null +++ b/main.cpp @@ -0,0 +1,101 @@ +#include +#include + +#include + +using namespace std; + +constexpr int Y_DIR = 1; + +double radians(double degrees) +{ + return degrees * M_PI / 180.0; +} + +void usage(const string& name) +{ + cout << "Usage: " << name << "
" << endl; + cout << "\tx - x coordinate of first point" << endl; + cout << "\ty - y coordinate of first point" << endl; + cout << "\tcenter - 0/1 - whether first point is center of polygon" << endl; + cout << "\tcount - ammount of points" << endl; + cout << "\tangle - rotation in degrees" << endl; + cout << "\tlength - length of side (when not center) - external radius of polygon (when center)" << endl; +} + +void center_polygon(double x, double y, int count, double angle, double radius) +{ + double x_calc; + double y_calc; + + for (int i = 0; i < count; ++i) + { + x_calc = cos(radians((360.0 / count) * i + angle)) * radius + x; + y_calc = sin(radians((360.0 / count) * i + angle)) * radius + y; + + cout << x_calc << "," << y_calc << endl; + } +} + +void corner_polygon(double x, double y, int count, double angle, double length) +{ + double x_calc; + double y_calc; + + x_calc = 0; + y_calc = 0; + + cout << x << "," << y << endl; + + for (int i = 0; i < count - 1; ++i) + { + x_calc = x_calc + cos(radians(angle)) * length; + y_calc = y_calc + sin(radians(angle)) * length; + + cout << x_calc + x << "," << y_calc + y << endl; + + angle += (360.0 / count); + } +} + +int main(int argc, char **argv) +{ + double x; + double y; + bool center; + int count; + double angle; + double length; + + if (argc < 7) + { + usage(argv[0]); + cout << "Insufficient arguments" << endl; + return -1; + } + + x = stod(argv[1]); + y = stod(argv[2]); + center = (0 != stoi(argv[3])); + count = stoi(argv[4]); + angle = stod(argv[5]); + length = stod(argv[6]); + + if (count < 1) + { + usage(argv[0]); + cout << "Wrong ammount of points" << endl; + return -1; + } + + if (center) + { + center_polygon(x, y, count, angle, length); + } + else + { + corner_polygon(x, y, count, angle, length); + } + + return 0; +}