# Thread: Calculating the distance between two points problem

1. Senior Member
Join Date
Feb 2009
Posts
182
Rep Power
12

## Calculating the distance between two points problem

Hi, this code is supposed to calculate the distance between two points, but I get the following compiler errors and don't know what to do. Any help greatly appreciated. There error seems to be a type error, that I tried to fix.Thank you. Derek

C:\JAVA_PROGRAMMING_CODE\code1\PointDistance>javac PointDistance.java
PointDistance.java:33: pow(double,double) in java.lang.Math cannot be applied to
(double)
insideRoot = Math.pow(x - a) + Math.pow(y - b);
^
PointDistance.java:33: pow(double,double) in java.lang.Math cannot be applied to
(double)
insideRoot = Math.pow(x - a) + Math.pow(y - b);
^
PointDistance.java:33: incompatible types
found : java.lang.String
required: double
insideRoot = Math.pow(x - a) + Math.pow(y - b);
^
3 errors
___________________________________________

the formula for Distance is :

square root of [(x2-x1)squared + (y2-y1)squared]

__________________________________________________ ____________
Java Code:
```import java.util.Scanner;

public class PointDistance
{

public static void main(String[] args)
{

int a, b, x, y;
double distance;
double insideRoot;

Scanner scan = new Scanner (System.in);

System.out.println("Enter the x coordinate for point 1: ");
a = scan.nextInt();

System.out.println("Enter the y coordinate for point 1: ");
b = scan.nextInt();

System.out.println("Enter the x coordinate for point 2: ");
x = scan.nextInt();

System.out.println("Enter the y coordinate for point 2: ");
y = scan.nextInt();

/*a = (double) a; //make double for power operation
b = (double) b;
x = (double) x;
y = (double) y;*/

insideRoot = Math.pow(x - a) + Math.pow(y - b);

distance = Math.sqrt(insideRoot);

System.out.println("The distance between the two points is " + distance + " .");

}

}```

2. Math.pow is exponentiation of two arguments, not squaring it. Also, scanner has a nextDouble method I believe.

3. Member
Join Date
May 2011
Posts
7
Rep Power
0
you need to initialize double inside root = 0;
then change your insideRoot line to insideRoot = Math.pow(x - a, insideRoot) + Math.pow(y - b, insideRoot);

you need to initialize double inside root = 0;
then change your insideRoot line to insideRoot = Math.pow(x - a, insideRoot) + Math.pow(y - b, insideRoot);
What good would delta x to the power of 0 + delta u to the power of 0 do?

5. Senior Member
Join Date
Feb 2009
Posts
182
Rep Power
12
Thanks, I tried to fix it according to your advice. Here is the new code and it seems to work. The good thing is I understand your solution. THANK GOD! LOL. There is nothing worse than getting a solution from the forum that I don't understand, like my last solution from the last book problem. So that is good news. Here is the new code. Thank you both for the help!!D

Java Code:
```import java.util.Scanner;

public class PointDistance
{

public static void main(String[] args)
{

double a, b, x, y;
double distance;
double insideRoot;

Scanner scan = new Scanner (System.in);

System.out.println("Enter the x coordinate for point 1: ");
a = scan.nextDouble();

System.out.println("Enter the y coordinate for point 1: ");
b = scan.nextDouble();

System.out.println("Enter the x coordinate for point 2: ");
x = scan.nextDouble();

System.out.println("Enter the y coordinate for point 2: ");
y = scan.nextDouble();

insideRoot = Math.sqrt(x - a) + Math.sqrt(y - b);

distance = Math.sqrt(insideRoot);

System.out.println("The distance between the two points is " + distance + " .");

}

}```

6. I don't believe that is correct, you used square root of delta x and delta y.

If you are unsure how this works I suggestion draw on paper and try to figure it out with a pen. Use small numbers to do it by hand.

Try these points, 5, 5 and 10, 15.

Pay attention to a few things, first, try to find what the 3rd point will be and plot it. The third point is (deltaX, deltaY), delta means the change of x or y. So calculate the 3rd point and plot it. Then see what kind of shape is produced.
Last edited by sunde887; 05-14-2011 at 03:55 PM.

7. Senior Member
Join Date
Feb 2009
Posts
182
Rep Power
12
You are right, I got confused between square root, and something squared. But I don't know how to put

insideRoot = (squared)(x - a) + (squared)(y - b);

insideRoot = Math.sqrt(x - a) + Math.sqrt(y - b);

I don't know how to work it out on paper, I have to read the algebra book still, I just ordered it.

8. Senior Member
Join Date
Feb 2009
Posts
182
Rep Power
12
I cheated. I looked it up on google "distance in java" LOL. Oh well, at least I know it now.

do you think this is correct? please. I don't have the algebra book yet.

Java Code:
```import java.util.Scanner;

//distance = square root of [(x2-x1)squared + (y2-y1)squared]
public class PointDistance
{

public static void main(String[] args)
{

double distance(double x1, double y1, double x2, double y2)
{
return math.sqrt((x1-x2)(x1-x2) + (y1-y2)(y1-y2));
}
double x1, x2, y, y2;
double distance;

Scanner scan = new Scanner (System.in);

System.out.println("Enter the x coordinate for point 1: ");
x1 = scan.nextDouble();

System.out.println("Enter the y coordinate for point 1: ");
y1 = scan.nextDouble();

System.out.println("Enter the x coordinate for point 2: ");
x2 = scan.nextDouble();

System.out.println("Enter the y coordinate for point 2: ");
y2 = scan.nextDouble();

distance = distance(x1,y1,x2,y2);

System.out.println("The distance between the two points is " + distance + " .");

}

}```
It seems to work in the command window.

Also, sunde887, do you think it is important that I be able to work these math problems out on paper? Or should I just use the formulas converted to java, and leave it at that? please. thanks. Derek
Last edited by silverglade; 05-14-2011 at 04:52 PM.

9. you don't know (x-a) * (x-a) + (y-b)*(y-b)?

edit: didn't see your post just above. Does this compile?
Java Code:
`return math.sqrt((x1-x2)(x1-x2) + (y1-y2)(y1-y2));`

10. There are some flaws still. Did you try using pen and paper? It may be helpful to calculate the delta of x and delta of y separately.

Take two points and on a piece of paper make a small graph and plot the points(use small points), is there any shape that can be made? (hint: it has 3 sides).

Here is a great link with illustrations.
The Distance Formula

Basically, finding the distance of 2 points breaks down to pythagoreans theorem. a^2 + b^2 = c^2. c^2 is the hypotenuse of the triangle you can form. So c(the length of the hypotenuse) is
Java Code:
`Math.sqrt(a^2 + b^2)`
What is a and b in these situations? It's the change of x and change of y between the two points. Perhaps it will be helpful to calculate the change separately. From there squaring and adding them is easy.

11. Senior Member
Join Date
Feb 2009
Posts
182
Rep Power
12
hehe oops. yes I do know how to subtract and then to square root that number with my calculator. That is the best I can do. It was just the formulas look intimidating to me, so I never think I can do it. Yes Fubarable that code above compiles, Ok I used a calculator and entered 2 for x1 and 4 for y1, difference is -2 squared which is 4.. then for x2 is 3 and y2 is 6, so difference is -3 squared which is 6. so 4 plus 6=10. square root of 10 is 3.16. Now I will try this with the program and see if it matches

Tried it with the app, doesn't work. The app gave me a solution of 1.55

edit: I did graph it out. It makes a diagonal line going up to the top right.

I just posted the post here, in case no one returns.
Last edited by silverglade; 05-14-2011 at 05:53 PM.

12. If you can do it by hand with small numbers it will always be much simpler to translate to java. Graphing on a piece of paper will be very helpful to figure out this problem. Basically the algorithm looks like this
Java Code:
```find delta x(x1 - x2)
find delta y(y1 - y1)
square delta x(x * x)
square delta y(y * y)
find the square root of that sum```

13. Senior Member
Join Date
Feb 2009
Posts
182
Rep Power
12
I used a calculator and entered 2 for x1 and 4 for y1, difference is -2 squared which is 4.. then for x2 is 3 and y2 is 6, so difference is -3 squared which is 6 EDIT: NOPE that would be 9. LOL. so 4 plus 9=13. square root of 13 is 3.6. Now I will try this with the program and see if it matches

Tried it with the app, doesn't work. The app gave me a solution of 1.55

edit: I did graph it out. It makes a diagonal line going up to the top right.
Last edited by silverglade; 05-14-2011 at 06:40 PM.

14. Senior Member
Join Date
Feb 2009
Posts
182
Rep Power
12
ya if you look up you would see that I told you it was cross posted

Ill type it again. (and you put the cross posted message on java ranch and this forum as well.)

This post is now cross posted in case no one returns.

Calculating the distance between two points problem (Beginning Java forum at JavaRanch)
Last edited by silverglade; 05-14-2011 at 06:38 PM.

15. Senior Member
Join Date
Feb 2009
Posts
182
Rep Power
12
ok sorry. Here is my current code.

it compiles

Java Code:
```

import java.util.Scanner;

//distance = square root of [(x2-x1)squared + (y2-y1)squared]
public class PointDistance
{

public static void main(String[] args)
{

double distance(double x1, double y1, double x2, double y2)
{
return Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
}
double x2, x1, y2, y1;
double distance;

Scanner scan = new Scanner (System.in);

System.out.println("Enter the x coordinate for point 1: ");
x1 = scan.nextDouble();

System.out.println("Enter the y coordinate for point 1: ");
y1 = scan.nextDouble();

System.out.println("Enter the x coordinate for point 2: ");
x2 = scan.nextDouble();

System.out.println("Enter the y coordinate for point 2: ");
y2 = scan.nextDouble();

distance = distance(x1,y1,x2,y2);

System.out.println("The distance between the two points is " + distance + " .");

}

}```

here is my work by hand.

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

ok . so lets say

x1=2
y1=3
x2=4
y2=4

so 4-2*4-2=4
and 4-3*4-3=1
4+1=5
square root of 5= 2.23

distance is 2.23
now I will try it on my application.

shows distance as 1.55
Last edited by silverglade; 05-14-2011 at 08:05 PM. Reason: i am a monkey

16. Senior Member
Join Date
Feb 2009
Posts
182
Rep Power
12
Ok 4 or 5 hours later, I got help from Mike and Greg at javaranch. Those guys are awesome!! thank you!! Ok HERE is the final working code to calculate the distance between two points!!

Java Code:
```import java.util.Scanner;

//distance = square root of [(x2-x1)squared + (y2-y1)squared]
public class PointDistance
{
static double distance(double x1, double y1, double x2, double y2)
{
return Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
}
public static void main(String[] args)
{

double x2, x1, y2, y1;
double distance;

Scanner scan = new Scanner (System.in);

System.out.println("Enter the x coordinate for point 1: ");
x1 = scan.nextDouble();

System.out.println("Enter the y coordinate for point 1: ");
y1 = scan.nextDouble();

System.out.println("Enter the x coordinate for point 2: ");
x2 = scan.nextDouble();

System.out.println("Enter the y coordinate for point 2: ");
y2 = scan.nextDouble();

distance = distance(x1,y1,x2,y2);

System.out.println("The distance between the two points is " + distance + " .");

}

}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•