Skip to content

Instantly share code, notes, and snippets.

@danieltanfh95
Created December 18, 2019 14:46
Show Gist options
  • Save danieltanfh95/0c162616f46f8edfb2b375b5c095e0ad to your computer and use it in GitHub Desktop.
Save danieltanfh95/0c162616f46f8edfb2b375b5c095e0ad to your computer and use it in GitHub Desktop.
Buttons py
def check_if_button_touched(point_x, point_y, button_x_min, button_y_min, button_x_max, button_y_max):
within_x = button_x_min <= point_x <= button_x_max
within_y = button_y_min <= point_y <= button_y_max
return within_x and within_y
def get_mid_point(button_x_min, button_y_min, button_x_max, button_y_max):
return (button_x_min+button_x_max) / 2.0, (button_y_min+button_y_max) / 2.0
def get_distance(point_1, point_2):
return ((point_2[0]-point_1[0])**2 + (point_2[1]-point_1[1])**2)**0.5
def getTouchedButtons(touchPos, buttonsPos):
button_list = []
for (index, button) in enumerate(buttonsPos):
if(check_if_button_touched(touchPos[0], touchPos[1], button[0], button[1], button[2], button[3])):
mid_point = get_mid_point(button[0], button[1], button[2], button[3])
distance = get_distance(touchPos, mid_point)
button_list.append((index, distance))
return [x[0] for x in sorted(button_list, key=lambda x:x[1])]
if __name__ == '__main__':
touch_pos = [0, 0]
button_pos = [[-1, -1, 1, 1], [0, 0, 1, 1], [1, 1, 2, 2]]
print(getTouchedButtons(touch_pos, button_pos))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment