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

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.

Always learning Java, currently using Eclipse on Fedora.

Linux user#: 501795

- 1

Darryl Burke wrote:derek, please BeForthrightWhenCrossPostingToOtherSites

http://www.java-forums.org/new-java/44060-calculating-distance-between-two-points-problem.html#post208186

Um, he was. He linked there in his original post, before any editing.

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.*

- 1

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.

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.

- X 2

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

- 1

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

Always learning Java, currently using Eclipse on Fedora.

Linux user#: 501795

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 .

Now HERE is the working code!!!

- 1

Always learning Java, currently using Eclipse on Fedora.

Linux user#: 501795

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |