In the previous article, we have seen Java Program to Check If Given Four Points Form a Square
In this article, we will check whether a point exists in a circle sector or not by using Java programming language.
Java Program to Check Whether a Point Exists in Circle Sector or Not
Before jumping into the program directly, let’s first know how to check whether a point exists in a circle sector or not.
Explanation:
Suppose, we have a circle centered at coordinates (0,0), the starting angle of the circle sector and the size of the circle sector in percentage.
Then we need to find out polar coordinates of that point and then go through the following steps:
- Converting x, y to polar coordinates.
- Check polarradius is less then radius of circle.
- Angle is between startAngle and endAngle.
Note : We can calculate by using Formulas –
- EndingAngle = 360 / percentage + StartingAngle ;
- double polarradius = Math.sqrt(x*x+y*y);
- double Angle = Math.atan(y/x);
Approach:
- Create
checkPoint()
method and find the endAngle, polarRadius, and Angle using the above formulas. - Check the condition if it is true which means points exist in the circle else do not exist.
- Call the
checkPoint()
method in the main() method and display the output.
Program:
class Main { //main method public static void main(String arg[]) { int radius = 8; int x = 3; int y = 4; float startAngle = 0; float percent = 12; //calling the checkPoint() method checkPoint(radius, x, y, percent, startAngle); } //user defined method static void checkPoint(int radius, int x, int y, float percent, float startAngle) { //find endAngle float endAngle = 360/percent + startAngle; //find polar co-ordinates double polarRadius = Math.sqrt(x*x+y*y); double Angle = Math.atan(y/x); // Checking whether polarradius is less then radius of circle or not // and checking whether Angle is between startAngle and endAngle or not if (Angle>=startAngle && Angle<=endAngle && polarRadius<rad) System.out.print("Point"+"("+x+","+y+")"+ " exist in the circle sector"); else System.out.print("Point"+"("+x+","+y+")"+ " exist in the circle sector"); } }
Output: Point(3,4) exist in the circle sector
If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.
Related Java Articles:
- Java Program to Solve Pizza Cut Problem(Circle Division by Lines)
- Java Program to Find Minimum Revolutions to Move Center of a Circle to a Target
- Java Program to Check if a Line Touches or Intersects a Circle
- Java Program to Check if a Given Circle Lies Completely Inside the Ring Formed by Two Concentric Circle