📜  四边形的最大面积

📅  最后修改于: 2021-10-23 09:09:10             🧑  作者: 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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程