Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- # <INPUT>
- x, y, r = [int(o) for o in input().split()]
- N = int(input())
- coords = []
- for _ in range(N):
- x1, y1 = [int(q) for q in input().split()]
- coords.append([x1, y1])
- # </INPUT>
- closest = []
- inside = []
- for l in range(N):
- if (coords[l][0] - x) ** 2 + (coords[l][1] - y) ** 2 <= r ** 2: # dots inside or on the circle
- inside.append(coords[l])
- else:
- closest.append(coords[l])
- def distance_to_cent(point):
- global x, y
- return ((point[0] - x) ** 2 + (point[1] - y) ** 2) ** 0.5
- flag = 0
- try:
- close = sorted(closest, key=distance_to_cent)
- except ValueError:
- flag = 1
- try:
- if not flag:
- close = close[:2]
- except IndexError:
- if not flag:
- close = close[0]
- on_circle = []
- for i in range(len(coords)):
- x1 = coords[i][0]
- y1 = coords[i][1]
- try:
- x2 = coords[i + 1][0]
- y2 = coords[i + 1][1]
- except IndexError:
- x2 = coords[0][0]
- y2 = coords[0][1]
- if [x1, y1] in inside:
- ind = inside.index([x1, y1])
- elif [x2, y2] in inside:
- ind = inside.index([x2, y2]) - 1
- try:
- k = (y2 - y1) / (x2 - x1)
- b = y1 - k * x1
- p = b - y
- A = k ** 2 + 1
- B = -(2 * x - 2 * p * k)
- C = x ** 2 + p ** 2 - r ** 2
- D = B ** 2 - 4 * A * C
- if D > 0:
- X_1 = (-B + D ** 0.5) / (2 * A)
- X_2 = (-B - D ** 0.5) / (2 * A)
- if x1 < X_1 < x2 or x1 > X_1 > x2:
- X_des = X_1
- Y_des = k * X_des + b
- inside.insert(ind + 1, [X_des, Y_des])
- on_circle.append([X_des, Y_des])
- elif x1 < X_2 < x2 or x1 > X_2 > x2:
- X_des = X_2
- Y_des = k * X_des + b
- inside.insert(ind + 1, [X_des, Y_des])
- on_circle.append([X_des, Y_des])
- except ZeroDivisionError:
- X_des = x2
- A = 1
- B = -2 * y
- C = y ** 2 + (X_des - x) ** 2 - r ** 2
- D = B ** 2 - 4 * A * C
- if D > 0:
- Y_1 = (-B + D ** 0.5) / (2 * A)
- Y_2 = (-B - D ** 0.5) / (2 * A)
- if y1 < Y_1 < y2 or y1 > Y_1 > y2:
- Y_des = Y_1
- inside.insert(ind + 1, [X_des, Y_des])
- on_circle.append([X_des, Y_des])
- elif y1 < Y_2 < y2 or y1 > Y_2 > y2:
- Y_des = Y_2
- inside.insert(ind + 1, [X_des, Y_des])
- on_circle.append([X_des, Y_des])
- # Calculating square:
- try:
- hord = ((on_circle[0][0] - on_circle[1][0]) ** 2 + (on_circle[0][1] - on_circle[1][1]) ** 2) ** 0.5
- p = (hord + 2 * r) / 2
- triangle_sq = (p * ((p - r) ** 2) * (p - hord)) ** 0.5
- cos = (2 * r ** 2 - hord ** 2) / (2 * r ** 2)
- angle = math.acos(cos)
- sector_sq = (angle * r ** 2) / 2
- hord_square = sector_sq - triangle_sq
- except IndexError:
- hord_square = 0
- res = 0
- for j in range(len(inside)):
- try:
- res += inside[j][0] * inside[j + 1][1]
- res -= inside[j + 1][0] * inside[j][1]
- except IndexError:
- res += inside[j][0] * inside[0][1]
- res -= inside[0][0] * inside[j][1]
- rope_sq = 0.5 * abs(res)
- square_final = hord_square + rope_sq
- print(square_final)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement