Answer:
import random
f_name = input('Enter the drawing file name: ')
total_shapes = int(input('Enter the no. of total shapes: '))
file = open(f_name,'w')
for i in range(total_shapes):
category = random.randint(0,1)
if category == 0:
left_coordinate = [random.randint(0,499),random.randint(0,499)]
while left_coordinate[0] == 499 and left_coordinate[499] == 0:
left_coordinate = [random.randint(0,499),random.randint(0,499)]
right_coordinate = [random.randint(left_coordinate[0]+1,499), random.randint(left_coordinate[1]+1,499)]
red_intensity = random.randint(0,100)
green_intensity = random.randint(100,200)
blue_intensity = random.randint(192,255)
if i == total_shapes-1:
file.write('Rectange; '+str(left_coordinate[0])+', '+str(left_coordinate[1])+'; '+str(right_coordinate[0])+', '+str(right_coordinate[1])+'; '+str(red_intensity)+', '+str(green_intensity)+', '+str(blue_intensity))
else:
file.write('Rectange; '+str(left_coordinate[0])+', '+str(left_coordinate[1])+'; '+str(right_coordinate[0])+', '+str(right_coordinate[1])+'; '+str(red_intensity)+', '+str(green_intensity)+', '+str(blue_intensity)+'\\')
else: #Circle
center = [random.randint(0,499),random.randint(0,499)]
if center[0] > center[1]:
radius = random.randint(0,499-center[0])
else:
radius = random.randint(0,499-center[1])
red_intensity = random.randint(0,100)
green_intensity = random.randint(100,200)
blue_intensity = random.randint(192,255)
if i == total_shapes-1:
file.write('Circle; '+str(center[0])+', '+str(center[1])+'; '+str(radius)+'; '+str(red_intensity)+', '+str(blue_intensity)+', '+str(green_intensity))
else:
file.write('Circle; '+str(center[0])+', '+str(center[1])+'; '+str(radius)+'; '+str(red_intensity)+', '+str(blue_intensity)+', '+str(green_intensity)+'\\')
file.close()
Step-by-step explanation:
- Get the file name from the user.
- Loop through to generate total shapes .
- Use randInt to randomly generate the category.
- If category is zero, then that means its a rectangle.
- Check for last record using an If statement and then display relevant information.