Minimum side of square embedded in Regular polygon with N sides | PI value in Java 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 polets ; Projection from all N polets on X - axis ; Projection from all N polets on Y - axis ; Maximum side ; Return the portion of side forming the square ; Driver code
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 ) ) ;