1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| import math import random %matplotlib inline import matplotlib.pyplot as plt from math import atan2 import math
allpoints=[(227, 123), (205, 135), (185, 167), (134, 162), (194, 216), (217, 171), (217, 145), (195, 157), (170, 155), (199, 180), (210, 158), (192, 142), (179, 137), (235, 161), (186, 117), (254, 96), (207, 121), (231, 142), (169, 88), (268, 176)] cx=204 cy=148
distances = []
def polar_angle(point): x, y = point return math.atan2(x-cx, y-cy)
FinalPoints=[]
i=0 def distance(point1, point2): x1, y1 = point1 x2, y2 = point2 return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
def ClockWiseSorting(sorted_list,centralpoints): for i in range(0,(len(sorted_list)-1),2): print(i) if(distance(centralpoints,sorted_list[i])<distance(centralpoints,sorted_list[i+1])): temp=sorted_list[i] sorted_list[i]=sorted_list[i+1] sorted_list[i+1]=temp temp=0 return sorted_list for coordinate in allpoints: distances.append((coordinate, distance(coordinate, (cx,cy)))) sorted_distances = sorted(distances, key=lambda x: x[1]) points_withdistance = sorted_distances[:10] outpoints_withdistance = sorted_distances[10:] points=[] outpoints=[] for pair, dist in points_withdistance: print(f"{pair}: {dist}") points.append(pair) for pair, dist in outpoints_withdistance: print(f"{pair}: {dist}") outpoints.append(pair)
sorted_points = sorted(outpoints, key=polar_angle) sorted_points=ClockWiseSorting(sorted_points,(cx,cy)) for point in sorted_points: print(point) FinalPoints plt.annotate(i, (point[0],point[1])) plt.plot(point[0],point[1],marker="o", markersize=5, markeredgecolor="red", markerfacecolor="red") i+=1 if(i%2==0): i+=2
sorted_points2 = sorted(points, key=polar_angle) sorted_points2=ClockWiseSorting(sorted_points2,(220,220)) i=2 flag=18 for point in sorted_points2: print("far") print(point) if(i==2): if(polar_angle((point[0], point[1]))<polar_angle((sorted_points[0][0], sorted_points[0][1]))): plt.annotate(flag, (point[0],point[1])) plt.plot(point[0],point[1],marker="o", markersize=5, markeredgecolor="lime", markerfacecolor="lime") flag+=1 continue plt.annotate(i, (point[0],point[1])) plt.plot(point[0],point[1],marker="o", markersize=5, markeredgecolor="lime", markerfacecolor="lime") i+=1 if(i%2==0): i+=2 print(polar_angle((194, 211))) print(polar_angle((187, 160))) plt.axis("equal") plt.show()
|