In the previous article, we have seen Java Program to Check if a Line Touches or Intersects a Circle
In this article we will discuss about how to check if a given circle lies completely inside the ring formed by two concentric circle using Java programming language.
Java Program to Check if a Given Circle Lies Completely Inside the Ring Formed by Two Concentric Circle
Before jumping into the program directly, let’s first know how can we Check if a Given Circle Lies Completely Inside the Ring Formed by Two Concentric Circle
Explanation:
r = radius of smaller concentric circle R = radius of Bigger concentric circle r1 = radius of the circle to be checked dist = distance between the origin and center of the circle Note: The concentric circles are at coordinate(0,0). If (dist+r1 = R) and (dist-r1 = r) then the circle lies inside the ring.
Example:
When r=4, R=8 and r1=2, Center(6,0)
Distance = sqrt(x*x+y*y)
= sqrt(36+0)
=6
6-2 = 4(r) and 6+2 = 8(R)
Hence it lies inside the ring.
Let’s see different ways to check if a given circle lies completely inside the ring.
Method-1: Java Program to Check if a Given Circle Lies Completely Inside the Ring Formed by Two Concentric Circle By Using Static Value
Approach:
- Declare the value for all three radiuses and the coordinate of the center.
- Then call the
checkCircle()
user defined method by passing all the values as parameter. - In this method the it checks if the distance of the center from the origin and compares it with the radius of the concentric circles.
- Then print the result.
Program:
import java.util.Scanner; import java.awt.Point; import static java.lang.Math.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); //Static initialization int r = 4, R = 8, r1 = 2; Point circle = new Point(6,0); // Prints the result if(circleCheck(r,R,r1,circle)) System.out.println("The circle is inside the ring"); else System.out.println("The circle is outside the ring"); } //circleCheck() method public static boolean circleCheck(int r, int R, int r1, Point circle) { // Uses pythagoras theorem to calculate the distance of the circle from origin int distance = (int)Math.sqrt(circle.x*circle.x + circle.y*circle.y); // Checks the condition and returns true or false return (distance - r1 >= r && distance + r1 <= R); } }
Output: The circle is inside the ring
Method-2: Java Program to Check if a Given Circle Lies Completely Inside the Ring Formed by Two Concentric Circle By User Input Value
Approach:
- Take user input for all three radiuses and the coordinate of the center.
- Then call the
checkCircle()
user defined method by passing all the values as parameter. - In this method the it checks if the distance of the center from the origin and compares it with the radius of the concentric circles.
- Then print the result.
Program:
import java.awt.Point; import java.util.Scanner; import static java.lang.Math.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); //Asking the user for input System.out.println("Enter the radiuses of the small, big and the circle to be checked"); int r = scan.nextInt(), R = scan.nextInt(), r1 = scan.nextInt(); System.out.println("Enter the coordinates of the center of the circle to be checked"); Point circle = new Point(scan.nextInt(),scan.nextInt()); // Prints the result if(circleCheck(r,R,r1,circle)) System.out.println("The circle is inside the ring"); else System.out.println("The circle is outside the ring"); } //circleCheck() method public static boolean circleCheck(int r, int R, int r1, Point circle) { // Uses pythagoras theorem to calculate the distance of the circle from origin int distance = (int)Math.sqrt(circle.x*circle.x + circle.y*circle.y); // Checks the condition and returns true or false return (distance - r1 >= r && distance + r1 <= R); } }
Output: Case-1 Enter the radiuses of the small, big and the circle to be checked 4 8 2 Enter the coordinates of the center of the circle to be checked 5 0 The circle is outside the ring Case-2 Enter the radiuses of the small, big and the circle to be checked 4 8 2 Enter the coordinates of the center of the circle to be checked 6 0 The circle is inside the ring
Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.
Related Java Articles:
- Java Program to Find Area of a Circumscribed Circle of a Square
- Java Program to Find Center of the Circle Using Endpoints of Diameter
- Java Program to Find Arc Length from Given Angle
- Java Program to Find Circumference of a Circle
- Java Program to Check Whether Given Circle Resides in Boundary Maintained by Two Other Circle