Loren Fiore Software

Pythagorean Theorem
By Michael Albright

Intro
In my opinion, the Pythagorean Theorem is one of the most useful, and simple, formulas a high school geometry student might come across. But what about us programmers? How is it useful to us? The Pythagorean Theorem is useful to us because it allows us to calculate the distance between two points with ease. Imagine that you have two characters for a simple 2-D game displayed on a form. One character is in the top left corner of the form, the other character is at the bottom right corner of the form. How can we calculate their distance apart (diagonally across the form)? That's where the theorem comes in.

Explanation
The Pythagorean Theorem describes a nice little equation for working with right triangles. Lucky for us, right triangles are everywhere. For an equation, the theorem says:

c² = a² + b²

C represents the hypotenuse of the right triangle, a and b are legs of the right triangle. The hypotenuse is the longest side of the triangle, and the the legs are the two shorter sides.


A Right Triangle

So, the formula says that the length of the hypotenuse squared, equals the sum of the lengths of the legs squared.

Distance
Now, for a few details. Length is equivalent to distance. Distance is the absolute value of point1 minus point2. Back to our form example. Pretend that character1 is at 100 on the X axis (100 units from the left of the form) and character2 is at 230 on the X axis (230 units from the left of the form). Therefore, point1 = 100 and point2 = 230. The distance between them is the absolute value of 100 - 230 (in math : |100 - 230| ). The absolute value of 100 - 230 = 130. The horizontal distance between the characters is 130. We have just found the length of b. Now lets say the character1 is at 50 on the Y axis (50 units from the top of the form) and character2 is at 475 on the Y axis (475 units from the top of the form). The distance between them is the absolute value of 50 - 475, or 425. The length of side a is 425. Now we can finish our calculation.

Calculation
We know that side a = 425, and side b = 130. We can now finish. Since c² = a² + b², We can say that c² = (425)² + (130)² c² = 180,625 + 16,900 c² = 197,525 Now we know what c² equals, but we need to know what c equals. When something is squared, square root it. The square root of c² = c. Therefore, c = 444.43, or approximately 444. The distance (diagonally) between character1 and character2 is 444 units.

Programming Help
Now you know the math. I’ll try to help you with the programming. First, lets make a function to find the distance.
Public Function Distance(Point1 as Long, _
                         Point2 as Long)_
                         as Long
    Distance = Abs(Point1 - Point2)
End Function
This function calculates the distance between two points, using Visual Basic’s Abs(), or Absolute Value function. Next, let’s make a function to calculate the length of the hypotenuse (C).
Public Function Pythagorean(A as long, B as long)
    Dim C_Squared as long

    C_Squared = (A*A) + (B*B)
    Pythagorean = Sqr(C_Squared)
End Function
This function, Pythagorean, returns the length of c when you pass it the length of a and b. Notice that A*A is equivalent to a². Also, C is found by fetching the square root of c², using Visual Basic’s
Sqr(), or Square Root function. And for the Grand Finale, a demonstration with Form_Load().
Private Sub Form_Load()
    Dim a as long, b as long, c as long
	
    b = Distance(100, 230)   
    a = Distance(50, 475)     
	
    c = Pythagorean(a, b)
End sub

Conclusion
Well, there you have it folks. I hope you now know what the Pythagorean Theorem is, what it is used for, and how YOU can use it to make your programming life easier. Please feel free to email any comments, questions, or suggestions you might have to me at vbfisherman@hotmail.com. I’m Michael Albright, thanks for reading.
Loren Fiore Software
About
Articles
Projects
Projects - Research & Development
Tutorials
Loren Fiore Software
Blank Button
Loren Fiore Software
CopyrightNewsLoren Fiore SoftwareContactLoren Fiore Software

This site is hosted for free by FreeWebz.com. Click here to get your own free website.