Code:
#include <iostream>
#include <string>
using namespace std;
class Point
{
private:
int x;
int y;
public:
Point();
Point(int takeX, int takeY);
void Set(int pntX, int pntY);
int GetX();
int GetY();
void DisplayPoint();
};
class Polygon
{
private:
int num_points;
static const int MAX_POINTS = 100;
string points[MAX_POINTS];
public:
Polygon();
Polygon(int polySize, int xArray[], int yArray[]);
void SetPoints(int polySize, int xArray[], int yArray[]);
string ShowPolygon();
void AddPoint(int x, int y);
void AddPoint(Point p);
void ClearPoly();
};
int main()
{
int coord1 = 83, coord2 = 25, coord3 = 27, coord4 = 85;
int groupNum = 6;
int groupX[] = {68,17,54,98,16,34};
int groupY[] = {38,15,84,56,68,84};
int x_values[] = {34,97,73,95,82,35,79,46,28,15};
int y_values[] = {35,94,67,15,94,68,64,26,68,18};
int coord5 = 37, coord6 = 15, coord7 = 67;
int coord8 = 92, coord9 = 73, coord10 = 93;
Point pointObj1(4,5);
pointObj1.DisplayPoint();
pointObj1.Set(coord1, coord2);
pointObj1.DisplayPoint();
Polygon poly1(10, x_values, y_values);
Polygon poly2;
poly2.SetPoints(groupNum, groupX, groupY);
cout << poly1.ShowPolygon() << endl;
cout << poly2.ShowPolygon() << endl;
poly2.AddPoint(pointObj1);
pointObj1.Set(coord3, coord4);
poly1.AddPoint(pointObj1.GetX(), pointObj1.GetY());
cout << poly1.ShowPolygon() << endl;
cout << poly2.ShowPolygon() << endl;
poly1.ClearPoly();
poly2.ClearPoly();
Point pointObj2(coord5, coord6);
Point pointObj3(coord7, coord8);
Point pointObj4(coord9, coord10);
poly1.AddPoint(pointObj2);
poly1.AddPoint(pointObj3);
poly1.AddPoint(pointObj4);
pointObj2.Set(x_values[0], x_values[1]);
pointObj3.Set(x_values[2], x_values[3]);
pointObj4.Set(x_values[4], x_values[5]);
poly2.AddPoint(pointObj2.GetX(), pointObj2.GetY());
poly2.AddPoint(pointObj3.GetX(), pointObj3.GetY());
poly2.AddPoint(pointObj4.GetX(), pointObj4.GetY());
cout << poly1.ShowPolygon() << endl;
cout << poly2.ShowPolygon() << endl;
return 0;
}
//------------------point methods--------------
Point::Point()
{
x = 0;
y = 0;
}
Point::Point(int takeX, int takeY)
{
Set(takeX, takeY);
}
void Point::Set(int pntX, int pntY)
{
if (pntX > 0 || pntX < 400)
{
x = pntX;
}
else
cout << "x is not within resolution boundries" << endl;
if (pntY > 0 || pntY < 400)
{
y = pntY;
}
else
cout << "y is not within resolution boundries" << endl;
}
int Point::GetX()
{
return x;
}
int Point::GetY()
{
return y;
}
void Point::DisplayPoint()
{
cout << "(" << x << "," << y << ")" << endl;
}
//-----------polygon methods------------------
Polygon::Polygon()
{
num_points = 0;
}
Polygon::Polygon(int polySize, int xArray[], int yArray[])//how to pass arrays int methods
{
SetPoints(polySize, xArray, yArray);
}
void Polygon::SetPoints(int polySize, int xArray[], int yArray[])
{
cout << "points Xarray is now " << xArray[0] << " " << xArray[1] << " " << xArray[2] << " " << xArray[3] << " " << endl;
cout << "points Yarray is now " << yArray[0] << " " << yArray[1] << " " << yArray[2] << " " << yArray[3] << " " << endl;
int xValue = sizeof(xArray), yValue = sizeof(yArray); // get array size
if (polySize < 0 && polySize >= MAX_POINTS)
cout << "polygon size is not within bounds" << endl;
else if (xValue > 50 || yValue > 50)
cout << "x array or y array is too large" << endl;
//else if (xValue != yValue || polySize != xValue || polySize != yValue)
//cout << "array size or array values do not agree" << endl;
else if (polySize % 2 != 0 || xValue % 2 != 0 || yValue % 2 != 0)
cout << "array size or array values are not even" << endl;
else
{
num_points = polySize;
int j = 0;
for (int h = 0;h < num_points;h++)
{
cout << "NUMPOINTS is: " << num_points << endl;
cout << "Xarray in for is now " << xArray[h] << endl;
points[j] = xArray[h];
points[j + 1] = yArray[h];
cout << "points[] in for is now " << points[j] << endl;
j += 2;
}
cout << "points_array is now " << points[0] << " " << points[1] << " " << points[2] << " " << points << " " << endl;
}
}
string Polygon::ShowPolygon()
{
string displayPoly;
for (int h = 0;h < num_points;h += 2)
{
displayPoly += "(" + points[h] + "," + points[h + 1] + ")";
if (h < num_points)
displayPoly += ",";
}
return displayPoly;
}
void Polygon::AddPoint(int x, int y)
{
points[num_points] = x;
points[num_points + 1] = y;
num_points += 2;
}
void Polygon::AddPoint(Point p)
{
points[num_points] = p.GetX();
points[num_points + 1] = p.GetY();
num_points += 2;
}
void Polygon::ClearPoly()
{
num_points = 0;
}