Advertisement
NOBLE-_-MAN

Untitled

Feb 20th, 2022
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.27 KB | None | 0 0
  1. import math
  2.  
  3. # <INPUT>
  4. x, y, r = [int(o) for o in input().split()]
  5. N = int(input())
  6. coords = []
  7. for _ in range(N):
  8.     x1, y1 = [int(q) for q in input().split()]
  9.     coords.append([x1, y1])
  10. # </INPUT>
  11.  
  12. closest = []
  13. inside = []
  14. for l in range(N):
  15.     if (coords[l][0] - x) ** 2 + (coords[l][1] - y) ** 2 <= r ** 2:     # dots inside or on the circle
  16.         inside.append(coords[l])
  17.     else:
  18.         closest.append(coords[l])
  19.  
  20.  
  21. def distance_to_cent(point):
  22.     global x, y
  23.     return ((point[0] - x) ** 2 + (point[1] - y) ** 2) ** 0.5
  24.  
  25.  
  26. flag = 0
  27. try:
  28.     close = sorted(closest, key=distance_to_cent)
  29. except ValueError:
  30.     flag = 1
  31.  
  32. try:
  33.     if not flag:
  34.         close = close[:2]
  35. except IndexError:
  36.     if not flag:
  37.         close = close[0]
  38.  
  39. on_circle = []
  40. for i in range(len(coords)):
  41.     x1 = coords[i][0]
  42.     y1 = coords[i][1]
  43.     try:
  44.         x2 = coords[i + 1][0]
  45.         y2 = coords[i + 1][1]
  46.     except IndexError:
  47.         x2 = coords[0][0]
  48.         y2 = coords[0][1]
  49.  
  50.     if [x1, y1] in inside:
  51.         ind = inside.index([x1, y1])
  52.     elif [x2, y2] in inside:
  53.         ind = inside.index([x2, y2]) - 1
  54.  
  55.     try:
  56.         k = (y2 - y1) / (x2 - x1)
  57.         b = y1 - k * x1
  58.         p = b - y
  59.         A = k ** 2 + 1
  60.         B = -(2 * x - 2 * p * k)
  61.         C = x ** 2 + p ** 2 - r ** 2
  62.         D = B ** 2 - 4 * A * C
  63.         if D > 0:
  64.             X_1 = (-B + D ** 0.5) / (2 * A)
  65.             X_2 = (-B - D ** 0.5) / (2 * A)
  66.             if x1 < X_1 < x2 or x1 > X_1 > x2:
  67.                 X_des = X_1
  68.                 Y_des = k * X_des + b
  69.                 inside.insert(ind + 1, [X_des, Y_des])
  70.                 on_circle.append([X_des, Y_des])
  71.             elif x1 < X_2 < x2 or x1 > X_2 > x2:
  72.                 X_des = X_2
  73.                 Y_des = k * X_des + b
  74.                 inside.insert(ind + 1, [X_des, Y_des])
  75.                 on_circle.append([X_des, Y_des])
  76.     except ZeroDivisionError:
  77.         X_des = x2
  78.         A = 1
  79.         B = -2 * y
  80.         C = y ** 2 + (X_des - x) ** 2 - r ** 2
  81.         D = B ** 2 - 4 * A * C
  82.         if D > 0:
  83.             Y_1 = (-B + D ** 0.5) / (2 * A)
  84.             Y_2 = (-B - D ** 0.5) / (2 * A)
  85.             if y1 < Y_1 < y2 or y1 > Y_1 > y2:
  86.                 Y_des = Y_1
  87.                 inside.insert(ind + 1, [X_des, Y_des])
  88.                 on_circle.append([X_des, Y_des])
  89.             elif y1 < Y_2 < y2 or y1 > Y_2 > y2:
  90.                 Y_des = Y_2
  91.                 inside.insert(ind + 1, [X_des, Y_des])
  92.                 on_circle.append([X_des, Y_des])
  93.  
  94. # Calculating square:
  95. try:
  96.     hord = ((on_circle[0][0] - on_circle[1][0]) ** 2 + (on_circle[0][1] - on_circle[1][1]) ** 2) ** 0.5
  97.     p = (hord + 2 * r) / 2
  98.     triangle_sq = (p * ((p - r) ** 2) * (p - hord)) ** 0.5
  99.     cos = (2 * r ** 2 - hord ** 2) / (2 * r ** 2)
  100.     angle = math.acos(cos)
  101.     sector_sq = (angle * r ** 2) / 2
  102.     hord_square = sector_sq - triangle_sq
  103. except IndexError:
  104.     hord_square = 0
  105. res = 0
  106. for j in range(len(inside)):
  107.     try:
  108.         res += inside[j][0] * inside[j + 1][1]
  109.         res -= inside[j + 1][0] * inside[j][1]
  110.     except IndexError:
  111.         res += inside[j][0] * inside[0][1]
  112.         res -= inside[0][0] * inside[j][1]
  113.  
  114. rope_sq = 0.5 * abs(res)
  115. square_final = hord_square + rope_sq
  116. print(square_final)
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement