Largest hexagon that can be inscribed within a square - GeeksforGeeks

24 Mar, 2021
Given side of a square a, the task is to find the side of the largest hexagon that can be inscribed within the given square.Examples:  
Input: a = 6 Output: 3.1056Input: a = 8 Output: 4.1408  
 
 
Approach:: Let, the side of the hexagon be x and assume that the side of the square, a gets divided into smaller length b & bigger length c i.e. a = b + c Now from the figure, we see, 
b2 + b2 = x2 which gives b = x / √2 Now again, d / (2 * x) = cos(30) = √3 / 2 i.e. d = x√3 And, c2 + c2 = d2 which gives c = d / √2 = x√3 / √2 Since, a = b + c. So, a = x / √2 + x√3 / √2 = ((1 + √3) / √2) * x = 1.932 * x So, side of the hexagon, x = 0.5176 * a  
Below is the implementation of the above approach: 
C++
Java
Python 3
C#
PHP
Javascript
// C++ Program to find the biggest hexagon which// can be inscribed within the given square#include <bits/stdc++.h>using namespace std; // Function to return the side// of the hexagonfloat hexagonside(float a){     // Side cannot be negative    if (a < 0)        return -1;     // Side of the hexagon    float x = 0.5176 * a;    return x;} // Driver codeint main(){    float a = 6;    cout << hexagonside(a) << endl;    return 0;}
// Java  Program to find the biggest hexagon which// can be inscribed within the given square import java.io.*; class GFG {     // Function to return the side// of the hexagonstatic double hexagonside(double a){     // Side cannot be negative    if (a < 0)        return -1;     // Side of the hexagon    double x = (0.5176 * a);    return x;} // Driver code    public static void main (String[] args) {         double a = 6;        System.out.println (hexagonside(a));    }//This code is contributed by ajit.   }
# Python 3 Program to find the biggest# hexagon which can be inscribed within# the given square # Function to return the side# of the hexagondef hexagonside(a):     # Side cannot be negative    if (a < 0):        return -1;     # Side of the hexagon    x = 0.5176 * a;    return x; # Driver codea = 6;print(hexagonside(a)); # This code is contributed# by Akanksha Rai
// C# Program to find the biggest hexagon which// can be inscribed within the given squareusing System; class GFG{         // Function to return the side// of the hexagonstatic double hexagonside(double a){     // Side cannot be negative    if (a < 0)        return -1;     // Side of the hexagon    double x = (0.5176 * a);    return x;} // Driver codepublic static void Main (){    double a = 6;    Console.WriteLine(hexagonside(a));}} // This code is contributed by Ryuga.
<?php// PHP Program to find the biggest hexagon which// can be inscribed within the given square // Function to return the side of the hexagonfunction hexagonside($a){     // Side cannot be negative    if ($a < 0)        return -1;     // Side of the hexagon    $x = 0.5176 * $a;    return $x;} // Driver code$a = 6;echo hexagonside($a); // This code is contributed by akt_mit?>
<script> // Javascript Program to find the biggest hexagon which// can be inscribed within the given square // Function to return the side// of the hexagonfunction hexagonside(a){     // Side cannot be negative    if (a < 0)        return -1;     // Side of the hexagon    let x = 0.5176 * a;    return x;} // Driver code     let a = 6;    document.write(hexagonside(a) + "<br>");     // This code is contributed by Manoj </script>
3.1056
 
jit_t
ankthon
Akanksha_Rai
mank1083
square-rectangle
C++ Programs
Geometric
Mathematical
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C++ Program for QuickSort
Shallow Copy and Deep Copy in C++
delete keyword in C++
Passing a function as a parameter in C++
cin in C++
How to check if a given point lies inside or outside a polygon?
Closest Pair of Points using Divide and Conquer algorithm
Program for distance between two points on earth
How to check if two given line segments intersect?
Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)