Tutorial: Rotation in QB

After reading this tutorial, try downloading rotation.zip for a demonstration and explanation in QB code.

Rotation is a fairly concept. You can probably think of several uses for it. For one, it allows lines to be moved without changing length. For example, imagine a turret or barrel of a gun that needs to be turned. Gauges with needles that rotate can also be programmed in this way. All you really need is a basic understanding of Trigonometry.

We'll start with a basic review of trig:

Consider this right triangle. Angle can be represented as a relation of the sides of the right triangle. You need to know the following:

SIN = Opposite / Hypotenuse
COS = Adjacent / Hypotenuse
TAN = Opposite / Adjacent

That's it! If you've never seen trig before, don't be overwhelmed. This is really not complicated; it's just a matter of getting used to calling Op / Adj SIN.

Thus, SIN gives us a number between 0 and 1 which tells us how large the opposite side is compared with the hypotenuse. Using simple algebra:

Hypotenuse * SIN = Opposite side

Thus, you can find the length of the opposite side if you know the angle and the hypotenuse. A similar method is used to find the adjacent side (Hypotenuse * COS = Adjacent). Now let's try something different: Think of the Hypotenuse as the radius of a circle, with radius being equal to the hypotenuse:

Thus, if we wanted a line to be rotated about the point (x,y), we might think of the line as a hypotenuse of a right triangle and drop sides to complete the triangle:

We can now determine the angle the line makes with an imaginary horizontal line, increase it, and redraw the line. The new line will be the same length as the old line, but it will face a different direction.

Each of the trig functions (SIN, COS, TAN) have inverse trig functions. This simply means "go backwards". To use SIN, for example, you must pass an angle to the function and you get a ratio of opposite / hypotenuse. To use Inverse SIN you pass the ratio and you get the angle. If we want to find the angle the line makes, all we need is the length of a couple sides and an inverse function: ARCTAN (inverse trig functions are often called Arcsin, Arccos, Arctan). Why use Arctan? It's the only inverse trig function QB supplies you with. With a little ingenuity, you can get the others from ARCTAN, but we won't go into that right now. Here's what you do:

Suppose we have a line which goes from (x1, y1) - (x2, y2)

The length of the imaginary "adjacent side" is (x2 - x1).
Likewise, the length of the opposite side is (y2 - y1).

Thus, we can now find the ratio of opposite / adjacent, which is, of course, TAN. Therefore,

Arctan((x2-x1)/(y2-y1)) = Angle.

This is important. Now you have the angle, but QB gives it to you in radians. There are 2 * pi radians in a circle, just like there are 360 degrees in a circle. It's just a different system of measurement, like Celsius and Kelvin. Now...the important lesson to be learned: To convert from radians to degrees:

Degrees = Radians * 180 / pi

Because of the symmetry of the ARCTAN function, if X < 0, that is X1 > X2, we need to add 180 degrees.  Now we have the angle this point makes with the point of rotation (X1, Y1).  The "radius" of our pretend circle can be found using the Pythagorean Therom --

H = SQR((X2 - X1) ^ 2 + (Y2 - Y1) ^ 2)

The reason I like to think of it as a radius is because this idea can be expanded to other applications, such as moving around a circle.  Anyway, once you have the degrees you can easily increment it: Degrees = Degrees + number.  Now all we have to do is turn the angle and point of rotation back into points:

X = COSINE(Degrees * PI / 180) * H

Y = SINE(Degrees * PI / 180) * H

Degrees must be converted back into radians, remember!  H can be substituted for a radius, R.  There you have it.  I hope that's not too confusing.  Check the program if you have questions, and if you still can't figure it out, give us an email at swsoft@hotmail.com.  

 

This page was generated by Microsoft FrontPage 98.
Copyright 2000 by Timothy D. Mowrer for Secret Weapon Software.