Maximum area of quadrilateral - GeeksforGeeks

29 Nov, 2021
Given four sides of quadrilateral a, b, c, d, find the maximum area of the quadrilateral possible from the given sides .Examples:  
Input : 1 2 1 2
Output : 2.00
It is optimal to construct a rectangle for maximum area .
 
 
According to Bretschneider’s formula, the area of a general quadrilateral is given by Here a, b, c, d are the sides of a quadrilateral, s is the semiperimeter of a quadrilateral and angles are two opposite angles. So, this formula is maximized only when opposite angles sum to pi(180) then we can use a simplified form of Bretschneider’s formula to get the (maximum) area K. This formula is called as Brahmagupta’s formula . Below is the implementation of given approach 
C++
C
Java
Python3
C#
PHP
Javascript
// CPP program to find maximum are of a // quadrilateral#include <iostream>#include <math.h>using namespace std;   double maxArea(double a, double b,                double c, double d){    // Calculating the semi-perimeter     // of the given quadrilateral    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 codeint main(){    double a = 1, b = 2, c= 1, d = 2;   cout <<maxArea(a, b, c, d);    return 0;}  // This code is contributed by shivanisinghss2110
// CPP program to find maximum are of a // quadrilateral#include <stdio.h>#include <math.h>   double maxArea(double a, double b,                double c, double d){    // Calculating the semi-perimeter     // of the given quadrilateral    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 codeint main(){    double a = 1, b = 2, c= 1, d = 2;    printf("%.2f\n",maxArea(a, b, c, d));    return 0;}
// Java program to find maximum are of a // quadrilateralimport java.io.*;  class GFG {    static double maxArea(double a, double b,                           double c, double d)    {        // Calculating the semi-perimeter         // of the given quadrilateral        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 program to find maximum # area of a quadrilateralimport math  def maxArea (a , b , c , d ):      # Calculating the semi-perimeter    # of the given quadrilateral    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 codea = 1b = 2c = 1d = 2print("%.2f"%maxArea(a, b, c, d))  # This code is contributed by "Sharad_Bhardwaj".
// C# program to find maximum are of a // quadrilateralusing System;  class GFG {          static double maxArea(double a, double b,                          double c, double d)    {                  // Calculating the semi-perimeter         // of the given quadrilateral        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// PHP program to find maximum are of a // quadrilateral  function maxArea( $a, $b, $c, $d){          // Calculating the semi-perimeter     // of the given quadrilateral    $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$a = 1; $b = 2; $c= 1; $d = 2;echo(maxArea($a, $b, $c, $d));  // This code is contributed by vt_m.?>
<script>  // JavaScript program to find maximum are of a // quadrilateral   function maxArea(a, b, c, d) {     // Calculating the semi-perimeter     // of the given quadrilateral     let 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       let a = 1, b = 2, c= 1, d = 2;     document.write(maxArea(a, b, c, d));   // This code is contributed by Surbhi Tyagi.  </script>
Output:  
2.00
 
vt_m
surbhityagi15
rajeev0719singh
shivanisinghss2110
area-volume-programs
Geometric
Technical Scripter
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Program for distance between two points on earth
Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)
Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
Closest Pair of Points | O(nlogn) Implementation
Given n line segments, find if any two segments intersect
Convex Hull | Set 2 (Graham Scan)
Equation of circle when three points on the circle are given
Polygon Clipping | Sutherland–Hodgman Algorithm
Program for Point of Intersection of Two Lines
Haversine formula to find distance between two points on a sphere