📜  四边形的最大面积

📅  最后修改于: 2021-05-07 09:40:56             🧑  作者: Mango

给定四边形a,b,c,d的四个边,可以从给定边找到四边形的最大面积。
例子:

Input : 1 2 1 2
Output : 2.00
It is optimal to construct a rectangle for maximum area .

根据Bretschneider的公式,一般四边形的面积为K={\sqrt {(s-a)(s-b)(s-c)(s-d)-abcd\cdot \cos ^{2}\left({\frac {\alpha +\gamma }{2}}\right)}}
这里a,b,c,d是四边形的边,s是四边形的半周,角度是两个相反的角度。
因此,仅当对角的总和等于pi(180)时,此公式才会最大化,然后我们可以使用Bretschneider公式的简化形式来获得(最大)面积K。
K={\sqrt {(s-a)(s-b)(s-c)(s-d)}}
该公式称为Brahmagupta公式
下面是给定方法的实现

C++
// CPP program to find maximum are of a
// quadrilateral
#include 
using namespace std;
 
double maxArea(double a, double b,
                double c, double d)
{
    // Calculating the semi-perimeter
    // of the given quadilateral
    double semiperimeter = (a + b + c + d) / 2;
 
    // Applying Brahmagupta's formula to
    // get maximum area of quadrilateral
    return sqrt((semiperimeter - a) *
                (semiperimeter - b) *
                (semiperimeter - c) *
                (semiperimeter - d));
}
 
// Driver code
int main()
{
    double a = 1, b = 2, c= 1, d = 2;
    printf("%.2f\n",maxArea(a, b, c, d));
    return 0;
}


Java
// Java program to find maximum are of a
// quadrilateral
import java.io.*;
 
class GFG
{
    static double maxArea(double a, double b,
                           double c, double d)
    {
        // Calculating the semi-perimeter
        // of the given quadilateral
        double semiperimeter = (a + b + c + d) / 2;
     
        // Applying Brahmagupta's formula to
        // get maximum area of quadrilateral
        return Math.sqrt((semiperimeter - a) *
                         (semiperimeter - b) *
                         (semiperimeter - c) *
                         (semiperimeter - d));
    }
     
    // Driver code
    public static void main (String[] args)
    {
        double a = 1, b = 2, c= 1, d = 2;
        System.out.println(maxArea(a, b, c, d));
    }
}
 
// This code is contributed by sunnysingh


Python3
# Python3 program to find maximum
# area of a quadrilateral
import math
 
def maxArea (a , b , c , d ):
 
    # Calculating the semi-perimeter
    # of the given quadilateral
    semiperimeter = (a + b + c + d) / 2
     
    # Applying Brahmagupta's formula to
    # get maximum area of quadrilateral
    return math.sqrt((semiperimeter - a) *
                    (semiperimeter - b) *
                    (semiperimeter - c) *
                    (semiperimeter - d))
 
# Driver code
a = 1
b = 2
c = 1
d = 2
print("%.2f"%maxArea(a, b, c, d))
 
# This code is contributed by "Sharad_Bhardwaj".


C#
// C# program to find maximum are of a
// quadrilateral
using System;
 
class GFG {
     
    static double maxArea(double a, double b,
                          double c, double d)
    {
         
        // Calculating the semi-perimeter
        // of the given quadilateral
        double semiperimeter = (a + b + c + d) / 2;
     
        // Applying Brahmagupta's formula to
        // get maximum area of quadrilateral
        return Math.Sqrt((semiperimeter - a) *
                         (semiperimeter - b) *
                         (semiperimeter - c) *
                         (semiperimeter - d));
    }
     
    // Driver code
    public static void Main ()
    {
        double a = 1, b = 2, c= 1, d = 2;
         
        Console.WriteLine(maxArea(a, b, c, d));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

2.00