Advertisement
SimeonTs

SUPyF Objects and Classes - 02. Closest Two Points

Aug 12th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.05 KB | None | 0 0
  1. """
  2. Objects and Classes
  3. Проверка: https://judge.softuni.bg/Contests/Practice/Index/950#1
  4.  
  5. SUPyF Objects and Classes - 02. Closest Two Points
  6.  
  7. Problem:
  8. Write a program to read n points and find the closest two of them.
  9.  
  10. Input:
  11.    The input holds the number of points n and n lines, each holding a point {X and Y coordinate}.
  12. Output
  13.    - The output holds the shortest distance and the closest two points.
  14.    - If several pairs of points are equally close, print the first of them (from top to bottom).
  15.  
  16. Hints:
  17.    - Use the class Point you created in the previous task.
  18.    - Create an array  points that will keep all points.
  19.    - Create a method find_closest_points(points) that will check distance between every two pairs from the array of
  20.    points and returns the two closest points in a new array.
  21.    - Print the closest distance and the coordinates of the two closest points.
  22. """
  23.  
  24. import math
  25. import sys
  26.  
  27.  
  28. class ClosePoints:
  29.     def __init__(self, first, second, diff):
  30.         self.first = first
  31.         self.second = second
  32.         self.diff = diff
  33.  
  34.  
  35. class Point:
  36.     def __init__(self, x, y):
  37.         self.x = x
  38.         self.y = y
  39.  
  40.     def calculate_distance(self, second_point):
  41.         a = int(math.fabs(self.x - second_point.x))
  42.         b = int(math.fabs(self.y - second_point.y))
  43.         result = math.sqrt((a * a) + (b * b))
  44.         return result
  45.  
  46.  
  47. count = int(input())
  48.  
  49. points = list()
  50. closest_points = list()
  51.  
  52. for i in range(count):
  53.     In = input().split(" ")
  54.     points.append(Point(int(In[0]), int(In[1])))
  55.  
  56. closest = sys.maxsize
  57. close_points = None
  58.  
  59. for p in range(len(points)):
  60.     for i in range(len(points)):
  61.         if p == i:
  62.             continue
  63.         distance = points[p].calculate_distance(points[i])
  64.         if distance < closest:
  65.             closest = distance
  66.             close_points = ClosePoints(points[p], points[i], distance)
  67.  
  68. print(f"{close_points.diff:.3f}")
  69. print(f"({close_points.first.x}, {close_points.first.y})")
  70. print(f"({close_points.second.x}, {close_points.second.y})")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement