给定四边形a、b、c、d的四个边,从给定的边中找出四边形可能的最大面积。
例子:
Input : 1 2 1 2
Output : 2.00
It is optimal to construct a rectangle for maximum area .
根据Bretschneider 公式,一般四边形的面积由下式给出
这里a、b、c、d是四边形的边,s是四边形的半周长,角是两个对角。
所以,这个公式只有在对角总和为 pi(180) 时才最大化,然后我们可以使用 Bretschneider 公式的简化形式来获得(最大)面积 K。
这个公式被称为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 现场工作专业课程和学生竞争性编程现场课程。