Largest hexagon that can be inscribed within a square
Last Updated :
25 Jun, 2022
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.1056
Input: 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++
#include <bits/stdc++.h>
using namespace std;
float hexagonside( float a)
{
if (a < 0)
return -1;
float x = 0.5176 * a;
return x;
}
int main()
{
float a = 6;
cout << hexagonside(a) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static double hexagonside( double a)
{
if (a < 0 )
return - 1 ;
double x = ( 0.5176 * a);
return x;
}
public static void main (String[] args) {
double a = 6 ;
System.out.println (hexagonside(a));
}
}
|
Python 3
def hexagonside(a):
if (a < 0 ):
return - 1 ;
x = 0.5176 * a;
return x;
a = 6 ;
print (hexagonside(a));
|
C#
using System;
class GFG
{
static double hexagonside( double a)
{
if (a < 0)
return -1;
double x = (0.5176 * a);
return x;
}
public static void Main ()
{
double a = 6;
Console.WriteLine(hexagonside(a));
}
}
|
PHP
<?php
function hexagonside( $a )
{
if ( $a < 0)
return -1;
$x = 0.5176 * $a ;
return $x ;
}
$a = 6;
echo hexagonside( $a );
?>
|
Javascript
<script>
function hexagonside(a)
{
if (a < 0)
return -1;
let x = 0.5176 * a;
return x;
}
let a = 6;
document.write(hexagonside(a) + "<br>" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...