fork download
  1. import java.lang.Math;
  2.  
  3. public class Main {
  4. public static void main (String[] args) {
  5. //r^2 = (x - h)^2 + (y - k)^2
  6. //in order of r, h, k
  7. double[] equation = {5.0D, 4.0D, 2.0D};
  8. double epsilon = 0.0001; //acceptable error
  9.  
  10. int[][] pointsToTest = {{-3, 4}, {0, 2}, {1, 9}, {0, 5}};
  11. for (int ai = 0; ai < pointsToTest.length; ai++) {
  12. System.out.println("Point (" + pointsToTest[ai][0] + ", " + pointsToTest[ai][1] + ") is " + relationshipToCircle(epsilon, equation, pointsToTest[ai][0], pointsToTest[ai][1]) + ".");
  13. }
  14. }
  15.  
  16. public static String relationshipToCircle(double error, double[] eq, int x, int y) {
  17. double rsq = eq[0] * eq[0];
  18. double h = eq[1];
  19. double k = eq[2];
  20. double eval = Math.pow((x - h), 2) + Math.pow((y - k), 2);
  21.  
  22. if (Math.abs(eval - rsq) < error) { //difference in values negligible
  23. return "on the circle";
  24. } else if (eval > rsq) { //bigger than circle's radius so it's ouside
  25. return "outside of the circle";
  26. } else { //not outside, not on, so inside
  27. return "inside the circle";
  28. }
  29. }
  30. }
Success #stdin #stdout 0.03s 245632KB
stdin
Standard input is empty
stdout
Point (-3, 4) is outside of the circle.
Point (0, 2) is inside the circle.
Point (1, 9) is outside of the circle.
Point (0, 5) is on the circle.