Results 1 to 17 of 17
  1. #1
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default 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. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

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

  3. #3
    radgator is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    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);

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by radgator View Post
    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. #5
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    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. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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. #7
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    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.

  8. #8
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    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. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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. #11
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    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. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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

  13. #13
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    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. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

  15. #15
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    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.

  16. #16
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    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

  17. #17
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    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 + " .");
    		
    	}
    	
    }

Similar Threads

  1. Calculating points in a circle
    By Masochist in forum New To Java
    Replies: 5
    Last Post: 02-14-2011, 06:36 PM
  2. calculating the angle between 3 points
    By imorio in forum New To Java
    Replies: 2
    Last Post: 12-24-2010, 08:49 AM
  3. Having problem in calculating leap year
    By lclclc in forum New To Java
    Replies: 3
    Last Post: 09-25-2009, 08:50 PM
  4. PROBLEM - calculating with array elements
    By ella in forum New To Java
    Replies: 13
    Last Post: 12-04-2008, 12:36 AM
  5. Replies: 2
    Last Post: 08-24-2008, 12:30 AM

Posting Permissions

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