[java] I want to calculate the distance between two points in Java

OK, so I've written most of a program that will allow me to determine if two circles overlap.

I have no problems whatsoever with my program aside from one issue: the program won't accept the code I've written for the distance between the two center points. I can figure out the if/else logic to tell the user what happens depending on the value of distance later, but I want to know what's wrong now. Eclipse, the program I'm coding on, is telling me that distance should be resolved to an array, but I've already told you that it's an int.

Here is my code:

package circles;
import java.util.Scanner;

public class MathCircles {    
    // variable for the distance between the circles' centers
    public static int distance;

    // variable for the lengths of the radii combined
    public static int radii;

    public static void main(String[] args) {
        // Get the x-value of the center of circle one
        System.out.println("What is the x-coordinate for the center of circle one?");
        Scanner keyboard = new Scanner(System.in);
        int x1 = keyboard.nextInt();

        //Get the y-value of the center of circle one
        System.out.println("What is the y-coordinate for the center of circle one?");
        Scanner keyboard1 = new Scanner(System.in);
        int y1 = keyboard1.nextInt();

        //Get the radius length of circle one.
        System.out.println("How long is circle one's radius?");
        Scanner keyboard2 = new Scanner(System.in);
        int r1 = keyboard2.nextInt();

        // Get the x-value of the center of circle two.
        System.out.println("What is the x-coordinate for the center of circle two?");
        Scanner keyboard3 = new Scanner(System.in);
        int x2 = keyboard3.nextInt();

        //Get the y-value of the center of circle two.
        System.out.println("What is the y-coordinate for the center of circle two?");
        Scanner keyboard4 = new Scanner(System.in);
        int y2 = keyboard4.nextInt();

        //Get the radius length of circle two.
        System.out.println("How long is circle two's radius?");
        Scanner keyboard5 = new Scanner(System.in);
        int r2 = keyboard5.nextInt();

        /*
         * OK, so now I have the location of the two circles' centers,
         * as well as the lengths of their radii.
         * The circles are intersecting IF THE DISTANCE BETWEEN THE TWO CENTERS
         * IS EQUAL TO OR LESS THAN THE COMBINED LENGTHS OF THE RADII.
         * Now I need to get some math done.
         */

        //calculate the combined lengths of the radii

        radii = r1 + r2;

        //calculate the distance
        distance = Math.sqrt((x1-x2)(x1-x2) + (y1-y2)(y1-y2));

    }    
}

This question is related to java math distance

The answer is


You need to explicitly tell Java that you wish to multiply.

(x1-x2) * (x1-x2) + (y1-y2) * (y1-y2)

Unlike written equations the compiler does not know this is what you wish to do.


Unlike maths-on-paper notation, most programming languages (Java included) need a * sign to do multiplication. Your distance calculation should therefore read:

distance = Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));

Or alternatively:

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

Based on the @trashgod's comment, this is the simpliest way to calculate distance:

double distance = Math.hypot(x1-x2, y1-y2);

From documentation of Math.hypot:

Returns: sqrt(x²+ y²) without intermediate overflow or underflow.


Math.sqrt returns a double so you'll have to cast it to int as well

distance = (int)Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));


You could also you Point2D Java API class:

public static double distance(double x1, double y1, double x2, double y2)

Example:

double distance = Point2D.distance(3.0, 4.0, 5.0, 6.0);
System.out.println("The distance between the points is " + distance);

Based on the @trashgod's comment, this is the simpliest way to calculate >distance:

double distance = Math.hypot(x1-x2, y1-y2); From documentation of Math.hypot:

Returns: sqrt(x²+ y²) without intermediate overflow or underflow.

Bob

Below Bob's approved comment he said he couldn't explain what the

Math.hypot(x1-x2, y1-y2);

did. To explain a triangle has three sides. With two points you can find the length of those points based on the x,y of each. Xa=0, Ya=0 If thinking in Cartesian coordinates that is (0,0) and then Xb=5, Yb=9 Again, cartesian coordinates is (5,9). So if you were to plot those on a grid, the distance from from x to another x assuming they are on the same y axis is +5. and the distance along the Y axis from one to another assuming they are on the same x-axis is +9. (think number line) Thus one side of the triangle's length is 5, another side is 9. A hypotenuse is

 (x^2) + (y^2) = Hypotenuse^2

which is the length of the remaining side of a triangle. Thus being quite the same as a standard distance formula where

 Sqrt of (x1-x2)^2 + (y1-y2)^2 = distance 

because if you do away with the sqrt on the lefthand side of the operation and instead make distance^2 then you still have to get the sqrt from the distance. So the distance formula is the Pythagorean theorem but in a way that teachers can call it something different to confuse people.


This may be OLD, but here is the best answer:

    float dist = (float) Math.sqrt(
            Math.pow(x1 - x2, 2) +
            Math.pow(y1 - y2, 2) );

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to math

How to do perspective fixing? How to pad a string with leading zeros in Python 3 How can I use "e" (Euler's number) and power operation in python 2.7 numpy max vs amax vs maximum Efficiently getting all divisors of a given number Using atan2 to find angle between two vectors How to calculate percentage when old value is ZERO Finding square root without using sqrt function? Exponentiation in Python - should I prefer ** operator instead of math.pow and math.sqrt? How do I get the total number of unique pairs of a set in the database?

Examples related to distance

Function to calculate distance between two coordinates I want to calculate the distance between two points in Java How do I find the difference between two values without knowing which is larger? Should I use px or rem value units in my CSS? Calculating distance between two geographic locations Haversine Formula in Python (Bearing and Distance between two GPS points) Shortest distance between a point and a line segment Calculate distance in meters when you know longitude and latitude in java