Advertisement
bwukki

Untitled

Mar 15th, 2018
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. class Point {
  8. public:
  9.     double x{};
  10.     double y{};
  11. private:
  12.  
  13. };
  14.  
  15. class Polygon {
  16. public:
  17.     void addPoint(Point input) {
  18.         Points.push_back(input);
  19.     }
  20.     Point getPoint(int index) const {
  21.         if (index >= 0) {
  22.             return Points[index];
  23.         }
  24.     }
  25.     size_t size() const {
  26.         return Points.size();
  27.     }
  28.     double area() const {
  29.         double area{};
  30.         double avg{};
  31.         double dif{};
  32.         int i{};
  33.         while (i < Points.size()/2){ //for each pair of points, iterate over and add appropriate amount to the area (so if there are 6 points this loop will run 3 times)
  34.             avg = (Points[i].y + Points[i+1].y)/2;
  35.             dif = Points[i+1].x-Points[i].x;
  36.             area = area + avg*dif;
  37.             i++;
  38.         }
  39.         avg = (Points[Points.size()-1].y + Points[0].y)/2; //pair the first and last points and repeat the process one final time
  40.         dif = Points[Points.size()-1].x - Points[0].x;
  41.         area = area + avg*dif;
  42.         return area;
  43.  
  44.     }
  45.     void printPoints()const {
  46.         for (Point i : Points ) {
  47.             cout << "x = " << i.x << " y = " << i.y << endl;
  48.         }
  49.     }
  50. private:
  51.     vector<Point> Points{};
  52. };
  53.  
  54.  
  55. int main () {
  56.     Polygon porygon{};
  57.     int numInputs{};
  58.     double pointX{};
  59.     double pointY{};
  60.     Point input{};
  61.     cin >> numInputs;
  62.     int i{};
  63.     while (i < numInputs) {
  64.         cin >> input.x;
  65.         cin >> input.y;
  66.         porygon.addPoint(input);
  67.         i++;
  68.     }
  69.     //porygon.printPoints();
  70.     cout << porygon.area();
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement