• Post Reply Bookmark Topic Watch Topic
  • New Topic

Program: calculates distance between 2 points on 2d plane  RSS feed

 
Seth Pink
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys,
Im trying to write a method that accepts two random generated numbers between 0 and 100 and then calculates the distance of the line segment on a 2d plane. I know how to create a random number that isn't my problem, im just confusted about the pythagerus(spelling? therom bit. Here is what I have so far.

Am I on the right tack? Can somebody pleae help me with this?
 
Seth Pink
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no one can help? :'(
 
David Peterson
author
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, we don't like to do folks homework for them!
Pythagoras's theorem says that the length of the hypotenuse of a triangle is the square root of the sum of the squares of the other two sides.
So, to calculate the hypotenuse you have to work out the length of the other two sides...

The sides are marked with dots. For example:

Also, the sqrt and pow functions are not built into the language, but are in the java.lang.Math package.

Hope this gets you along the right track. Let us know how you get on.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In pseudo-mathematical notation, Pythagorean's Theorem can be written as:
a^2+b^2=c^2
where ^ stands for exponentiation (raising to a power).
Also, a, b, and c stand for the lengths of the sides of the triangle. So the first task, as David illustrated, is to visualize a right triangle defined by the two points and find the length of each side. From there you can use the above equation to solve for the length of the other side.
This will help you derive the formula yourself, however if you just want to look it up, any algebra book should have a section on the distance formula. This is the version of Pythagorean's Theorem that is useful for this situation.
HTH
Layne
 
Seth Pink
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys and girls... I didn't want anyone to do my "homework" for me.. just a little help which you guys gave me, Cheers
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!