def in_circle ( ): #*****************************************************************************80 # ## in_circle() shows which random points are inside a circle. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 29 January 2025 # # Author: # # John Burkardt # import matplotlib.pyplot as plt import numpy as np n = 400 print ( '' ) print ( 'in_circle():' ) print ( ' Compute ', n, ' random points in [-1,+1]x[-1,+1].' ) print ( ' Show which ones are inside the unit circle.' ) print ( ' Demonstrate numpy conditional indexing.' ) # # Pick NC points on unit circle boundary. # nc = 101 t = np.linspace ( 0.0, 2.0 * np.pi, nc ) xc = np.zeros ( [ nc, 2 ] ) xc[:,0] = np.cos ( t ) xc[:,1] = np.sin ( t ) # # Sample N points in [-1,+1] x [-1,+1] # x = 2.0 * np.random.random ( [ n ] ) - 1.0 y = 2.0 * np.random.random ( [ n ] ) - 1.0 # # Measure RSQ, the squared distance of X from center. # rsq = x[:]**2 + y[:]**2 # # Green: circle boundary. # Red: points with 1.0 < RSQ # Blue: points with RSQ <= 1.0 # plt.clf ( ) plt.plot ( xc[:,0], xc[:,1], 'g', linewidth = 3 ) plt.plot ( x[1.0' ) plt.ylabel ( '<--- Y --->' ) plt.title ( 'in or out of circle?' ) plt.grid ( 'True' ) plt.axis ( 'equal' ) filename = 'in_circle.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) plt.show ( ) plt.close ( ) return if ( __name__ == "__main__" ): in_circle ( )