Given the coordinates of the centres of two circles (X1, Y1) and (X2, Y2) as well as the radii of the respective circles R1 and R2.Find the floor of the area of their intersection.
Note: Use the value of Pi as 3.14
Example 1:
Input:
X1=0,Y1=0,R1=4
X2=6,Y2=0,R2=4
Output:
7
Explanation:
The intersecting area equals 7.25298806.
So,Answer is 7.
Example 2:
Input:
X1=0,Y1=0,R1=5
X2=11,Y2=0,R2=5
Output:
0
Explanation:
The circles don't intersect.
Your Task:
You don't need to read input or print anything. Your task is to complete the function intersectionArea() which takes the coordinates of the centers as well as the radii(X1, Y1, R1, X2, Y2, R2) as input parameters and returns the area of intersection of the two circles.
Expected Time Complexity:O(LogN)
Expected Auxillary Space:O(1)
Constraints:
-10^{9}<=X1,Y1,X2,Y2<=10^{9}
1<=R1,R2<=10^{9}

Source: geeksforgeeks | Difficulty: EASY
Tags: Geometry

Mathematics

Starter code: #User function Template for python3

class Solution:
    def intersectionArea(self,X1,Y1,R1,X2,Y2,R2):
        #code here

Solutions:
class Solution:
	import math

	def intersectionArea(self, X1, Y1, R1, X2, Y2, R2):
		D = math.sqrt((X2 - X1) ** 2 + (Y2 - Y1) ** 2)
		if D > R1 + R2:
			return 0
		elif D < abs(R1 - R2):
			return min(R1, R2) ** 2 * 3.14
		else:
			a = (R1 ** 2 - R2 ** 2 + D ** 2) / (2 * D)
			b = D - a
			c = math.sqrt(R1 ** 2 - a ** 2)
			area = R1 ** 2 * math.acos(a / R1) - a * c + R2 ** 2 * math.acos(b / R2) - b * math.sqrt(R2 ** 2 - b ** 2)
			return math.floor(area)


import math

class Solution:

	def intersectionArea(self, X1, Y1, R1, X2, Y2, R2):
		Pi = 3.14
		d = math.sqrt((X2 - X1) * (X2 - X1) + (Y2 - Y1) * (Y2 - Y1))
		if d > R1 + R2:
			ans = 0
		elif d <= R1 - R2 and R1 >= R2:
			ans = math.floor(Pi * R2 * R2)
		elif d <= R2 - R1 and R2 >= R1:
			ans = math.floor(Pi * R1 * R1)
		else:
			alpha = math.acos((R1 * R1 + d * d - R2 * R2) / (2 * R1 * d)) * 2
			beta = math.acos((R2 * R2 + d * d - R1 * R1) / (2 * R2 * d)) * 2
			a1 = 0.5 * beta * R2 * R2 - 0.5 * R2 * R2 * math.sin(beta)
			a2 = 0.5 * alpha * R1 * R1 - 0.5 * R1 * R1 * math.sin(alpha)
			ans = math.floor(a1 + a2)
		return ans


class Solution:

	def intersectionArea(self, X1, Y1, R1, X2, Y2, R2):
		d = math.sqrt((X1 - X2) ** 2 + (Y1 - Y2) ** 2)
		if d >= R1 + R2:
			area = 0
		elif d + min(R1, R2) <= max(R1, R2):
			area = 3.14 * min(R1, R2) ** 2
		else:
			theta1 = 2 * math.acos((R1 * R1 + d * d - R2 * R2) / (2 * R1 * d))
			arc1 = theta1 * R1 ** 2 * 0.5
			tri1 = R1 ** 2 * math.sin(theta1) * 0.5
			area1 = arc1 - tri1
			theta2 = 2 * math.acos((R2 * R2 + d * d - R1 * R1) / (2 * R2 * d))
			arc2 = theta2 * R2 ** 2 * 0.5
			tri2 = R2 ** 2 * math.sin(theta2) * 0.5
			area2 = arc2 - tri2
			area = area1 + area2
		return area


