A shape is equable if its area is equal to its perimeter. Given ordered coordinates of polygon find whether the shape is equable or not.
Examples :
Input : X[] = {0, 5, 0}
Y[] = {0, 0, 12}
Output : Equable Shape
Input : X[] = {0, 4, 4, 0}
Y[] = {0, 0, 4, 4}
Output : Equable Shape
Input: X[] = {0, 6, 6, 0}
Y[] = {0, 0, 4, 4}
Output: Not Equable Shape
We can find area of polygon using shoelace formula which is described in Area of a polygon with given n ordered vertices. We can also find its perimeter simply by adding distances between adjacent points.
C++
#include <bits/stdc++.h>
using namespace std;
double polygonArea( 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);
}
double polygonPerimeter( double X[], double Y[],
int n)
{
double perimeter = 0.0;
int j = n - 1;
for ( int i = 0; i < n; i++) {
perimeter += sqrt ((X[j] - X[i]) * (X[j] - X[i]) +
(Y[j] - Y[i]) * (Y[j] - Y[i]));
j = i;
}
return perimeter;
}
void equableShape( double X[], double Y[], int n)
{
if (polygonPerimeter(X, Y, n) == polygonArea(X, Y, n))
cout << "Equable Shape" ;
else
cout << "Not Equable Shape" ;
}
int main()
{
double X[] = { 0, 5, 0 };
double Y[] = { 0, 0, 12 };
int n = sizeof (X) / sizeof (X[0]);
equableShape(X, Y, n);
return 0;
}
|
Java
class equable {
static double polygonArea( 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 Math.abs(area / 2.0 );
}
static double polygonPerimeter( double X[], double Y[], int n)
{
double perimeter = 0.0 ;
int j = n - 1 ;
for ( int i = 0 ; i < n; i++) {
perimeter += Math.sqrt((X[j] - X[i]) * (X[j] - X[i]) +
(Y[j] - Y[i]) * (Y[j] - Y[i]));
j = i;
}
return perimeter;
}
static void equableShape( double X[], double Y[], int n)
{
if (polygonPerimeter(X, Y, n) == polygonArea(X, Y, n))
System.out.println( "Equable Shape" );
else
System.out.println( "Not Equable Shape" );
}
public static void main(String[] args)
{
double X[] = { 0 , 5 , 0 };
double Y[] = { 0 , 0 , 12 };
int n = X.length;
equableShape(X, Y, n);
}
}
|
Python3
import math
def polygonArea(X, Y, n):
area = 0.0
j = n - 1
for i in range (n):
area + = (X[j] + X[i]) * (Y[j] - Y[i])
j = i
return abs (area / 2.0 )
def polygonPerimeter(X, Y, n):
perimeter = 0.0
j = n - 1
for i in range (n):
perimeter + = math.sqrt((X[j] - X[i]) * (X[j] - X[i]) +
(Y[j] - Y[i]) * (Y[j] - Y[i]))
j = i
return perimeter
def equableShape(X, Y, n):
if (polygonPerimeter(X, Y, n) = = polygonArea(X, Y, n)):
print ( "Equable Shape" )
else :
print ( "Not Equable Shape" )
X = [ 0 , 5 , 0 ]
Y = [ 0 , 0 , 12 ]
n = len (X)
equableShape(X, Y, n)
|
C#
using System;
class equable {
static double polygonArea( 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 Math.Abs(area / 2.0);
}
static double polygonPerimeter( double []X,
double []Y,
int n)
{
double perimeter = 0.0;
int j = n - 1;
for ( int i = 0; i < n; i++) {
perimeter += Math.Sqrt((X[j] - X[i]) *
(X[j] - X[i]) +
(Y[j] - Y[i]) *
(Y[j] - Y[i]));
j = i;
}
return perimeter;
}
static void equableShape( double []X,
double []Y,
int n)
{
if (polygonPerimeter(X, Y, n) ==
polygonArea(X, Y, n))
Console.WriteLine( "Equable Shape" );
else
Console.WriteLine( "Not Equable Shape" );
}
public static void Main(String []args)
{
double []X = {0, 5, 0};
double []Y = {0, 0, 12};
int n = X.Length;
equableShape(X, Y, n);
}
}
|
PHP
<?php
function polygonArea( $X , $Y , $n )
{
$area = 0.0;
$j = $n - 1;
for ( $i = 0; $i < $n ; $i ++)
{
$area += ( $X [ $j ] + $X [ $i ]) *
( $Y [ $j ] - $Y [ $i ]);
$j = $i ;
}
return abs ( $area / 2.0);
}
function polygonPerimeter( $X , $Y , $n )
{
$perimeter = 0.0;
$j = $n - 1;
for ( $i = 0; $i < $n ; $i ++)
{
$perimeter += sqrt(( $X [ $j ] - $X [ $i ]) *
( $X [ $j ] - $X [ $i ]) +
( $Y [ $j ] - $Y [ $i ]) *
( $Y [ $j ] - $Y [ $i ]));
$j = $i ;
}
return $perimeter ;
}
function equableShape( $X , $Y , $n )
{
if (polygonPerimeter( $X , $Y , $n ) ==
polygonArea( $X , $Y , $n ))
echo "Equable Shape" ;
else
echo "Not Equable Shape" ;
}
$X = array ( 0, 5, 0 );
$Y = array ( 0, 0, 12 );
$n = sizeof( $X );
equableShape( $X , $Y , $n );
?>
|
Javascript
<script>
function polygonArea(X, Y, n)
{
let area = 0.0;
let j = n - 1;
for (let i = 0; i < n; i++)
{
area += (X[j] + X[i]) * (Y[j] - Y[i]);
j = i;
}
return Math.abs(area / 2.0);
}
function polygonPerimeter(X, Y, n)
{
let perimeter = 0.0;
let j = n - 1;
for (let i = 0; i < n; i++)
{
perimeter += Math.sqrt((X[j] - X[i]) *
(X[j] - X[i]) +
(Y[j] - Y[i]) *
(Y[j] - Y[i]));
j = i;
}
return perimeter;
}
function equableShape(X, Y, n)
{
if (polygonPerimeter(X, Y, n) ==
polygonArea(X, Y, n))
document.write( "Equable Shape" + "</br>" );
else
document.write( "Not Equable Shape" + "</br>" );
}
let X = [ 0, 5, 0 ];
let Y = [ 0, 0, 12 ];
let n = X.length;
equableShape(X, Y, n);
</script>
|
Output :
Equable Shape
Time Complexity: O(NlogN)
Auxiliary Space: O(N)
Reference:
https://en.wikipedia.org/wiki/Equable_shape
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!