- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Equable Shapes in C++
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)}
Program to illustrate the working of our solution,
Example
#include <bits/stdc++.h> using namespace std; double findShapeArea(double cord[][2], int n) { double area = 0.0; int j = n - 1; for (int i = 0; i < n; i++) { area += (float)(cord[j][0] + cord[i][0]) * (cord[j][1] - cord[i][1]); j = i; } return abs(area / 2.0); } double findShapeperimeter(double cord[][2], int n) { double perimeter = 0.0; int j = n - 1; for (int i = 0; i < n; i++) { perimeter += sqrt((cord[j][0] - cord[i][0]) * (cord[j][0] - cord[i][0]) + (cord[j][1] - cord[i][1]) * (cord[j][1] - cord[i][1])); j = i; } return perimeter; } int isEquableShape(double cord[][2], 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][2] = {{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