Seth Pink

Greenhorn

Posts: 4

posted 13 years ago

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?

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?

David Peterson

author

Ranch Hand

Ranch Hand

Posts: 154

posted 13 years ago

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.

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

posted 13 years ago

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

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

It is sorta covered in the JavaRanch Style Guide. |