1. Engineering
  2. Computer Science
  3. help finish code by inserting code to compute area and...

Question: help finish code by inserting code to compute area and...

Question details

Help finish code by inserting code to compute area and perimeter of the polygon.

Implement into the class Polygon

double perimeter();

double area();

CODE:
#include<iostream>
#include<cstdlib>
#include<cmath>

using namespace std;

class Vertex{
public:

Vertex( int x1, int y1, Vertex * n = NULL, Vertex * p = NULL) {
xCoord =x1; yCoord = y1;
prev = p; next =n;
}

int getX(){
return xCoord;
}

int getY(){
return yCoord;
}

void setY(int y){
yCoord=y;
}
private:
int xCoord, yCoord;
Vertex *next, *prev;
friend class Polygon;//have access to private memebers
}; //vertex

class Polygon{

public:
Polygon(){
header=NULL; currPtr=NULL;
}
bool isEmpty(){
return (header==NULL);
}
void reset(){
currPtr=header;
}

void forward(){
if (currPtr) currPtr= currPtr->next;
}
void backward(){
if(currPtr) currPtr=currPtr->prev;
}
void insertAfter(int x, int y);
void readPolygon();
void printPolygon();

double perimeter();

double area();

private:
Vertex *header;
Vertex *currPtr;
};

void Polygon::readPolygon(){//reads first number, gets x and y
int x1,y1,n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>x1>>y1;
insertAfter(x1,y1);
forward();
}
}
void Polygon::printPolygon(){
if (isEmpty()){
cout<<"empty List "<< endl;
return;
}
Vertex *ptr=header;   
cout<< " ("<< (ptr->getX())<< ","<<(ptr->getY())<< ")";
ptr=ptr->next;

while(ptr != header){
cout<< " (" << (ptr->getX()) << ","<< (ptr->getY())<< ")";
ptr= ptr->next;
}
cout<<endl;

}

void Polygon::insertAfter(int x, int y) {
Vertex *ptr=new Vertex(x,y,NULL,NULL);
if (isEmpty()) {
ptr->prev=ptr; ptr->next=ptr;
currPtr= ptr; header=ptr;
return;
}

else{
ptr->prev=currPtr; ptr->next=currPtr->next;
currPtr->next->prev=ptr; currPtr->next=ptr;
}
}

int main(){
Polygon L1;
L1.readPolygon();

cout<< endl<< "Coordinates of the Vertices: " << endl;
L1.printPolygon();
return 0;
}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution