Minimum side of square embedded in Regular polygon with N sides
Last Updated :
25 Oct, 2022
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++
// C++ program to find the minimum
// side of the square in which
// a regular polygon with even sides
// can completely embed
#include <bits/stdc++.h>
using namespace std;
// PI value in C++ using
// acos function
const double pi = acos(-1.0);
// Function to find the minimum
// side of the square in which
// a regular polygon with even sides
// can completely embed
double nGon(int N)
{
// Projection angle variation
// from axes
double proAngleVar;
// Projection angle variation
// when the number of
// sides are in multiple of 4
if (N % 4 == 0) {
proAngleVar = pi * (180.0 / N) / 180;
}
else {
proAngleVar = pi * (180.0 / (2 * N)) / 180;
}
// Distance between the end points
double negX = 1.0e+99, posX = -1.0e+99, negY = 1.0e+99,
posY = -1.0e+99;
for (int j = 0; j < N; ++j) {
// Projection from all N points
// on X-axis
double px = cos(2 * pi * j / N + proAngleVar);
// Projection from all N points
// on Y-axis
double py = sin(2 * pi * j / N + proAngleVar);
negX = min(negX, px);
posX = max(posX, px);
negY = min(negY, py);
posY = max(posY, py);
}
// Maximum side
double opt2 = max(posX - negX, posY - negY);
// Return the portion of side
// forming the square
return (double)opt2 / sin(pi / N) / 2;
}
// Driver code
int main()
{
int N = 10;
cout << nGon(N);
return 0;
}
Java
// Java program to find the minimum
// side of the square in which
// a regular polygon with even sides
// can completely embed
class GFG {
// PI value in Java using
// acos function
static double pi = Math.acos(-1.0);
// Function to find the minimum
// side of the square in which
// a regular polygon with even sides
// can completely embed
static double nGon(int N)
{
// Projection angle variation
// from axes
double proAngleVar;
// Projection angle variation
// when the number of
// sides are in multiple of 4
if (N % 4 == 0) {
proAngleVar = pi * (180.0 / N) / 180;
}
else {
proAngleVar = pi * (180.0 / (2 * N)) / 180;
}
// Distance between the end points
double negX = 1.0e+99, posX = -1.0e+99,
negY = 1.0e+99, posY = -1.0e+99;
for (int j = 0; j < N; ++j) {
// Projection from all N points
// on X-axis
double px
= Math.cos(2 * pi * j / N + proAngleVar);
// Projection from all N points
// on Y-axis
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);
}
// Maximum side
double opt2 = Math.max(posX - negX, posY - negY);
// Return the portion of side
// forming the square
return (double)opt2 / Math.sin(pi / N) / 2;
}
// Driver code
public static void main(String[] args)
{
int N = 10;
System.out.printf("%.5f", nGon(N));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python 3 program to find the minimum
# side of the square in which
# a regular polygon with even sides
# can completely embed
import math
# PI value in Python 3 using
# acos function
pi = math.acos(-1.0)
# Function to find the minimum
# side of the square in which
# a regular polygon with even sides
# can completely embed
def nGon(N):
# Projection angle variation
# from axes
proAngleVar = 0
# Projection angle variation
# when the number of
# sides are in multiple of 4
if (N % 4 == 0):
proAngleVar = (pi * (180.0 / N)
/ 180)
else:
proAngleVar = (pi * (180.0 / (2 * N))
/ 180)
# Distance between the end points
negX = 1.0e+99
posX = -1.0e+99
negY = 1.0e+99
posY = -1.0e+99
for j in range(N):
# Projection from all N points
# on X-axis
px = math.cos(2 * pi * j / N
+ proAngleVar)
# Projection from all N points
# on Y-axis
py = math.sin(2 * pi * j / N
+ proAngleVar)
negX = min(negX, px)
posX = max(posX, px)
negY = min(negY, py)
posY = max(posY, py)
# Maximum side
opt2 = max(posX - negX,
posY - negY)
# Return the portion of side
# forming the square
return (opt2 / math.sin(pi / N) / 2)
# Driver code
if __name__ == "__main__":
N = 10
print('%.5f' % nGon(N))
# This code is contributed by ukasp.
C#
// C# program to find the minimum
// side of the square in which
// a regular polygon with even sides
// can completely embed
using System;
class GFG {
// PI value in Java using
// acos function
static double pi = Math.Acos(-1.0);
// Function to find the minimum
// side of the square in which
// a regular polygon with even sides
// can completely embed
static double nGon(int N)
{
// Projection angle variation
// from axes
double proAngleVar;
// Projection angle variation
// when the number of
// sides are in multiple of 4
if (N % 4 == 0) {
proAngleVar = pi * (180.0 / N) / 180;
}
else {
proAngleVar = pi * (180.0 / (2 * N)) / 180;
}
// Distance between the end points
double negX = 1.0e+99, posX = -1.0e+99,
negY = 1.0e+99, posY = -1.0e+99;
for (int j = 0; j < N; ++j) {
// Projection from all N points
// on X-axis
double px
= Math.Cos(2 * pi * j / N + proAngleVar);
// Projection from all N points
// on Y-axis
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);
}
// Maximum side
double opt2 = Math.Max(posX - negX, posY - negY);
// Return the portion of side
// forming the square
return (double)opt2 / Math.Sin(pi / N) / 2;
}
// Driver code
public static void Main()
{
int N = 10;
Console.Write(string.Format("{0:F5}", nGon(N)));
}
}
// This code is contributed by Nidhi_biet
JavaScript
<script>
// Javascript program to find the minimum
// side of the square in which
// a regular polygon with even sides
// can completely embed
// PI value in Java using
// acos function
let pi = Math.acos(-1.0);
// Function to find the minimum
// side of the square in which
// a regular polygon with even sides
// can completely embed
function nGon( N) {
// Projection angle variation
// from axes
let proAngleVar;
// Projection angle variation
// when the number of
// sides are in multiple of 4
if (N % 4 == 0) {
proAngleVar = pi * (180.0 / N) / 180;
} else {
proAngleVar = pi * (180.0 / (2 * N)) / 180;
}
// Distance between the end points
let negX = 1.0e+99, posX = -1.0e+99, negY = 1.0e+99, posY = -1.0e+99;
for ( let j = 0; j < N; ++j) {
// Projection from all N points
// on X-axis
let px = Math.cos(2 * pi * j / N + proAngleVar);
// Projection from all N points
// on Y-axis
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);
}
// Maximum side
let opt2 = Math.max(posX - negX, posY - negY);
// Return the portion of side
// forming the square
return opt2 / Math.sin(pi / N) / 2;
}
// Driver code
let N = 10;
document.write(nGon(N).toFixed(5));
// This code is contributed by Princi Singh
</script>
Time Complexity: O(N), where N is the number of sides of the polygon.
Auxiliary Space: O(1)
Similar Reads
Polygon with maximum sides that can be inscribed in an N-sided regular polygon Given a regular polygon of N sides, the task is to find the maximum sided polygon that can be inscribed inside the given polygon by joining non-adjacent vertices. Print -1, if no such polygon exist. Examples: Input: N = 8Output: 4Explanation:At most a 4 sided polygon can be inscribed inside the give
3 min read
Find the angle of Rotational Symmetry of an N-sided regular polygon Given an integer N which is the number of sides of a regular polygon. The task is to find the smallest angle of rotation such that the generated regular polygons have a similar position and dimensions, i.e. the new rotated polygon is in symmetry with the initial one. A shape is said to have a rotati
4 min read
Area of largest Circle inscribe in N-sided Regular polygon Given a regular polygon of N sides with side length a. The task is to find the area of the Circle which inscribed in the polygon. Note : This problem is mixed version of This and This Examples: Input: N = 6, a = 4 Output: 37.6801 Explanation: In this, the polygon have 6 faces and as we see in fig.1
5 min read
Program to find Area of Triangle inscribed in N-sided Regular Polygon Given the triangle inscribed in an N-sided regular polygon with given side length, formed using any 3 vertices of the polygon, the task is to find the area of this triangle. Examples: Input: N = 6, side = 10 Output: 129.904 Input: N = 8, side = 5 Output: 45.2665 Approach: Consider the 1st example: G
6 min read
Apothem of a n-sided regular polygon Given here the side length a of a regular n-sided polygon, the task is to find the length of its Apothem. Apothem is the line drawn from the center of the polygon that is perpendicular to one of its sides. Examples: Input a = 9, n = 6 Output: 7.79424 Input: a = 8, n = 7 Output: 8.30609 Approach: In
4 min read