2
#include <iostream> #include <math.h> #include <iomanip> using namespace std; double polygonal_area(float x[],float y[], int n){ double area=0; int j=n-1; for(int i=0; i<n; i++){ area += (x[j]+x[i])*(y[j]-y[i]); j=i; } if(area<0){return (area/-2);} return area/2; } bool input_data(){ int estimate; cout<<"Input a rough estimate of the area\n"; cin>>estimate; estimate=estimate; float numpoints; int numPoints; cout<<"Please input number of points on the polygon\n"; cin>>numpoints; numPoints=numpoints; if(numPoints*numpoints!=numPoints*numPoints){cout<<"Must be Integer!\n"; ret urn 0;} float X[numPoints],Y[numPoints]; for(int i=0; i<numPoints; i++){ cout<<"Please input the X co-ordinate number "<<i+1<<"\n"; cin>>X[i]; cout<<"Please input the Y co-ordinate number "<<i+1<<"\n"; cin>>Y[i]; } double area = polygonal_area(X,Y,numPoints); cout<<"\n\n\n\n"; for(int i=0; i<numPoints; i++){ cout<<"P"<<i+1<<"("<<X[i]<<","<<Y[i]<<") "; if(i==5){cout<<endl;} } cout << fixed << showpoint << setprecision(4); cout<<"\nThe Area is:"<<area<<"\n"; for(int i=0; i<numPoints-1; i++){ cout<<"Dictance between point "<<i+1<<" and "<<i+2<<" is "<<round((sqrt( pow(X[i]-X[i+1],2)+pow(Y[i]-Y[i+1],2)))*100)/100<<" metres\n"; } cout<<"Dictance between point "<<numPoints<<" and "<<1<<" is "<<round((sqrt( pow(X[numPoints-1]-X[0],2)+pow(Y[numPoints-1]-Y[0],2)))*100)/100<<" metres\n"; if(!((estimate<area*1.25)&&(estimate>area*0.75))){ cout<<"*The area is not within a 25% error range of your estimate. Make sure all \npoints were inputted correctly.\n\n\n"; cout<<"Make sure points are in a clockwise or anticlockwise directio n\nfor the entire shape so that you don't create negative area.\n\n\n"; return 0; } return 1; } int main() { bool repeat = 0; while(repeat==0){

Area of Polygon C++

Embed Size (px)

DESCRIPTION

C++ program for calculating the area of a polygon

Citation preview

Page 1: Area of Polygon C++

#include <iostream>#include <math.h>#include <iomanip>using namespace std;double polygonal_area(float x[],float y[], int n){ double area=0; int j=n-1; for(int i=0; i<n; i++){ area += (x[j]+x[i])*(y[j]-y[i]); j=i; } if(area<0){return (area/-2);} return area/2;}bool input_data(){ int estimate; cout<<"Input a rough estimate of the area\n"; cin>>estimate; estimate=estimate; float numpoints; int numPoints; cout<<"Please input number of points on the polygon\n"; cin>>numpoints; numPoints=numpoints; if(numPoints*numpoints!=numPoints*numPoints){cout<<"Must be Integer!\n"; return 0;} float X[numPoints],Y[numPoints]; for(int i=0; i<numPoints; i++){ cout<<"Please input the X co-ordinate number "<<i+1<<"\n"; cin>>X[i]; cout<<"Please input the Y co-ordinate number "<<i+1<<"\n"; cin>>Y[i]; } double area = polygonal_area(X,Y,numPoints); cout<<"\n\n\n\n"; for(int i=0; i<numPoints; i++){ cout<<"P"<<i+1<<"("<<X[i]<<","<<Y[i]<<") "; if(i==5){cout<<endl;} } cout << fixed << showpoint << setprecision(4); cout<<"\nThe Area is:"<<area<<"\n";

for(int i=0; i<numPoints-1; i++){ cout<<"Dictance between point "<<i+1<<" and "<<i+2<<" is "<<round((sqrt(pow(X[i]-X[i+1],2)+pow(Y[i]-Y[i+1],2)))*100)/100<<" metres\n"; } cout<<"Dictance between point "<<numPoints<<" and "<<1<<" is "<<round((sqrt(pow(X[numPoints-1]-X[0],2)+pow(Y[numPoints-1]-Y[0],2)))*100)/100<<" metres\n"; if(!((estimate<area*1.25)&&(estimate>area*0.75))){ cout<<"*The area is not within a 25% error range of your estimate. Make sure all \npoints were inputted correctly.\n\n\n"; cout<<"Make sure points are in a clockwise or anticlockwise direction\nfor the entire shape so that you don't create negative area.\n\n\n"; return 0; } return 1;}int main(){ bool repeat = 0; while(repeat==0){

Page 2: Area of Polygon C++

bool success = 0; while(success==0){ success=input_data(); }

string decision; cout<<"Would you like to go again? Y/N\n"; cin>>decision; if(decision == "Y" | decision == "y" | decision == "yes" | decision == "Yes"){ repeat = 0; } else{ repeat = 1; } } cin.get();}