Check if a Point Exists in Circle Sector in Java


A circle is a closed shape formed by tracing a point that moves in a plane such that its distance from a given point is constant. In this article we will check how to form the equation of circle from given radius and centre.

We will be given a circle with point i.e (x, y), radius r and centre i.e (x1, y1). We need to form the equation of circle from given radius and centre. The equation of the circle is given by formula −

$$\mathrm{(x\:-\:x1)^2\:+\:(y\:-\:y1)^2\:=\:r^2}$$

Where,

  • (x, y) is a point.

  • (x1, y1) is centre.

  • And r is radius.

Now to check if a point exists in a circle sector the point must satisfy the following equation −

$$\mathrm{(x\:-\:x1)^2\:+\:(y\:-\:y1)^2\:<\:r^2}$$

Lets take the centre of the circle as (0, 0).

Let’s see how we can check if a point exists in circle sector by using Java programming language.

To show you some instances

Instance-1

  • Given inputs for points and radius are −

    • Points = (2, 6), Radius = 3

  • After checking the condition, the result will be −

    • Point does not exist in circle sector.

Instance-2

  • Given inputs for points and radius are −

    • Points = (8, 5), Radius = 11

  • After checking the condition, the result will be −

    • Point exists in circle sector.

Algorithm

  • Step-1 − Declare and initialize the variables.

  • Step-2 − Putting the values in formula.

  • Step-3 − Check for condition.

  • Step-4 − Print the result.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Inputs

  • By Using User Defined Method

Let’s see the program along with its output one by one.

Approach-1: By Using Static Input

In this approach, value of point and radius will be initialized in the program. Then as per the algorithm we will find if a point exists in circle sector.

Example

public class Main{
   //main method
   public static void main(String arr[]){
      
      //declaring variables
	  double x = 2, y = 6, r = 3;
      
      //applying logic
	  double m = x * x;
	  double n = y * y;
	  double o = r * r;
      double p = m + n;
      
      //checking the condition
      if (p < o) {
         
         //print if point lie inside circle
         System.out.println("Point exist in circle sector.");
      } else {
         
         //print if point does not lie inside circle
         System.out.println("Point does not exist in circle sector.");
      }
   }
}

Output

Point does not exist in circle sector.

Approach-2: By Using User Defined Method

In this approach, value of point and radius will be initialized in the program. Then call a user defined method by passing the given values and inside method as per the algorithm we will find if a point exists in circle sector.

Example

public class Main{  
   //main method
   public static void main(String arr[]){
      
      //declaring variables
	  double x = 8, y = 5, r = 11;
	   
      //calling user defined method
	  sector_circle(x, y, r);
   }
   
   //user defined method
   static void sector_circle(double x, double y, double r){
      
      //applying logic
	  double m = x * x;
	  double n = y * y;
	  double o = r * r;
      double p = m + n;
      
      //checking the condition
      if (p < o) {
         
         //print if point lie inside circle
         System.out.println("Point exists in circle sector.");
      } else {
         
         //print if point does not lie inside circle
         System.out.println("Point does not exist in circle sector.");
      }
   }
}

Output

Point exists in circle sector.

In this article, we explored different approaches to check if a point exists in a circle sector by using Java programming language.

Updated on: 04-May-2023

200 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements