0

I have solved this problem but im not sure if its correct..

User should give the coordinates of a point and I should check if that point is within,outside or on the circle. I used the distance formula to solve this . The given information about the circle are:

Circle is centered at ( 0,0 ) and radius is 10

 public static void main(String[] strings) {


    Scanner scanner = new Scanner(System.in);

    System.out.println("Enter a point with two coordinates");
    double y1 = scanner.nextDouble();
    double x1 = scanner.nextDouble();

    //  circle is centered at 0,0
    double y2 = 0;
    double x2 = 0;

    double radius = 10;

    double distance;
    // using the distance formula to calculate the distance
    // from the point given from the user and point where circle is centered

    /**
     * distance formula
     *  d = √ ( x2 - x1 )^2 + (y2 - y1 )^2
     */

    distance = Math.pow( x2 - x1,2) + Math.pow(y2-y1,2);

    // find square root
    distance = Math.sqrt(distance);

    String result="";

    if(distance < radius) {
        result = "("+y1+" , "+x1+")" +" is within the circle";
    }
    else if(distance > radius) {
        result = y1+" , "+x1 +" is outside the circle";
    }
    else if(distance == radius) {
        result =y1+" , "+x1 +" is on the circle";
    }

    System.out.println(result);

}
weston
  • 54,145
  • 21
  • 145
  • 203
Evi Tinou
  • 23
  • 1
  • 2

3 Answers3

3

It's fine but sloppy.

There's no need to compute the square root. Work in units of distance squared.

Then compare using distance < radius * radius etc., perhaps renaming distance for the sake of clarity. Computing square roots is costly and imprecision can creep in which can be difficult to control. This is particularly important in your case where you want to test for a point being on the circle's edge.

Also consider writing (x2 - x1) * (x2 - x1) rather than using pow for the second power. Although Java possibly (I never remember for sure which is certainly a good enough reason for my not using it) optimises to the longer form, other languages (such as C) don't and imprecision can creep in there too.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

Consider using Math.hypot() to calculate a distance and compare double values using some small threshold:

static final double DELTA = 1E-5;  // not necessarily 1E-5; see comments

//...

distance = Math.hypot(x2 - x1, y2 - y1);    
if (Math.abs(distance - radius) < DELTA)
   // on the circle
else if (distance > radius)
   // outside
else
   // inside

The reason for using DELTA is that there is a very small chance to get equal double values by calculations. If they differ in at least one bit, direct comparison will return false.

By appliying threshold, you check not whether the point lays exactly on the circle, but whether it lays inside a ring between radius - DELTA and radius + DELTA. So DELTA is a kind of tolerance limit which value should be chosen particularily for application, e. g. depending on absolute or relative input inaccuracy.

Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67
  • And the *science* behind that choice of `DELTA` is? – Bathsheba Mar 10 '16 at 10:11
  • @Bathsheba sorry, I'm not going to fall into long discussion. You downvoted thus expressing your attitude to my answer and explanation, and I accepted it. – Alex Salauyou Mar 10 '16 at 10:24
  • Um. I didn't downvote; not yet anyway. I'm merely inviting you to discuss your choice of `DELTA`. For a small circle, that's a very thick edge ;-) – Bathsheba Mar 10 '16 at 10:27
  • @Bathsheba okay, sorry for that. No particular reason, just some number that covers input inaccuracy (I believe a user won't enter numbers with more than 5 decimal digits, anyway, it should be selected and explained in every particular case). – Alex Salauyou Mar 10 '16 at 10:32
  • @Bathsheba and I think you agree that for using `==` with doubles one must deeply understand their internal structure. – Alex Salauyou Mar 10 '16 at 10:34
  • 1
    Absolutely. Your approach would be better (even upvoteble) if the 'delta' was linear on the radius of the circle. – Bathsheba Mar 10 '16 at 10:38
  • Given distance and radius will be positive, why the `Math.Abs`? – weston Mar 10 '16 at 11:03
  • @weston if `distance < radius` => `distance - radius < 0` making statement true for any `DELTA >= 0` – Alex Salauyou Mar 10 '16 at 11:12
0

Are you sure this question requires doubles as input? The examples given are integers. With integers you can be sure of a points location, with real numbers (doubles) you cannot be sure about the "on circle" or not which is another reason I think the question expects you to use integers.

And the trick for performance and accuracy is to not use Math.sqrt and only work with integers.

int x;
int y;
int centreX;
int centreY;

int deltaX = x - centreX;
int deltaY = y - centreY;

int distanceSquared = deltaX * deltaX + deltaY * deltaY;

int radiusSquared = radius * radius;

if (distanceSquared == radiusSquared) { //distance == radius
  //on circle
} else if (distanceSquared < radiusSquared) { //distance < radius
  //in circle
} else {
  //out of circle
}
weston
  • 54,145
  • 21
  • 145
  • 203