I got a list of coordinate points, and I would like to sort them in either clockwise/anti-clockwise.
This is the list I mentioned:
[(985, 268), (112, 316), (998, 448), (1018, 453), (1279, 577), (1196, 477), (1161, 443), (986, 0), (830, 0), (983, 230), (998, 425), (998, 255)]
These coordinate points will help me to draw the line segments of an object. Below is an image for illustration. As you can see, I have marked down all the points in the list in this image.
And my goal is to sort these coordinate points in order to create several line segments. Therefore, my expected result would be as follow:
Anti-Clockwise Direction: [(985, 268), (998, 425), (112, 316), (998, 448), (1018, 453), (1279, 577), (1196, 477), (1161, 443), (998, 255), (986, 0), (983, 230), (830, 0)]
Clockwise Direction: [(985, 268), (830, 0),(983, 230), (986, 0), (998, 255), (1161, 443), (1196, 477), (1279, 577), (1018, 453), (998, 448), (112, 316), (998, 425)]
So far, I have taken a website, https://www.baeldung.com/cs/sort-points-clockwise, as ref and written the following codes but it does not work:
def getDistance(pt1 , pt2):
x = pt1[0] - pt2[0]
y = pt1[1] - pt2[1]
return math.sqrt(x*x+y*y)
def getAngle(pt_center, pt):
x = pt[0] - pt_center[0]
y = pt[1] - pt_center[1]
angle = math.atan2(y,x)
if angle <= 0:
angle = 2*math.pi + angle
return angle
def comparePoints(pt_center, pt1, pt2):
angle1 = getAngle(pt_center, pt1)
angle2 = getAngle(pt_center, pt2)
if angle1 < angle2:
return True
d1 = getDistance(pt_center, pt1)
d2 = getDistance(pt_center, pt2)
if angle1 == angle2 and d1 < d2:
return True
return False
final_concave_points_list = []
for items in final_concave_points:
final_concave_points_list.append([])
for points in items:
final_concave_points_list[-1].append(list(points))
pt_center = [0,0]
point = []
points = final_concave_points_list[0]
for pt in points:
pt_center[0] = pt_center[0] + pt[0]
pt_center[1] = pt_center[1] + pt[1]
pt_center[0] = pt_center[0] / len(points)
pt_center[1] = pt_center[1] / len(points)
for pt in points:
pt[0] = pt[0] - pt_center[0]
pt[1] = pt[1] - pt_center[1]
point.append((pt[0], pt[1]))
print(point)
'''
[(23.0, -56.333333333333314), (-850.0, -8.333333333333314), (36.0, 123.66666666666669), (56.0, 128.66666666666669), (317.0, 252.66666666666669), (234.0, 152.66666666666669), (199.0, 118.66666666666669), (24.0, -324.3333333333333), (-132.0, -324.3333333333333), (21.0, -94.33333333333331), (36.0, 100.66666666666669), (36.0, -69.33333333333331)]
'''
points = scaled_point_list[0]
angle_list = []
for concave in points:
angle = getAngle((0,0), concave)
angle_list.append(angle)
print(angle_list)
'''
[5.102097551727555, 3.15100414040828, 1.2882413743253092, 1.1612360403462985, 0.6735857636846376, 0.5790742693677309, 0.5389402114087971, 4.78632801804263, 4.325513262653661, 4.932184051908722, 1.2283997388640362, 5.193276260580025]
'''
zipped_list = zip(angle_list, points)
sorted_zipped_lists = sorted(zipped_list)
sorted_list1 = [element for _, element in sorted_zipped_lists]
print(sorted_list1)
'''
[(199, 119), (234, 153), (317, 253), (56, 129), (36, 101), (36, 124), (-850, -8), (-132, -324), (24, -324), (21, -94), (23, -56), (36, -69)]
'''
Although I add back the center point (962, 324) to each of the above points, they are still not the desired result.
Thanks a lot.