answersLogoWhite

0


Best Answer

#include<iostream>

#include<vector>

#include<assert.h>

// Typdefs to hide unnecessary implementation detail and remove verbosity.

using Coordinates = std::pair<int,int>;

using Polygon = std::vector<Coordinates>;

// Calculate the area of a given polygon.

int area (const Polygon& polygon)

{

// The given polygon must list all vertices in the correct sequence

// either clockwise or anti-clockwise. It does not matter which

// vertex begins the sequence, but the last vertex is assumed to

// join the first.

// Initialise an accumulator.

int accumulator = 0;

// A polygon with less than 3 vertices is no polygon!

if (polygon.size() < 3)

return accumulator;

// The last vertex is the previous vertex to the first vertex.

// We'll deal with this specific pair of vertices first.

size_t previous = polygon.size() - 1;

// Iterate through all vertices in sequence.

for (size_t current=0; current<polygon.size(); ++current)

{

// The previous and current vertices form an edge. We need to calculate

// the area of the imaginary rectangle extending horizontally from this

// edge until it meets the Y-axis. This edge may not be vertical so we

// also extend in the opposite direction by the same amount. That is,

// for vertices {x1, y1} and {x2, y2}, the imaginary rectangle's opposing

// corners will be at imaginary vertices {0, y1}, {x1+x2, y2}. The area

// of this imaginary rectangle is therefore (x1+x2)*(y1-y2).

// Note: the imaginary rectangle's area may be negative but that's OK.

// It'll simply be subtracted from the accumulator and that's exactly

// what we want.

accumulator +=

(polygon[previous].first + polygon[current].first) *

(polygon[previous].second - polygon[current].second);

// The current vertex now becomes the previous vertex

// in readiness for the next iteration.

previous = current;

}

// Remove the sign (make absolute).

accumulator *= (accumulator<0) ? -1 : 1;

// At this point the accumulated total is guaranteed to be an even

// number (as we'll see). But let's play safe and assert that fact.

assert (accumulator % 2 == 0);

// Since each imaginary rectangle was exactly double what we needed

// (because we extended in both directions), divide the accumulated

// total by 2. It's more efficient to do that here, once, rather

// than for each individual rectangle. We don't have to worry about

// fractions since the accumulator is guaranteed to be even.

return accumulator / 2;

}

// Driver to test the function.

int main()

{

Polygon square;

square.push_back (Coordinates (0, 0));

square.push_back (Coordinates (0, 10));

square.push_back (Coordinates (10, 10));

square.push_back (Coordinates (10, 0));

assert (area (square) == 100);

std::cout << "Square (10 x 10) has area " << area (square) << std::endl;

Polygon triangle;

triangle.push_back (Coordinates (0, 0));

triangle.push_back (Coordinates (0, 6));

triangle.push_back (Coordinates (8, 0));

assert (area (triangle) == 24);

std::cout << "Right-angled triangle (width 8, height 6) has area " << area (triangle) << std::endl;

Polygon irregular;

irregular.push_back (Coordinates (0, 0));

irregular.push_back (Coordinates (0, 14));

irregular.push_back (Coordinates (8, 14));

irregular.push_back (Coordinates (8, 4));

irregular.push_back (Coordinates (12, 4));

irregular.push_back (Coordinates (12, 0));

assert (area (irregular) == 128);

std::cout << "6-sided irregular polygon has area " << area (irregular) << std::endl;

Polygon line;

line.push_back (Coordinates (0, 0));

line.push_back (Coordinates (0, 4));

line.push_back (Coordinates (0, 8));

assert (area (line) == 0);

std::cout << "3 points on a line of length 8 has area " << area (line) << std::endl;

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the program to find the area of polygons using c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you find the area of a square using the radius and the apothem?

For a SQUARE, the area is (2r)2 because the length and width are the same. The apothem (radius) is used to find the area of other regular polygons.


How do I find the area of irregular shaped polygons?

For instance, you might divide the polygons into triangles, calculate the area of each triangle, and then add everything up.


How do you find the Perimeter of a polygon with only the area?

Different polygons have different relationships between perimeter and area. For example, if we assume regular polygons, an equilateral triangle and a square have different perimeters for the same area. If you allow irregular polygons, the variety is even bigger.


How do you find an area of a shape rectangle square and a circle in C program?

By using that one thing.


Does it matter which way you divide a complex figure int simple polygons to find the area?

no


For perimeter of a polygon do you times the sides?

For the perimeter of a polygon you add the sides to find the total distance around the shape. For area, you multiply using the various formulas for different polygons.


How do you find the area of the shaded region in a rectangle?

You will need to divide the shaded area into smaller parts, such as triangles or rectangles, or find the length of sides of these polygons.


What is the equasion to find the area of a rectangle?

in order to find the area of a rectangle you must multiply the base of the rectangle by its height. This is also the same for most polygons


Program to find area of circle using class in c?

class area{ int l,w,a; a=i*w; printf"area of rectangle is= %d",a; endl; }


How can you find the area of a polygon inside another polygon?

More information about the two polygons is required.


What is the use of pick's theorem?

The use of Pick's Theorem is to find the area of polygons when they are located on a lattice grid.


How would you find the area of the remaining figure?

To find the area of the remaining figure, subtract the area of the removed portion from the total area of the original figure.