Untitled

mail@pastecode.io avatar
unknown
python
a year ago
1.6 kB
3
Indexable
import math

def calculate_area_of_intersection(center1, center2, radius1, radius2):
  """Calculates the area of intersection of two circles.

  Args:
    center1: A tuple containing the coordinates of the center of the first circle.
    center2: A tuple containing the coordinates of the center of the second circle.
    radius1: The radius of the first circle.
    radius2: The radius of the second circle.

  Returns:
    The area of intersection of the two circles, rounded up to 6 decimal places.
  """

  distance = math.sqrt((center1[0] - center2[0])**2 + (center1[1] - center2[1])**2)

  if distance > radius1 + radius2:
    return 0
  elif distance < radius1 - radius2:
    return math.pi * radius2**2
  else:
    theta1 = 2 * math.acos((radius1**2 + distance**2 - radius2**2) / (2 * radius1 * distance))
    theta2 = 2 * math.acos((radius2**2 + distance**2 - radius1**2) / (2 * radius2 * distance))

    area_of_intersection = 0.5 * (theta1 * radius1**2) - 0.5 * (radius1**2 * math.sin(theta1)) + 0.5 * (theta2 * radius2**2) - 0.5 * (radius2**2 * math.sin(theta2))

    return round(area_of_intersection, 6)

def main():
  """Calculates the area of intersection of two circles."""

  # Input the coordinates and radii of the two circles.
  center1 = (int(input()), int(input()))
  radius1 = int(input())
  center2 = (int(input()), int(input()))
  radius2 = int(input())

  # Calculate the area of intersection.
  area_of_intersection = calculate_area_of_intersection(center1, center2, radius1, radius2)

  # Output the area of intersection.
  print(area_of_intersection)

if __name__ == '__main__':
  main()