Initial commit with working code

This commit is contained in:
nedko 2022-09-12 17:18:30 +03:00
commit 2c9e137ea3
3 changed files with 105 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
polygon
polygon.exe

2
Makefile Normal file
View File

@ -0,0 +1,2 @@
all:
g++ main.cpp -o polygon

101
main.cpp Normal file
View File

@ -0,0 +1,101 @@
#include <iostream>
#include <string>
#include <math.h>
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 << "<x> <y> <center> <count> <angle> <length>" << 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;
}