Question

2. If n points are connected to form a closed polygon as shown below, the area A of the polygon can be computed as n-2 Notice
a. Name your program file lab9p4 b. Do not write any functions using arrays, since we have not completed it yet in class. Ins
0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOLUTION

#include <iostream>

using namespace std;

class ClosedPolygon

{

public:

ClosedPolygon();

~ClosedPolygon();

double area();

friend istream& operator>>(istream& in, ClosedPolygon& poly);

friend ostream& operator<<(ostream& out, const ClosedPolygon& poly);

private:

void resize(int nsize);

double* x;

double* y;

int size;

};

ClosedPolygon::ClosedPolygon()

{

size = 0;

x = NULL;

y = NULL;

}

ClosedPolygon::~ClosedPolygon()

{

delete[] x;

delete[] y;

}

void ClosedPolygon::resize(int nsize)

{

delete[] x;

delete[] y;

x = new double[nsize];

y = new double[nsize];

size = nsize;

}

double ClosedPolygon::area()

{

double result = 0;

for (int i = 0; i < size - 1; i++)

{

result += (x[i+1] + x[i])*(y[i+1] - y[i]);

}

if (result < 0)

result *= -1;

return result / 2;

}

istream& operator>>(istream& in, ClosedPolygon& poly)

{

cout << "Enter number of points: ";

int size;

in >> size;

poly.resize(size);

cout << "Enter x y pair: ";

for (int i = 0; i < size; i++)

in >> poly.x[i] >> poly.y[i];

return in;

}

ostream& operator<<(ostream& out, const ClosedPolygon& poly)

{

out << "x\ty" << endl;

for (int i = 0; i < poly.size; i++)

cout << poly.x[i] << "\t" << poly.y[i] << endl;

return out;

}

int main()

{

ClosedPolygon poly;

cin >> poly;

cout << "The area is: " << poly.area() << endl;

return 0;

}

// sample run

C:Windows system3zicmd.exe Enter number of points? Enter x y pair: 4 0 4 7.5 7.5 4 0 The area is: 25.5 Press any key to conti

Add a comment
Know the answer?
Add Answer to:
2. If n points are connected to form a closed polygon as shown below, the area...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem 2 9.9 Geometry: n-sided regular polygon pg. 362 (25 points) Follow instructions as provided on...

    Problem 2 9.9 Geometry: n-sided regular polygon pg. 362 (25 points) Follow instructions as provided on page 362, reprinted below 9.9 (Geometry: n-sided regular polygon) In an n-sided regular polygon, all sides have the same length and all angles have the same degree (i.e., the polygon is both equilateral and equiangular) Design a class named RegularPolygon that contains A private int data field named n that defines the number of sides in the polygon with default value of 3 A...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT