Results 1 to 17 of 17
 05142011, 03:17 PM #1Senior Member
 Join Date
 Feb 2009
 Posts
 182
 Rep Power
 9
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 [(x2x1)squared + (y2y1)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 + " ."); } }
 05142011, 03:21 PM #2
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 11
Math.pow is exponentiation of two arguments, not squaring it. Also, scanner has a nextDouble method I believe.
 05142011, 03:24 PM #3Member
 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);
 05142011, 03:27 PM #4
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 11
 05142011, 03:29 PM #5Senior Member
 Join Date
 Feb 2009
 Posts
 182
 Rep Power
 9
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 + " ."); } }
 05142011, 03:51 PM #6
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 11
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; 05142011 at 03:55 PM.
 05142011, 04:38 PM #7Senior Member
 Join Date
 Feb 2009
 Posts
 182
 Rep Power
 9
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);
instead of
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.
 05142011, 04:49 PM #8Senior Member
 Join Date
 Feb 2009
 Posts
 182
 Rep Power
 9
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 [(x2x1)squared + (y2y1)squared] public class PointDistance { public static void main(String[] args) { double distance(double x1, double y1, double x2, double y2) { return math.sqrt((x1x2)(x1x2) + (y1y2)(y1y2)); } 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 + " ."); } }
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. DerekLast edited by silverglade; 05142011 at 04:52 PM.

you don't know (xa) * (xa) + (yb)*(yb)?
edit: didn't see your post just above. Does this compile?
Java Code:return math.sqrt((x1x2)(x1x2) + (y1y2)(y1y2));
 05142011, 05:02 PM #10
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 11
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)
 05142011, 05:02 PM #11Senior Member
 Join Date
 Feb 2009
 Posts
 182
 Rep Power
 9
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; 05142011 at 05:53 PM.
 05142011, 05:06 PM #12
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 11
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) add the squares find the square root of that sum
 05142011, 05:54 PM #13Senior Member
 Join Date
 Feb 2009
 Posts
 182
 Rep Power
 9
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; 05142011 at 06:40 PM.
 05142011, 06:29 PM #14
 05142011, 06:31 PM #15Senior Member
 Join Date
 Feb 2009
 Posts
 182
 Rep Power
 9
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; 05142011 at 06:38 PM.
 05142011, 06:53 PM #16Senior Member
 Join Date
 Feb 2009
 Posts
 182
 Rep Power
 9
ok sorry. Here is my current code.
it compiles
Java Code:import java.util.Scanner; //distance = square root of [(x2x1)squared + (y2y1)squared] public class PointDistance { public static void main(String[] args) { double distance(double x1, double y1, double x2, double y2) { return Math.sqrt((x2x1)*(x2x1) + (y2y1)*(y2y1)); } 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 (x2x1)*(x2x1) + (y2y1)*(y2y1))
ok . so lets say
x1=2
y1=3
x2=4
y2=4
so 42*42=4
and 43*43=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.55Last edited by silverglade; 05142011 at 08:05 PM. Reason: i am a monkey
 05142011, 08:23 PM #17Senior Member
 Join Date
 Feb 2009
 Posts
 182
 Rep Power
 9
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 [(x2x1)squared + (y2y1)squared] public class PointDistance { static double distance(double x1, double y1, double x2, double y2) { return Math.sqrt((x2x1)*(x2x1) + (y2y1)*(y2y1)); } 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 + " ."); } }
Similar Threads

Calculating points in a circle
By Masochist in forum New To JavaReplies: 5Last Post: 02142011, 07:36 PM 
calculating the angle between 3 points
By imorio in forum New To JavaReplies: 2Last Post: 12242010, 09:49 AM 
Having problem in calculating leap year
By lclclc in forum New To JavaReplies: 3Last Post: 09252009, 08:50 PM 
PROBLEM  calculating with array elements
By ella in forum New To JavaReplies: 13Last Post: 12042008, 01:36 AM 
given number of points(cordinates) , find max points lie on the same line ?
By Hayzam in forum New To JavaReplies: 2Last Post: 08242008, 12:30 AM
Bookmarks