Minimum side of square embedded in Regular polygon with N sides | Python 3 program to find the minimum side of the square in which a regular polygon with even sides can completely embed ; PI value in Python 3 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
import math NEW_LINE pi = math . acos ( - 1.0 ) NEW_LINE def nGon ( N ) : NEW_LINE INDENT proAngleVar = 0 NEW_LINE if ( N % 4 == 0 ) : NEW_LINE INDENT proAngleVar = ( pi * ( 180.0 / N ) / 180 ) NEW_LINE DEDENT else : NEW_LINE INDENT proAngleVar = ( pi * ( 180.0 / ( 2 * N ) ) / 180 ) NEW_LINE DEDENT negX = 1.0e+99 NEW_LINE posX = - 1.0e+99 NEW_LINE negY = 1.0e+99 NEW_LINE posY = - 1.0e+99 NEW_LINE for j in range ( N ) : NEW_LINE INDENT px = math . cos ( 2 * pi * j / N + proAngleVar ) NEW_LINE py = math . sin ( 2 * pi * j / N + proAngleVar ) NEW_LINE negX = min ( negX , px ) NEW_LINE posX = max ( posX , px ) NEW_LINE negY = min ( negY , py ) NEW_LINE posY = max ( posY , py ) NEW_LINE DEDENT opt2 = max ( posX - negX , posY - negY ) NEW_LINE return ( opt2 / math . sin ( pi / N ) / 2 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 10 NEW_LINE print ( ' % .5f ' % nGon ( N ) ) NEW_LINE DEDENT