Minimum side of square embedded in Regular polygon with N sides
Given an even number N which represents the number of sides of a regular polygon with N vertices, the task is to find the square of the minimum size such that given Polygon can completely embed in the square.
A Polygon is a convex figure and has equal sides and equal angles. All sides have length 1.
Embedding: Place Polygon in the square in such way that each point which lies inside or on a border of N should also lie inside or on a border of the square.
Examples:
Input: N = 4
Output: 1
Explanation:
Regular polygon with 4 Sides is square with side 1.
Given polygon can easily embed on the square with side 1.
Input: N = 6
Output: 1.931851653
Explanation :
Regular polygon with 6 Sides is Hexagon with side 1.
Given polygon can easily embed on the square with side 1.931851653.
Approach: The idea is to observe that on a 3-D plane, when a polygon is embedded in a square, it might be rotated. A similar approach has been discussed in Hexagon problem and Octagon problem . Therefore, we take the projection of each side on both the axis using the mathematical functions sin() and cos(). The overall sum of all the projections is the minimum side of the square required in this problem.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const double pi = acos (-1.0);
double nGon( int N)
{
double proAngleVar;
if (N % 4 == 0) {
proAngleVar = pi * (180.0 / N) / 180;
}
else {
proAngleVar = pi * (180.0 / (2 * N)) / 180;
}
double negX = 1.0e+99, posX = -1.0e+99, negY = 1.0e+99,
posY = -1.0e+99;
for ( int j = 0; j < N; ++j) {
double px = cos (2 * pi * j / N + proAngleVar);
double py = sin (2 * pi * j / N + proAngleVar);
negX = min(negX, px);
posX = max(posX, px);
negY = min(negY, py);
posY = max(posY, py);
}
double opt2 = max(posX - negX, posY - negY);
return ( double )opt2 / sin (pi / N) / 2;
}
int main()
{
int N = 10;
cout << nGon(N);
return 0;
}
|
Java
class GFG {
static double pi = Math.acos(- 1.0 );
static double nGon( int N)
{
double proAngleVar;
if (N % 4 == 0 ) {
proAngleVar = pi * ( 180.0 / N) / 180 ;
}
else {
proAngleVar = pi * ( 180.0 / ( 2 * N)) / 180 ;
}
double negX = 1 .0e+ 99 , posX = - 1 .0e+ 99 ,
negY = 1 .0e+ 99 , posY = - 1 .0e+ 99 ;
for ( int j = 0 ; j < N; ++j) {
double px
= Math.cos( 2 * pi * j / N + proAngleVar);
double py
= Math.sin( 2 * pi * j / N + proAngleVar);
negX = Math.min(negX, px);
posX = Math.max(posX, px);
negY = Math.min(negY, py);
posY = Math.max(posY, py);
}
double opt2 = Math.max(posX - negX, posY - negY);
return ( double )opt2 / Math.sin(pi / N) / 2 ;
}
public static void main(String[] args)
{
int N = 10 ;
System.out.printf( "%.5f" , nGon(N));
}
}
|
Python3
import math
pi = math.acos( - 1.0 )
def nGon(N):
proAngleVar = 0
if (N % 4 = = 0 ):
proAngleVar = (pi * ( 180.0 / N)
/ 180 )
else :
proAngleVar = (pi * ( 180.0 / ( 2 * N))
/ 180 )
negX = 1.0e + 99
posX = - 1.0e + 99
negY = 1.0e + 99
posY = - 1.0e + 99
for j in range (N):
px = math.cos( 2 * pi * j / N
+ proAngleVar)
py = math.sin( 2 * pi * j / N
+ proAngleVar)
negX = min (negX, px)
posX = max (posX, px)
negY = min (negY, py)
posY = max (posY, py)
opt2 = max (posX - negX,
posY - negY)
return (opt2 / math.sin(pi / N) / 2 )
if __name__ = = "__main__" :
N = 10
print ( '%.5f' % nGon(N))
|
C#
using System;
class GFG {
static double pi = Math.Acos(-1.0);
static double nGon( int N)
{
double proAngleVar;
if (N % 4 == 0) {
proAngleVar = pi * (180.0 / N) / 180;
}
else {
proAngleVar = pi * (180.0 / (2 * N)) / 180;
}
double negX = 1.0e+99, posX = -1.0e+99,
negY = 1.0e+99, posY = -1.0e+99;
for ( int j = 0; j < N; ++j) {
double px
= Math.Cos(2 * pi * j / N + proAngleVar);
double py
= Math.Sin(2 * pi * j / N + proAngleVar);
negX = Math.Min(negX, px);
posX = Math.Max(posX, px);
negY = Math.Min(negY, py);
posY = Math.Max(posY, py);
}
double opt2 = Math.Max(posX - negX, posY - negY);
return ( double )opt2 / Math.Sin(pi / N) / 2;
}
public static void Main()
{
int N = 10;
Console.Write( string .Format( "{0:F5}" , nGon(N)));
}
}
|
Javascript
<script>
let pi = Math.acos(-1.0);
function nGon( N) {
let proAngleVar;
if (N % 4 == 0) {
proAngleVar = pi * (180.0 / N) / 180;
} else {
proAngleVar = pi * (180.0 / (2 * N)) / 180;
}
let negX = 1.0e+99, posX = -1.0e+99, negY = 1.0e+99, posY = -1.0e+99;
for ( let j = 0; j < N; ++j) {
let px = Math.cos(2 * pi * j / N + proAngleVar);
let py = Math.sin(2 * pi * j / N + proAngleVar);
negX = Math.min(negX, px);
posX = Math.max(posX, px);
negY = Math.min(negY, py);
posY = Math.max(posY, py);
}
let opt2 = Math.max(posX - negX, posY - negY);
return opt2 / Math.sin(pi / N) / 2;
}
let N = 10;
document.write(nGon(N).toFixed(5));
</script>
|
Time Complexity: O(N), where N is the number of sides of the polygon.
Auxiliary Space: O(1)
Last Updated :
25 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...