/****************************************************/
/*                                                  */
/*   CSCI 121: Principles of Computer Programming   */
/*   Programming Project #6                         */
/*   Due Friday, February 25                        */
/*   Programmed by Ian Giese                        */
/*                                                  */
/****************************************************/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>

using namespace std;

double distance(double xPoint1, double xPoint2, double yPoint1, double yPoint2)
{
       double distance;
       distance = sqrt(pow(xPoint2 - xPoint1, 2) + pow(yPoint2 - yPoint1, 2));
       return distance;
}

double semiPerimeter(double D1, double D2, double D3)
{
       double semiPer;
       semiPer = ((D1 + D2 + D3) / 2);
       return semiPer;
}

double area(double D1, double D2, double D3)
{
       double area, semiPer;
       semiPer = semiPerimeter(D1, D2, D3);
       area = sqrt(semiPer * (semiPer - D1) * (semiPer - D2) * (semiPer - D3));
       return area;
}

int main()
{
    //declare all variables
    char description[41], again;
    int numPoints, count;
    double Ax, Ay, Bx, By, D1, D2, Cx, Cy, D3, polyArea, triArea;
    ofstream output("polygonout.txt");
    
    do
    {
        polyArea = 0;
        
        //Input the polygon description and number of points
        cout << "Polygon Description: ";
        cin >> ws;
        cin.getline(description, 40);
        cout << "Number of points in polygon: ";
        cin >> numPoints;
        
        //Input the first two points
        cout << "First point (x): ";
        cin >> Ax;
        cout << "First point (y): ";
        cin >> Ay;
        cout << "Second point (x): ";
        cin >> Bx;
        cout << "Second point (y): ";
        cin >> By;
        
        //Set up output to file
        output << "Polygon with description " << description << endl
               << "Polygon contains " << numPoints << " vertices:" << endl
               << "(" << fixed << showpoint << setprecision(1) << setw(5)
               << Ax << "," << setw(5) << Ay << ")  (" << setw(5) << Bx
               << "," << setw(5) << By << ")  ";
               
        numPoints -= 2;
        count = 0;
        D1 = abs(distance(Ax, Bx, Ay, By));
        
        //Main program loop
        do
        {
               cout << "Next point (x): ";
               cin >> Cx;
               cout << "Next point (y): ";
               cin >> Cy;
               D2 = abs(distance(Ax, Cx, Ay, Cy));
               D3 = abs(distance(Cx, Bx, Cy, By));
               triArea = area(D1, D2, D3);
               polyArea += triArea;
               if (count == 3)
                  output << endl;
               output << "(" << setw(5) << Cx << "," << setw(5) << Cy << ")  ";
               count++;
               Bx = Cx;
               By = Cy;
               D1 = D2;
               numPoints--;
        }
        while(numPoints > 0);
        
        //Output polygon summary
        output << endl <<"Area of the polygon is " << setprecision(2) 
               << polyArea << " square units." << endl << endl;
               
        cout << "Any more polygons (Y/N)? ";
        cin >> again;
    }
    while(toupper(again) == 'Y');

return 0;
}

