• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calculating the distance between two points problem

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

derek smythe wrote:so difference is -3 squared which is 6.



You are sure about that?
 
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

derek smythe wrote:
The following code compiles and runs


Really? What language are you using?
 
derek smythe
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Master Rancher
Posts: 4796
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Mike Simmons
Master Rancher
Posts: 4796
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Master Rancher
Posts: 4796
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Master Rancher
Posts: 4796
72
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 563
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Master Rancher
Posts: 4796
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

derek smythe wrote:
it compiles


Not in Java it doesn't.
 
derek smythe
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok thank you. How is this now? please.


 
derek smythe
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Master Rancher
Posts: 4796
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 563
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, good to hear you've fixed the problem, congratulations.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
That's my roommate. He's kinda weird, but he always pays his half of the rent. And he gave me this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic