# Equable Shapes in C++

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

In this problem, we are given the coordinates of a polygon. Our task is to create a program to check whether the given polygon is equable or not.

Equable Shape is the shape whose perimeter is equal to the area of the shape.

## Let’s take an example to understand the problem,

Input: polygon[][] = {{0, 0}, {5, 7}, {2, 0}}

Output: Not equable

Explanation:

Perimeter = 18.21
Area = 7

## Solution Approach:

The solution to the problem lies in find the area and perimeter of the shape and then compare both of them to check weather the given shape is an equable shape or not.

Finding a perimeter using the coordinates is simple. We simply need to find the length using coordinates and the find the perimeter,

Perimeter = side1 + side2 + side3

To find the area using coordinates is done by using the formula,

Area = 1/2 {(x_1 y_2+ x_2 y_3  + ....x_(n-1) y_n  + x_n y_1 )  - (x_2 y_1  + x_3 y_2  + ....+ x_n y_(n-1)  + x_1 n)}

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;

double findShapeArea(double cord[], int n)
{
double area = 0.0;
int j = n - 1;
for (int i = 0; i < n; i++) {
area += (float)(cord[j] + cord[i]) * (cord[j] - cord[i]);
j = i;
}

return abs(area / 2.0);
}

double findShapeperimeter(double cord[], int n) {

double perimeter = 0.0;
int j = n - 1;
for (int i = 0; i < n; i++) {
perimeter += sqrt((cord[j] - cord[i]) * (cord[j] - cord[i]) + (cord[j] - cord[i]) * (cord[j] - cord[i]));
j = i;
}
return perimeter;
}

int isEquableShape(double cord[], int n)
{
int area = findShapeArea(cord, n);
int peri = findShapeperimeter(cord, n);
cout<<"The area of the given shape is "<<area<<endl;
cout<<"The perimeter of the given shape is "<<peri<<endl;
if (area == peri)
return 1;
else
return 0;
}

int main() {

int n = 3;
double cord[n] = {{0, 0} , {5, 7}, {2, 0}};
if (isEquableShape(cord, n))
cout<<"The given shape is an equable shape";
else
cout<<"The given shape is not an equable shape";
return 0;
}

## Output −

The area of the given shape is 7
The perimeter of the given shape is 18
The given shape is not an equable shape