In: Computer Science
5. [2 marks] Can we do better than this? Recall that the human player is at some fixed location (px, py). Your task is to work out how you would sort the array A so that those enemy AIs that need to be marked can be identified in log(N) time.Specifically, complete the following comparison function that would be used while sorting the array A. Here, (x1, y1) and (x2, y2) are two points from the array A. The function should return true if it considers the first point to be less than or equal to the second, and should return false otherwise. Your function can use the player’s coordinate (px, py) as global variables, i.e. you are allowed to refer to px and py in your function。 Function LessOrEqualTo((x1, y1),(x2, y2))
'''
Python version : 2.7
'''
import math
# function that returns true if (x1,y1) is less than or equal to (x2,y2) with respect to player’s coordinate (px, py)
# player’s coordinate (px,py) are global variables which are accessible by this function
def LessOrEqualTo((x1, y1),(x2, y2)):
# calculate the distance of (x1,y1) from (px,py)
dist1 = math.sqrt((x1-px)**2 + (y1-py)**2)
# calculate the distance of (x2,y2) from (px,py)
dist2 = math.sqrt((x2-px)**2 + (y2-py)**2)
# if dist1 is less than or equal to dist2 return True else return False
if(dist1 <= dist2):
return True
else:
return False
#end of function
Code Screenshot: