

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Area of a polygon with given n ordered vertices in C++
In this program, we have to find the area of a polygon. The coordinates of the vertices of this polygon are given. Before we move further lets brushup old concepts for a better understanding of the concept that follows.
The area is the quantitative representation of the extent of any two-dimensional figure.
Polygon is a closed figure with a given number of sides.
Coordinates of vertices are the value of points in the 2-d plane. For example (0,0).
Now, let's see the mathematical formula for finding the area.
Formula
Area = ½ [(x1y2 + x2y3 + …… + x(n-1)yn + xny1) - (x2y1 + x3y2 + ……. + xny(n-1) + x1yn ) ]
Using this formula the area can be calculated,
Example
#include <iostream> #include <math.h> using namespace std; double areaOfPolygon(double x[], double y[], int n){ double area = 0.0; int j = n - 1; for (int i = 0; i < n; i++){ area += (x[j] + x[i]) * (y[j] - y[i]); j = i; } return abs(area / 2.0); } int main(){ double X[] = {0, 1, 4, 8}; double Y[] = {0, 2, 5, 9}; int n = sizeof(X)/sizeof(X[0]); cout<<"The area is "<<areaOfPolygon(X, Y, n); }
Output
The area is 3.5
- Related Questions & Answers
- Area of a n-sided regular polygon with given Radius?
- Area of a n-sided regular polygon with given Radius in C Program?
- Area of a n-sided regular polygon with given side length in C++
- Area of largest Circle inscribed in N-sided Regular polygon in C Program?
- Area of largest Circle inscribe in N-sided Regular polygon in C Program?
- Program to find area of a polygon in Python
- Check if it is possible to create a polygon with given n sidess in Python
- Apothem of a n-sided regular polygon in C++
- Finding the simple non-isomorphic graphs with n vertices in a graph
- Construct a graph from given degrees of all vertices in C++
- Find the cordinates of the fourth vertex of a rectangle with given 3 vertices in Python
- Count ordered pairs with product less than N in C++
- Minimum height of a triangle with given base and area in C++
- How to fill an area within a polygon in Python using matplotlib?
- Area of hexagon with given diagonal length in C Program?
Advertisements