Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Program: calculates distance between 2 points on 2d plane

Greenhorn
Posts: 4
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
no one can help? :'(

author
Ranch Hand
Posts: 154
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.

Ranch Hand
Posts: 3061
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
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