
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- Area of a n-sided regular polygon with given Radius?
- Area of a n-sided regular polygon with given side length in C++
- Area of a n-sided regular polygon with given Radius in C Program?
- Check if it is possible to create a polygon with given n sidess in Python
- Area of largest Circle inscribe in N-sided Regular polygon in C Program?
- Area of largest Circle inscribed in N-sided Regular polygon in C Program?
- Program to find area of a polygon in Python
- Count ordered pairs with product less than N in C++
- Apothem of a n-sided regular polygon in C++
- Finding the simple non-isomorphic graphs with n vertices in a graph
- Given here are some figures.Classify each of them on the basis of the following.(a) Simple curve(b) Simple closed curve(c) Polygon(d) Convex polygon(e) Concave polygon."\n
- Tick the correct answer:\nNumber of non overlapping Triangle we can make in a n-gon ( polygon having n\n sides) , by joining the vertices is \na. n-1\nb. n-2\nc. n-3\nd. n+2
- Find the cordinates of the fourth vertex of a rectangle with given 3 vertices in Python
- Check if it is possible to create a polygon with a given angle in Python
- Minimum height of a triangle with given base and area in C++

Advertisements