Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- class Point {
- public:
- double x{};
- double y{};
- private:
- };
- class Polygon {
- public:
- void addPoint(Point input) {
- Points.push_back(input);
- }
- Point getPoint(int index) const {
- if (index >= 0) {
- return Points[index];
- }
- }
- size_t size() const {
- return Points.size();
- }
- double area() const {
- double area{};
- double avg{};
- double dif{};
- int i{};
- 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)
- avg = (Points[i].y + Points[i+1].y)/2;
- dif = Points[i+1].x-Points[i].x;
- area = area + avg*dif;
- i++;
- }
- avg = (Points[Points.size()-1].y + Points[0].y)/2; //pair the first and last points and repeat the process one final time
- dif = Points[Points.size()-1].x - Points[0].x;
- area = area + avg*dif;
- return area;
- }
- void printPoints()const {
- for (Point i : Points ) {
- cout << "x = " << i.x << " y = " << i.y << endl;
- }
- }
- private:
- vector<Point> Points{};
- };
- int main () {
- Polygon porygon{};
- int numInputs{};
- double pointX{};
- double pointY{};
- Point input{};
- cin >> numInputs;
- int i{};
- while (i < numInputs) {
- cin >> input.x;
- cin >> input.y;
- porygon.addPoint(input);
- i++;
- }
- //porygon.printPoints();
- cout << porygon.area();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement