Finding Center point of a polygon using triangles' center

I created a program that finds the center point of triangles but when I use it on things other than just a triangle the outputs are wrong.

class Polygon
{
private:
int NumVertices; //hold the number of vertices
Point *pPtr; //a Point pointer holding vertices array. memory is dynamically allocated
public:
Polygon(){ //default constructor
NumVertices = 0;
pPtr = 0; //initialize the pointer to null
};

//mutators
void setNumOfPoints(int); //setting the number of points and also dynamically allocate memory for the array to hold point data
void setPoint(int i,Point p); //set single point. i is from 0 to NumVertices-1

//getters
int getNumOfPoints() const{
return NumVertices;
}
Point getPoint(int index) const{
//index: the position of the vertex, starting from 0 to NumVertices-1
return pPtr[index];
}
double getArea() const; //get the area of the polygon
Point getGeometricCenter() const; //get the geometric center point of the polygon

//destructor
~Polygon(){
if (NumVertices != 0){
delete [] pPtr; //delete the memory allocated when the object is destroyed
}
}


};
void Polygon::setNumOfPoints(int noPts){
//noPts: number of vertices of the polygon

NumVertices = noPts; //simple statment
pPtr = new Point[noPts]; //point pointer array
}

void Polygon::setPoint(int index,Point p){
pPtr[index] = p; //pointer index
}

Point Polygon::getGeometricCenter() const{

//Having problems with calculations.
Point pt1, pt2 = {0,0};
Triangle tri1;

for(int i = 2; i < NumVertices; i++) //starts at two because I want i to be the third point in the array
{
tri1.setPoints(pPtr[0], pPtr[i-1], pPtr[i]);
pt2 = tri1.getCenterPoint(); //getting centerpoint and weighting it
pt1.x += pt2.x/(NumVertices-2);
pt1.y += pt2.y/(NumVertices-2);
}

return pt1;

}
class Triangle
{
private:
Point _p1, _p2, _p3;
public:
Triangle(){}; //default constructor, doing nothing
Triangle(Point p1,Point p2,Point p3){ //constructor by passing in three points
setPoints(p1,p2,p3);
}

//mutator
void setPoints(Point p1,Point p2,Point p3); //set points by passing in three points

//getter
double getArea() const;
Point getCenterPoint() const;
Point getPoint(int) const;

//destructor
~Triangle(){}; //doing nothing
};

Point Triangle::getCenterPoint() const{


//Creating temporary points before returning them
double centerX, centerY;
centerX = (_p1.x + _p2.x + _p3.x)/3.0;
centerY = (_p1.y + _p2.y + _p3.y)/3.0;
//Point tempPoint = {centerX, centerY};
//return tempPoint;
return {centerX, centerY};
}

Topic archived. No new replies allowed.