在这个问题中,我们得到了多边形的坐标。我们的任务是创建一个程序来检查给定的多边形是否相等。
相等形状 是其周长等于该形状的面积的形状。
输入: polygon [] [] = {{0,0},{5,7},{2,0}}
输出: 不相等
解释:
周长= 18.21
面积= 7
解决该问题的方法是找到形状的面积和周长,然后将两者进行比较以检查天气是否为给定形状。
使用坐标查找周长很简单。我们只需要使用坐标来找到长度并找到周长,
周长=第1边+第2边+第3边
要使用坐标查找区域,可以使用公式,
面积= 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 )}
#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<<"给定形状的面积为 "<<area<<endl;
cout<<"给定形状的周长为 "<<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;
}给定形状的面积为 7 给定形状的周长为 18 The given shape is not an equable shape