#pointsXY.py #randomly generate (x,y) integer points in the plane, #storing them in two lists X and Y. #average point ("centroid"). #distance each point to the centroid. Sum of all these distances. #Find the point closest/farthest from the centroid. import random import math def distance_pair_points(x1,y1, x2,y2): return math.sqrt((x2-x1)**2+(y2-y1)**2) range_nums = int(input("Enter the range of the points, +/-? ")) num_points = int(input("Enter the number of random points: ")) X = [] #x-coordinates Y = [] #y-coordinates for p in range(num_points): X.append(random.randint(-range_nums,range_nums)) Y.append(random.randint(-range_nums,range_nums)) print("("+str(X[p])+","+str(Y[p])+")", end=" ") print() #print(X) #print(Y) centroid_X = sum(X)/num_points centroid_Y = sum(Y)/num_points print("Centroid: ("+str(centroid_X)+","+str(centroid_Y)+")") distances = [] #distance each point to centroid for p in range(num_points): distances.append(distance_pair_points(X[p],Y[p], centroid_X,centroid_Y)) print("Sum of distances to centroid:", sum(distances)) print("Average distance to centroid:", sum(distances)/num_points) print("Min distance to centroid:", min(distances)) print(" ("+str(X[distances.index(min(distances))])+","+ str(Y[distances.index(min(distances))])+")") print("Max distance to centroid:", max(distances)) print(" ("+str(X[distances.index(max(distances))])+","+ str(Y[distances.index(max(distances))])+")") ''' from ezgraphics import GraphicsWindow size = int(input("Enter square size of graphics window: ")) win = GraphicsWindow(size,size) canvas = win.canvas() canvas.drawLine(size/2,1, size/2,size) canvas.drawLine(1,size/2, size,size/2) scale = size/(range_nums*2) canvas.setColor("blue") for p in range(num_points): canvas.setColor("blue") canvas.drawOval(X[p]*scale+size/2,size-(Y[p]*scale+size/2), 2,2) canvas.setColor("yellow") canvas.drawLine(centroid_X*scale+size/2,size-(centroid_Y*scale+size/2), X[p]*scale+size/2,size-(Y[p]*scale+size/2)) canvas.setColor("red") canvas.drawOval(centroid_X*scale+size/2,size-(centroid_Y*scale+size/2), 3,3) print("Look at the graphics window") '''