Untitled

mail@pastecode.io avatar
unknown
python
a year ago
1.2 kB
1
Indexable
import math

def calculate_area_of_intersection(centerX1, centerX2, centerY1, centerY2, radius1, radius2):

  distance = math.sqrt((centerX1 - centerX2)**2 + (centerY1 - centerY2)**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()