Minimum side of square embedded in Regular polygon with N sides | C ++ program to find the minimum side of the square in which a regular polygon with even sides can completely embed ; PI value in C ++ using acos function ; Function to find the minimum side of the square in which a regular polygon with even sides can completely embed ; Projection angle variation from axes ; Projection angle variation when the number of sides are in multiple of 4 ; Distance between the end points ; Projection from all N points on X - axis ; Projection from all N points on Y - axis ; Maximum side ; Return the portion of side forming the square ; Driver code
#include <bits/stdc++.h> NEW_LINE 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 ; }