📌  相关文章
📜  给定n个有序顶点的多边形的面积

📅  最后修改于: 2021-05-04 23:30:16             🧑  作者: Mango

给定具有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

我们可以使用Shoelace公式计算多边形的面积。

通过遵循顶点的叉积得出在多边形中形成的三角形的面积,可以得出上式。
下面是上述公式的实现。

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)
{
    // Initialze 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)
    {
        // Initialze 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):
 
    # Initialze 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)
    {
         
        // Initialze 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并计算以原点O为顶点的三角形ABO的(有符号)面积,并取叉积(给出平行四边形的面积)并除以2而得出的。环绕多边形,这些具有正负面积的三角形将重叠,并且原点和多边形之间的面积将被抵消并总计为0,而仅保留参考三角形内的面积。 [资料来源:维基]

为了更好地理解,请查看以下图表:

使用叉积的三角形面积

将多边形分成较小的三角形以计算面积

同样,对于不规则多边形,我们可以形成三角形以计算面积