给定具有 n 个顶点的多边形的有序坐标。求多边形的面积。这里的有序意味着坐标以顺时针方式或逆时针方式从第一个顶点到最后一个顶点给出。
例子 :
Input : X[] = {0, 4, 4, 0}, Y[] = {0, 0, 4, 4};
Output : 16
Input : X[] = {0, 4, 2}, Y[] = {0, 0, 4}
Output : 8
我们可以使用鞋带公式计算多边形的面积。
Area
= | 1/2 [ (x1y2 + x2y3 + … + xn-1yn + xny1) –
(x2y1 + x3y2 + … + xnyn-1 + x1yn) ] |
上面的公式是通过对顶点的叉积求得多边形中形成的三角形的面积来推导出来的。
下面是上述公式的一个实现。
CPP
// C++ program to evaluate area of a polygon using
// shoelace formula
#include
using namespace std;
// (X[i], Y[i]) are coordinates of i'th point.
double polygonArea(double X[], double Y[], int n)
{
// Initialize area
double area = 0.0;
// Calculate value of shoelace formula
int j = n - 1;
for (int i = 0; i < n; i++)
{
area += (X[j] + X[i]) * (Y[j] - Y[i]);
j = i; // j is previous vertex to i
}
// Return absolute value
return abs(area / 2.0);
}
// Driver program to test above function
int main()
{
double X[] = {0, 2, 4};
double Y[] = {1, 3, 7};
int n = sizeof(X)/sizeof(X[0]);
cout << polygonArea(X, Y, n);
}
Java
// Java program to evaluate area
// of a polygon using shoelace formula
import java.io.*;
class GFG
{
// (X[i], Y[i]) are coordinates of i'th point.
public static double polygonArea(double X[], double Y[],
int n)
{
// Initialize area
double area = 0.0;
// Calculate value of shoelace formula
int j = n - 1;
for (int i = 0; i < n; i++)
{
area += (X[j] + X[i]) * (Y[j] - Y[i]);
// j is previous vertex to i
j = i;
}
// Return absolute value
return Math.abs(area / 2.0);
}
// Driver program
public static void main (String[] args)
{
double X[] = {0, 2, 4};
double Y[] = {1, 3, 7};
int n = 3;
System.out.println(polygonArea(X, Y, n));
}
}
// This code is contributed by Sunnnysingh
Python3
# python3 program to evaluate
# area of a polygon using
# shoelace formula
# (X[i], Y[i]) are coordinates of i'th point.
def polygonArea(X, Y, n):
# Initialize area
area = 0.0
# Calculate value of shoelace formula
j = n - 1
for i in range(0,n):
area += (X[j] + X[i]) * (Y[j] - Y[i])
j = i # j is previous vertex to i
# Return absolute value
return int(abs(area / 2.0))
# Driver program to test above function
X = [0, 2, 4]
Y = [1, 3, 7]
n = len(X)
print(polygonArea(X, Y, n))
# This code is contributed by
# Smitha Dinesh Semwal
C#
// C# program to evaluate area
// of a polygon using shoelace formula
using System;
class GFG {
// (X[i], Y[i]) are coordinates of i'th point.
public static double polygonArea(double[] X,
double[] Y, int n)
{
// Initialize area
double area = 0.0;
// Calculate value of shoelace formula
int j = n - 1;
for (int i = 0; i < n; i++) {
area += (X[j] + X[i]) * (Y[j] - Y[i]);
// j is previous vertex to i
j = i;
}
// Return absolute value
return Math.Abs(area / 2.0);
}
// Driver program
public static void Main()
{
double[] X = { 0, 2, 4 };
double[] Y = { 1, 3, 7 };
int n = 3;
Console.WriteLine(polygonArea(X, Y, n));
}
}
// This code is contributed by vt_m.
PHP
Javascript
输出 :
2
为什么叫鞋带配方?
之所以这么称呼这个公式,是因为我们评估它的方式。
例子 :
Let the input vertices be
(0, 1), (2, 3), and (4, 7).
Evaluation procedure matches with process of tying
shoelaces.
We write vertices as below
0 1
2 3
4 7
0 1 [written twice]
we evaluate positive terms as below
0 \ 1
2 \ 3
4 \ 7
0 1
i.e., 0*3 + 2*7 + 4*1 = 18
we evaluate negative terms as below
0 1
2 / 3
4 / 7
0 / 1
i.e., 0*7 + 4*3 + 2*1 = 14
Area = 1/2 (18 - 14) = 2
See this for a clearer image.
这是如何运作的?
我们总是可以将多边形划分为三角形。面积公式是通过取每条边 AB 并通过取叉积(给出平行四边形的面积)并除以 2 来计算以原点为顶点的三角形 ABO 的(有符号)面积得出的。作为一个环绕多边形,这些具有正负面积的三角形将重叠,原点和多边形之间的面积将被抵消并为0,而只保留参考三角形内部的面积。 [来源:维基]
为了更好地理解,请看下图:
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。