class Solution:

	def intersectionArea(self, X1, Y1, R1, X2, Y2, R2):
		P = 3.14
		d = math.sqrt((X2 - X1) * (X2 - X1) + (Y2 - Y1) * (Y2 - Y1))
		if d > R1 + R2:
			ans = 0
		elif d <= abs(R2 - R1) and R1 >= R2:
			ans = P * R2 * R2
		elif d <= abs(R2 - R1) and R2 >= R1:
			ans = P * R1 * R1
		else:
			alpha = math.acos((R1 * R1 + d * d - R2 * R2) / (2 * R1 * d)) * 2
			beta = math.acos((R2 * R2 + d * d - R1 * R1) / (2 * R2 * d)) * 2
			a1 = 0.5 * beta * R2 * R2 - 0.5 * R2 * R2 * math.sin(beta)
			a2 = 0.5 * alpha * R1 * R1 - 0.5 * R1 * R1 * math.sin(alpha)
			ans = a1 + a2
		ans = int(ans)
		return ans


class Solution:

	def intersectionArea(self, X1, Y1, R1, X2, Y2, R2):
		P = 3.14
		d = math.sqrt((X2 - X1) * (X2 - X1) + (Y2 - Y1) * (Y2 - Y1))
		if d > R1 + R2:
			ans = 0
		elif d <= abs(R2 - R1) and R1 >= R2:
			ans = P * R2 * R2
		elif d <= abs(R2 - R1) and R2 >= R1:
			ans = P * R1 * R1
		else:
			alpha = math.acos((R1 * R1 + d * d - R2 * R2) / (2 * R1 * d)) * 2
			beta = math.acos((R2 * R2 + d * d - R1 * R1) / (2 * R2 * d)) * 2
			a1 = 0.5 * beta * R2 * R2 - 0.5 * R2 * R2 * math.sin(beta)
			a2 = 0.5 * alpha * R1 * R1 - 0.5 * R1 * R1 * math.sin(alpha)
			ans = a1 + a2
		ans = int(ans)
		return ans
import math
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(X1, Y1, R1, X2, Y2, R2) = map(int, input().strip().split(' '))
		ob = Solution()
		ans = ob.intersectionArea(X1, Y1, R1, X2, Y2, R2)
		ans = int(ans)
		print(ans)


class Solution:

	def intersectionArea(self, X1, Y1, R1, X2, Y2, R2):
		pi = 3.14
		d = math.sqrt((X1 - X2) ** 2 + (Y1 - Y2) ** 2)
		if d >= R1 + R2:
			return 0
		elif d <= abs(R1 - R2):
			if R1 >= R2:
				return round(pi * R2 ** 2, 6)
			else:
				return round(pi * R1 ** 2, 6)
		else:
			a = R1 ** 2 * math.acos((d ** 2 + R1 ** 2 - R2 ** 2) / (2 * d * R1))
			b = R2 ** 2 * math.acos((d ** 2 + R2 ** 2 - R1 ** 2) / (2 * d * R2))
			c = 0.5 * math.sqrt((-d + R1 + R2) * (d + R1 - R2) * (d - R1 + R2) * (d + R1 + R2))
			return round(a + b - c, 6)


class Solution:

	def intersectionArea(self, x1, y1, r1, x2, y2, r2):
		p = 3.14
		d = math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))
		if d > r1 + r2:
			return 0
		elif d <= abs(r2 - r1) and r1 >= r2:
			return p * r2 * r2
		elif d <= abs(r2 - r1) and r2 >= r1:
			return p * r1 * r1
		else:
			alph = math.acos((r1 * r1 + d * d - r2 * r2) / (2 * r1 * d)) * 2
			bet = math.acos((r2 * r2 + d * d - r1 * r1) / (2 * r2 * d)) * 2
			a1 = 0.5 * bet * r2 * r2 - 0.5 * r2 * r2 * math.sin(bet)
			a2 = 0.5 * alph * r1 * r1 - 0.5 * r1 * r1 * math.sin(alph)
			return int(a1 + a2)