Advertisement
acclivity

pyFindNearestCoordinates

Oct 12th, 2022
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | Software | 0 0
  1.  
  2. points = [[501, 335], [249, 380], [544, 696], [225, 159], [11, 124], [334, 559], [634, 95], [455, 632], [89, 611],
  3.           [17, 243],  [316, 67], [727, 223], [589, 534], [736, 703], [578, 361], [663, 614], [654, 623], [677, 345],
  4.           [622, 583], [516, 20], [640,404], [480, 15], [440, 168], [542, 412], [578, 551], [294, 308], [249, 250],
  5.           [663, 682], [650, 57], [470, 514], [352, 601], [631, 519], [416, 617], [717, 436], [202, 469], [47, 620],
  6.           [441, 529], [91, 175], [415, 600], [426, 642], [583, 184], [121, 319], [358, 703]]
  7.  
  8. while True:
  9.     user_pos = input("\nEnter your position as x, y: ")
  10.     user_data = user_pos.split(",")
  11.     if len(user_data) != 2:
  12.         print("Invalid coordinate data. Re-try")
  13.         continue
  14.  
  15.     try:
  16.         ux = float(user_data[0].strip())
  17.         uy = float(user_data[1].strip())
  18.     except:
  19.         print("Invalid coordinate data ")
  20.         continue
  21.  
  22.     near_dist = float("inf")
  23.     near_point = 0
  24.     for idx, point in enumerate(points):
  25.         dsq = (point[0] - ux) ** 2 + (point[1] - uy) ** 2
  26.         if dsq < near_dist:
  27.             near_dist = dsq
  28.             near_point = idx
  29.  
  30.     near_dist = near_dist ** 0.5
  31.     print(f"The nearest point to you is {points[near_point]} at a distance of {near_dist:.2f}")
  32.  
  33.  
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement