![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
Pythagorean Theorem c² = a² + b²
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. |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() ![]() ![]() ![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| This site is hosted for free by FreeWebz.com. Click here to get your own free website. |