Question:
Assume that there are 2 circles which are intersecting at 2 points, as given in the figure below.
The points P and Q refer to the centre of each circle while a and b represent the radius of each of the circles. The angle α and β are formed between the lines drawn from the points at which circles intersect to the centres of the respective circles. Based on this, you are expected to write a program to find the area common to both the circles(highlighted in yellow in the figure above).
Consider the input values as a=10 and b=8 depicting the radius of each of the circles and α=45o and β=30o are the angles from the centre. Based on trigometric and mensuration formulae, it can be determined that the area of intersection is approximately 4.6697. then, taking the cell value of it, would yield us the value 5.
Function Description
Complete the function findInterSectionArea in the editor below. The function must state what must be returned or printed.
findInterSectionArea has the following parameter(s):
alpha : the angle α
radiusA: the radius of first circle (the left one as in figure above)
beta : the angle β
radius : the radius of second circle ( the right one as in figure above)
The final output of the function should be the cell value of the resultant of the calculations.
Constraints
0<alpha(α)<=90
0<beta(β)<=90
0<radiusA<=20
0<radius<=20
All the input values are integers.
Should any of the above constraints be violated.
then the program should show the below output:
Invalid input
Sample Case 0:
Sample Input For Custom Testing
45
10
30
8
Sample Output
5
Explanation
After applying the trigonometric and mensuration formulae, the above value can be achieved.
Sample Case 0:
Sample Input For Custom testing
45
10
110
20
Sample Output
Invalid input
Explanation
The value of angle beta violates the constraint as mentioned above. Thus, the above output.
Input:
45
10
30
8
Output:
5
Solution:
Python :
#https://codewindow.in
#join our telegram channel @codewindow
import math
alpha = int(input())
beta = int(input())
radA = int(input())
radB = int(input())
if((alpha < 0 and alpha > 90) or (beta < 0 and beta > 90) or (radA < 0 and radA > 20) or (radB < 0 and radB > 20)):
print("Invalid Input")
else:
a = alpha*3.14/180
b = beta*3.14/180
a1 = 0.5 * b * radB * radB - 0.5 * radB * radB * math.sin(b)
a2 = 0.5 * a * radA * radA - 0.5 * radA * radA * math.sin(a)
ans = math.ceil(a1 + a2)
print(ans)
JAVA :
//https://codewindow.in
//join our telegram channel @codewindow
import java.util.Arrays;
import java.util.Scanner;
import java.io.*;
public class Main {
public static void main(String args[]) throws Exception
{
Scanner sc = new Scanner(System.in);
int alpha = sc.nextInt();
int beta = sc.nextInt();
int radA = sc.nextInt();
int radB = sc.nextInt();
if((alpha < 0 && alpha > 90) || (beta < 0 && beta > 90) || (radA < 0 && radA > 20) || (radB < 0 && radB > 20))
System.out.println("Invalid Input");
else {
double a1, a2;
double a = alpha*3.14/180;
double b = beta*3.14/180;
a1 = (double)0.5 * b * radB * radB - 0.5 * radB * radB * Math.sin(b);
a2 = (double)0.5 * a * radA * radA - 0.5 * radA * radA * Math.sin(a);
long ans = (long)(a1 + a2);
System.out.println(ans);
}
}
}
C++ :
//https://codewindow.in
//join our telegram channel @codewindow
#include<iostream>
#include<math.h>
using namespace std;
int main() {
int alpha, beta, radA, radB;
cin >> alpha;
cin >> beta;
cin >> radA;
cin >> radB;
if((alpha < 0 && alpha > 90) || (beta < 0 && beta > 90) || (radA < 0 && radA > 20) || (radB < 0 && radB > 20))
cout << "Invalid Input";
else {
double a1, a2;
double a = alpha*3.14/180;
double b = beta*3.14/180;
a1 = (double)0.5 * b * radB * radB - 0.5 * radB * radB * sin(b);
a2 = (double)0.5 * a * radA * radA - 0.5 * radA * radA * sin(a);
long ans = (long)(a1 + a2);
cout << ans;
}
return 0;
}
Also Checkout