Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

We wish to estimate the area of the intersection of two overlapping circles with

ID: 665607 • Letter: W

Question

We wish to estimate the area of the intersection of two overlapping circles with diameters d1 and d2, whose centres are separated by x. We can do this by a simple sampling procedure. Imagine the two circles are enclosed in a rectangle with width d1+d2 and height dk, where dk is the greater of d1 and d2: 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 di, double d2, double x, int n) that implements the above procedure. You use an object from the library class java.util.Random to generate a sequence of should random points inside the rectangle, and you will need the following method from that class public double next Double 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.*;
public class Fitness {

    // Method needed to check if a point lies inside the circle
    public static boolean checkPointInCircle(double center,double radius,double pointX,double pointY)
    {
        double distanceFromCenter=Math.sqrt(pointY*pointY +(pointX- center)*(pointX- center));
        if(distanceFromCenter<radius)
            return true;
        else
            return false;
    }
    public static double area(double d1, double d2,double x, int n)
    {
        int numberOfInteresectionPoints=0;
        int numberOfOutsidePoints=0;
        double width=d1+d2;
        double height= Math.max(d1,d2);
        double center1=d1/2;
        double center2=center1+x;
        Random r = new Random();
        for(int i=0;i<n;i++)
        {
            double pointX=r.nextDouble()*width;
            double pointY=(r.nextDouble()*height) - (height/2);
            if(checkPointInCircle(center1,d1/2,pointX,pointY) && checkPointInCircle(center2,d2/2,pointX,pointY))
            {
                numberOfInteresectionPoints+=1;
            }
            else
            {
                numberOfOutsidePoints+=1;
            }
        }
        double areaOfRectangle=(height*width);
        double areaOfIntersection=(areaOfRectangle*numberOfInteresectionPoints)/numberOfOutsidePoints;
        return areaOfIntersection;
    }
    public static void main(String argv[])
    {
        System.out.println(area(3,4,3,1000));
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote