Java Program to Find Minimum Revolutions to Move Center of a Circle to a Target

In the previous article, we have discussed about Java Program to Find Equation of Circle From Radius and Center

In this article we are going to see how to find minimum revolutions to move center of a circle to a target using Java programming language.

Java Program to Find Minimum Revolutions to Move Center of a Circle to a Target

Before Jumping into the program directly let’s see how to find minimum revolutions to move center of a circle to a target.

Explanation:

Given there is a circle of radius r

Center of the circle is (x1, y1)

Another given point is (x2, y2).

Now, The task is to move center of circle from given center (x1, y1) to target (x2, y2) using minimum number of steps. To move the center with maximum distance we need to rotate it around the line and with 180 degrees. So the maximum distance we can move the center each time = 2r.

Let’s continue moving the center with 2r distance each time until the two circles intersects.

Now we can make the center moves into its final position by rotating it around one of the intersection points of the two circles until it reaches the final destination.
Every time we make the circle moves 2r times except the last move it’ll be less than 2r.

Let the initial distance between the two points be d.

Minimum revolutions to move center of a circle to a target will be ceil(d/2*r).

Let’s see different ways to find minimum revolutions to move center of a circle to a target.

Method-1: Java Program to Find Minimum Revolutions to Move Center of a Circle to a Target By Using Static Input Value

Approach:

  • Declare an double variable say ‘x1’ and assign the value to it, which holds the x coordinate of the center of the circle.
  • Declare an double variable say ‘y1’ and assign the value to it, which it holds the y coordinate of the center of the circle.
  • Declare an double variable say ‘x2’ and assign the value to it, which holds the x coordinate of the targeted point.
  • Declare an double variable say ‘y2’ and assign the value to it, which it holds the y coordinate of the targeted point.
  • Declare an double variable say ‘r’ and assign the value to it, which holds the radius of the circle.
  • Find the Min revolution to move center of a circle to a target using the formula ceil(d/2*r)
  • Print the result.

Program:

import java.io.*;
class Main
{
    public static void main(String [] args)
    {
        double x1 = 1;
        double y1 = 2;
        double x2 = 3;
        double y2 = 4;     
        double r =  5;
        // formula to find distance between the center and the targeted point
        double d = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
        // formula to find min revolution to move center of a circle to a targeted point
        double minRevolution = Math.ceil(d / (2 * r));
        System.out.println("Min revolution to move center of a circle to a target is " + (int)minRevolution);
    }
}
Output:

Min revolution to move center of a circle to a target is 1

Method-2: Java Program to Find Minimum Revolutions to Move Center of a Circle to a Target By Using User Input Value

Approach:

  • Declare an double variable say ‘x1’ and take the value as user input, which holds the x coordinate of the center of the circle.
  • Declare an double variable say ‘y1’ and take the value as user input, which it holds the y coordinate of the center of the circle.
  • Declare an double variable say ‘x2’ and take the value as user input, which holds the x coordinate of the targeted point.
  • Declare an double variable say ‘y2’ and take the value as user input, which it holds the y coordinate of the targeted point.
  • Declare an double variable say ‘r’ and take the value as user input, which holds the radius of the circle.
  • Find the Min revolution to move center of a circle to a target using the formula ceil(d/2*r)
  • Print the result.
import java.io.*;
import java.util.Scanner;
class Main
{
    public static void main(String [] args)
    {
         // scanner class obj ref 
        Scanner s = new Scanner(System.in);                              
        System.out.println("Enter the x coordinate of center of the circle");
       // to take user input value
        double x1 = s.nextDouble();                                         
        System.out.println("Enter the y coordinate of center of the circle");
        double y1 =  s.nextDouble();
        System.out.println("Enter the x coordinate of targeted point");
         // to take user input value
        double x2 = s.nextDouble();                                        
        System.out.println("Enter the y coordinate of targeted point");
        double y2 =  s.nextDouble();
        System.out.println("Enter the radius of center of the circle");
        double r =  s.nextDouble();
        // formula to find distance between the center and the targeted point
        double d = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
        // formula to find min revolution to move center of a circle to a targeted point
        double minRevolution = Math.ceil(d / (2 * r));
        System.out.println("Min revolution to move center of a circle to a target is " + (int)minRevolution);
    
    }
}
Output:

Enter the x coordinate of center of the circle
1
Enter the y coordinate of center of the circle
2
Enter the x coordinate of targeted point
3
Enter the y coordinate of targeted point
4
Enter the radius of center of the circle
5
Min revolution to move center of a circle to a target is 1

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output

Related Java Programs: