aspose file tools*
The moose likes Beginning Java and the fly likes Calculating the distance between two points problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calculating the distance between two points problem" Watch "Calculating the distance between two points problem" New topic
Author

Calculating the distance between two points problem

derek smythe
Ranch Hand

Joined: Apr 18, 2011
Posts: 63
Hi, my calculations on paper to find the distance between 2 lines is not matching up with what my app is giving me. please any help greatly appreciated.


the formula for Distance is :

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

The following code compiles and runs, but the output seems to be wrong.



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 also posted this question here, but I might not get an answer anymore.

here
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
So your test points are:

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

And the pencil and paper solution would be

sqrt( (3 - 2)^2 + (6 - 4)^2 )

Try that, and then check to see if that's what you've coded.


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
derek smythe wrote:so difference is -3 squared which is 6.


You are sure about that?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
derek smythe wrote:
The following code compiles and runs

Really? What language are you using?
derek smythe
Ranch Hand

Joined: Apr 18, 2011
Posts: 63
wtf db. This is the second time you have noticed that. and the second time you have given me the forthright message. Please read my entire post before doing that again. It makes me look like an ass.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9

Um, he was. He linked there in his original post, before any editing.
derek smythe
Ranch Hand

Joined: Apr 18, 2011
Posts: 63
oops. LOL. ya 3 squared is 9 not 6. So that would be 9+4 which is 13, squared which is 3.6. My app gives me 1.5. any more help greatly appreciated. thank you .Derek
derek smythe
Ranch Hand

Joined: Apr 18, 2011
Posts: 63
Greg Brannon wrote:So your test points are:

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

And the pencil and paper solution would be

sqrt( (3 - 2)^2 + (6 - 4)^2 )

Try that, and then check to see if that's what you've coded.



Ok thank you. I will look at my code again. Thank you. Derek
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4529
    
    5

derek smythe wrote:wtf db. This is the second time you have noticed that. and the second time you have given me the forthright message. Please read my entire post before doing that again. It makes me look like an ass.


I see I missed your edit, which was probably done during the time I had the page open. Sorry about that.


luck, db
There are no new questions, but there may be new answers.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
derek smythe wrote:I used a calculator and entered 2 for x1 and 4 for y1, difference is -2 squared which is 4..

Why are you subtracting x1 - y1? What does that have to do with anything?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
Darryl Burke wrote:I see I missed your edit, which was probably done during the time I had the page open. Sorry about that.

No, I'm sure the link was there in the original; I had a copy loaded in another tab and it didn't have the "edited by", but did have the link, with the same text.
derek smythe
Ranch Hand

Joined: Apr 18, 2011
Posts: 63
Ok thank you. Sorry about my messed up math. ok NEVER depend on someone's code off the net!!! That is what I did and it was WRONG!@!!

Here is the correct method to calculate a distance between 2 points. I still have to check it out again. thank you! derek.




ok EDIT: I feel like a monkey trying to do math. here is what I tried to do, remember though, I am a monkey.


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


OK.

lets say

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

2-0=2 *2=4
4-3=1*1=1

4+1=5

square root of 5 is 2.2


my app gives me 1.55 for this.

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
Maybe you should post your current code. Make sure it really compiles, and copy and paste the actual code. What you've posted so far does not compile, so it couldn't be giving you any results at all.
derek smythe
Ranch Hand

Joined: Apr 18, 2011
Posts: 63
ok sorry. Here is my current code.


it compiles




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

Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
You say it compiles - and maybe it does - but there should be errors:

1. You've defined your method distance() inside main(), then
2. Your method distance() must be static to be called by your static main() method
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
derek smythe wrote:
it compiles

Not in Java it doesn't.
derek smythe
Ranch Hand

Joined: Apr 18, 2011
Posts: 63
ok thank you. How is this now? please.


derek smythe
Ranch Hand

Joined: Apr 18, 2011
Posts: 63
the code above does compile in java. Here is a paste of the app working in the command window.


C:\JAVA_PROGRAMMING_CODE\code1\PointDistance>java PointDistance
Enter the x coordinate for point 1:
2
Enter the y coordinate for point 1:
3
Enter the x coordinate for point 2:
4
Enter the y coordinate for point 2:
4
The distance between the two points is 1.5537739740300374 .
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
It sounds like at some point in the past, you successfully compiled a class called PointDistance. But the code you're showing us now does not compile - you're probably re-running the old code.

What happens right now if you type

javac PointDistance.java

?
derek smythe
Ranch Hand

Joined: Apr 18, 2011
Posts: 63
omg sorry! I was saving it over an old text file. It does NOT compile sorry. I get like 8 errors as well. I will check them out one sec please. thank you!
derek smythe
Ranch Hand

Joined: Apr 18, 2011
Posts: 63
HAHA!!! Mike and Greg you guys are Awesome!!! It works!! hahahahahaha. I love you!!! I have been at this for like 4 or 5 hours. LOL!! Thank you. Now I can relax. man I learned a lot from this project. And that I need to focus on the basics more, and improve my math ability, and not depend on code from the internet that is messed up. Well can get ideas from the net, but to copy and paste their code without analyzing it first is deadly. Thank you so much you guys!!

Now HERE is the working code!!!




Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
Glad you made it through the problems. Yes, I believe you learn more when you start from scratch, and you don't have to fix someone else's problems or correct their code to fit your needs, especially on these basic problems. However, it can be instructive to see how someone else attacks the same or a similar problem, but then write your own solution.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
Yes, good to hear you've fixed the problem, congratulations.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
There is a method in the Math class which works out those distances. It's called something like hypotenuse, or hypotenuse abbreviated. I think you have to pass x1 - x2 and y1 - y2 as arguments.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calculating the distance between two points problem