We wish to estimate the area of the intersection of two overlapping circles with
ID: 663314 • Letter: W
Question
We wish to estimate the area of the intersection of two overlapping circles with diameters dl and d2, whose centres are separated by z. We can do this by a simple sampling procedure. Imagine the two circles arc enclosed in a rectangle with width d1+d2 and height dk, where dk is the greater of dl and d2: [Dl Generate n points randomly in this rectangle, and count how many points sit in the intersection (i.e. inside both circles). The proportion of points that sit in the intersection approximates A/B, where A is the area of the intersection, and B is the area of the rectangle. From this we can estimate A. Clearly the precision of the estimate increases with the number of samples. Write a method public double area(double dl, double d2, double x, int n) that implements the above procedure. You should use an object from the library class java .until .Random to generate a sequence of random points inside the rectangle, and you will need the following method from that class. public double nextDouble() Returns the next pseudorandom, uniformly-distributed double between 0.0 and 1.0 from this random number generator?s sequence.Explanation / Answer
import java.util.Random;
import java.util.Scanner;
public class AreaOfIntersection {
public double area(double d1, double d2, double x, int n)
{
double width=d1+d2;
double hight=0;
double []xc=new double[n]; // to store x-coordinate of random point
double []yc=new double[n]; // to store y-coordinate of random point
Random r=new Random(); // object of random class
double A=0;// area of intersection
if(d1>d2)
{
hight=d1;
}
else
hight=d2;
// Area of rectangle is
double B=width*hight;
// generating points
for(int i=0;i<n;i++)
{
xc[i]=r.nextDouble()*10;
yc[i]=r.nextDouble()*10;
}
// let center of the circle (0,0) and (x,0)
int count=0;
for (int i=0;i<n;i++)
{
boolean temp1=false,temp2=false;
// whether point lies inside or out side the circle
// using x^2+y^2<r^2
if(((yc[i]-d1/2)*(yc[i]-d1/2)+(xc[i]-d1/2)*(xc[i]-d1/2))<((d1/2)*(d1/2)))
temp1=true;
if(((yc[i]-d1/2)*(yc[i]-d1/2)+(xc[i]-(x+d1/2))*(xc[i]-(x+d1/2)))<((d2/2)*(d2/2)))
temp2=true;
if(temp1&&temp2)
{
count++; // Cunting the points which lies inside the intersection
}
}
// as count is proportional to A/B
// so A=count*B;
A=count*B;
return A;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
// reading input
double d1=sc.nextDouble();
double d2=sc.nextDouble();
double x=sc.nextDouble();
int n=sc.nextInt();
double A=0;
// creating instance of method
AreaOfIntersection ob=new AreaOfIntersection();
A=ob.area(d1, d2, x, n);
System.out.println(A);
}
}
output: (According to given in question Count is Approximates to (A/B) .....so A=count*B.......where A is the intersection area.....if I interpreted wrong then answer may vary .....please make change only in formula accordingly )
d1=5
d2=4
x=2
n=100
A=360.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.