Java Program to Check if Point is Inside or Outside a Circle

«
»
This is a Java Program to check whether a point lies inside, outside or on the circle. For any point t (xt, yt) on the plane, its position with respect to the circle defined by 3 points (x1, y1) , (x2, y2), (x3, y3).
s = (x-xt)^2 + (y-yt)^2 – r*r
If s < 0, t lies inside the circle; if s > 0, t lies outside the circle; if s = 0, t lies on the circle.

Here is the source code of the Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Plane. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a java program to check whether point d lies inside or outside the circle defined by a, b, c points
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. public class Position_Point_WRT_Circle
  6. {
  7.     public static void main(String args[])
  8.     {
  9.         Random random = new Random();
  10.  
  11.         int x1, y1, x2, y2, x3, y3;
  12.         double m1, m2, c1, c2, r;
  13.  
  14.         x1 = random.nextInt(10);
  15.         y1 = random.nextInt(10);
  16.         x2 = random.nextInt(10);
  17.         y2 = random.nextInt(10);
  18.         x3 = random.nextInt(10);
  19.         y3 = random.nextInt(10);
  20.  
  21.         m1 = (y1 - y2) / (x1 - x2);
  22.         m2 = (y3 - y2) / (x3 - x2);
  23.  
  24.         c1 = ((m1 * m2 * (y3 - y1)) + (m1 * (x2 + x3)) - (m2 * (x1 + x2)))
  25.                 / (2 * (m1 - m2));
  26.         c2 = ((((x1 + x2) / 2) - c1) / (-1 * m1)) + ((y1 + y2) / 2);
  27.         r = Math.sqrt(((x3 - c1) * (x3 - c1)) + ((y3 - c2) * (y3 - c2)));
  28.  
  29.         System.out.println("The points on the circle are: (" + x1 + ", " + y1
  30.                 + "), (" + x2 + ", " + y2 + "), (" + x3 + ", " + y3 + ")");
  31.         System.out.println("The center of the circle is (" + c1 + ", " + c2
  32.                 + ") and radius is " + r);
  33.  
  34.         System.out.println("Enter the point : <x>,<y>");
  35.         Scanner scan = new Scanner(System.in);
  36.         int x, y;
  37.         x = scan.nextInt();
  38.         y = scan.nextInt();
  39.  
  40.         double s = ((x - c1) * (x - c1)) + ((y - c2) * (y - c1)) - (r * r);
  41.         if (s < 0)
  42.             System.out.println("The point lies inside the circle");
  43.         else if (s > 0)
  44.             System.out.println("The point lies outside the circle");
  45.         else
  46.             System.out.println("The point lies on the circle");
  47.         scan.close();
  48.     }
  49. }

Output:

advertisement
$ javac Position_Point_WRT_Circle.java
$ java Position_Point_WRT_Circle
 
The points on the circle are: (5, 9), (4, 7), (2, 0)
The center of the circle is (34.5, 23.25) and radius is 39.960136386153636
Enter the point : <x>,<y>
3 5
The point lies inside the circle
 
The points on the circle are: (0, 1), (2, 3), (5, 4)
The center of the circle is (3.5, 4.5) and radius is 1.5811388300841898
Enter the point : <x>,<y>
1 2
The point lies outside the circle

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.