Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trigonometry 1

Status
Not open for further replies.

KenshinHimura

Programmer
May 8, 2003
91
US
I really need help on these qbasic functions. I learned a little about these in my math class, but I don't really see how it applies to qbasic. And since I'm not getting into trig this year at school I need help. Does anyone know any sites where I can find information about these functions?
:)
 
The BASIC trig functions are
COS - cosine in radians
SIN - sine in radians
TAN - tangent in radians
ATN - cotangent in radians

Have use ever heard of SOH-CAH-TOA?
sin x = opposite/hypotenuse
cos x = adjacent/hypotenuse
tan x = opposite/adjacent

Trig is used to do mathematical calculations with triangles and using angles and the lengths of the sides to find anything that you need.

Trig functions are usually used in qbasic for three-dimentional programming, but of course could be used for anything that you need.

 
if you EVER want to make a raycaster, isometric engine, wireframe engine, or whatever, you will need to know these. but you know what? my advice is not to worry abou them until you take it up in school. `
 
Nah...

Trig is easy, once you understand it...

Take a look at the visual description I made a while back...

mysincos.JPG


Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif

 
I'm not getting into 3d graphics yet. I understand what the functions are used for, but I don't understand how exactly. I'm mainly focusing on this for 2d rotations and other effects. I've experimented a lot, but I'm not sure if I'm doing anything right. I just hope some of the programs I make using the functions work out. Like this:

SCREEN 13
pi = 3.141592654#
RANDOMIZE TIMER
DO
LINE (160, 100)-(degrees, degrees2), 0
x = x + 1
y = y + 1
degrees! = COS(x) * 180 / pi + 160
degrees2! = SIN(y) * 180 / pi + 100
CIRCLE (degrees!, degrees2!), 7, INT(RND * 255)
LINE (160, 100)-(degrees, degrees2), INT(RND * 255)
WAIT &H3DA, 8
LOOP

All I did was find out how to convert radians to degrees and the rest I just experimented with. The things I need to know are what are the trig functions actually giving me? What are radians? And heck why that program up there works?
Thx for the visual CubeE101, but I don't get how that applies to qbasic when it finds the cos and sin of a number. I get how it works in "normal" math tho. Thx for all the info you've given me so far its been helpful.
 
no...

You have it backwards...

Use them like this...

Code:
Const Pi = 3.141592654#

Sub AngLine(X1, Y1, Degree, Radius, Color)
  X2 = X1 + (Cos(Degree * Pi / 180) * Radius)
  Y2 = Y1 + (Sin(Degree * Pi / 180) * Radius)
  Line (X1,Y1)-(X2,Y2), Color
End Sub

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif

 
Or... if you want to draw a circle...

Code:
Const Pi = 3.141592654#

Sub DrawCirc(X, Y, Radius, Color)
  For Deg = 1 To 360
    X1 = X + (Cos((Deg - 1) * Pi / 180) * Radius)
    Y1 = Y + (Sin((Deg - 1) * Pi / 180) * Radius)
    X2 = X + (Cos(Deg * Pi / 180) * Radius)
    Y2 = Y + (Sin(Deg * Pi / 180) * Radius)
    Line (X1,Y1)-(X2,Y2), Color
  Next
End Sub

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif

 
What he was showing you in the graphic above is what is known as the unit circle. The parent function always has its centre at (0,0) and has 1 for a radius. Then the position of B in the graphics (cos A, sin A) where A is the angle.

 
Yes...

This is also moving into Vectors...
A Vector is...
An element with a base point at X,Y(,Z)
A direction (Angle)
And A Magnitude (Length)

A "Normal Vector" is...
A Vector with the magnitude equal to 1

This Vector Can then be scaled, by multiplying...

So you use the Base Function...
X = Cos(Angle)
Y = Sin(Angle)


Which will give you the coordinates (X,Y) of a point relative to Zero (0,0) that represents a direction and a length of 1 (Which is also known as a normal vector)

You Can then Scale the vector by multiplying Both the X and the Y equations by the Same Number...
X = Cos(Angle) * Length
Y = Sin(Angle) * Length


You can also Translate the base coordinates by adding a value to the product of those equations...
X = BaseX + (Cos(Angle) * Length)
Y = BaseY + (Sin(Angle) * Length)


So if you Want to Draw A line starting at (3,5) and extending 10 units at 45 degrees...

BaseX = 3
BaseY = 5
Length = 10
Angle = 45 * Pi / 180 'MUST BE IN RADIANS TO WORK
X = BaseX + (Cos(Angle) * Length)
Y = BaseY + (Sin(Angle) * Length)

Line (BaseX, BaseY) - (X, Y)


It's that simple ;-)
----------------

There are also many other uses for Sin & Cos...
If you Plot Y as a funtion of X, such as...

Code:
Screen 13
For X = 1 to 320
  Y1 = 100 + (Sin((X-1) * Pi / 180) * 50)
  Y2 = 100 + (Sin(X * Pi / 180) * 50)
  Line (X, Y1) - (X, Y2)
Next

You Will notice that this creates a Wave...
This is known as a Sine Wave
Cosine Creates A Similar Effect, but is offset from the Sine Wave...

Code:
Screen 13
For X = 1 to 320
  Y1 = 100 + (Cos((X-1) * Pi / 180) * 50)
  Y2 = 100 + (Cos(X * Pi / 180) * 50)
  Line (X, Y1) - (X, Y2)
Next

You can do many things With these functions such as warping pictures, shifting colors, and many other stange effects, just by tweaking the above formulas and using your imagination...

For an example of what all you can do with Sin & Cos... Download LudaTRIS from my site... it is a wack tetris game I made a few years ago, that uses Sin & Cos functions For the Intro Thread effect, Color Shifting, and Warping the Screen... (I believe you hold 'W' to add to the 'Wave' Effect...

The source code is also there for you to download...

I Hope This Helps...
And Have Fun ;-),
Josh S

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif

 
If you are unfamiliar with the sine wave you can see it by doing this

SCREEN 12
FOR a% = 0 TO 360
ang = a% * 3.14159 / 180
PSET (a%,ang * 10 + 100)
NEXT

The cosine will give you that same thing but offest by exactly -pi/2

If you know about calculus you will also notice that the cosine and sine graphs are derivatives of each other, but since you haven't taken trig yet, you probably wouldn't know that. Thats not important to know for trig programming, its just a little FYI
 
You actually made me understand the basic forms of trig....man, you people are geniuses. I pretty much understand it now. Great now I need to get into the inverse and hyperbolic and complex stuff like that....but I'll live. Thx so much you've really helped me out.
 
Ok... So you have your basic Right Triangle...
mytrig.jpg


Where...
Line A is the Height (Y or Rise)
Line B is the Length (X or Run)
Line C is the Hypotenuse (Radius)

Angle a is Opposite Line A
Angle b is Opposite Line B
Angle c is Opposite Line C (and is always 90 degrees)

For the functions to work, the Triangle must be Normalized...
So, Assume the Hypotenuse (C) is 1 unit in length
And ALL angles are in Radians (Degree * Pi / 180)

----Sine and Cosine-----
Sin(a) = B / C
-so-
Sin(a) * C = B
-or-
B / Sin(a) = C

Cos(a) = A / C
-so-
Cos(a) * C = A
-or-
A / Cos(a) = C

----Tangent and Arch Tangent-----
*Note: A / B is also known as Rise over Run AKA slope
Tan(a) = A / B
-so-
Tan(a) * B = A
-or-
A / Tan(a) = B

ArcTan (ATN) is the opposite of Tan
Atn(A / B) = a

So they cancel each other out...
Tan(Atn(A / B)) = A / B
-or-
Atn(Tan(a)) = a

Other Functions can be derived from these base functions...

Secant: Sec(X) = 1 / Cos(X)
Cosecant: Cosec(X) = 1 / Sin(X)
Cotangent: Cotan(X) = 1 / Tan(X)
Inverse Sine: Arcsin(X) = Atn(X / Sqr(-X * X + 1))
Inverse Cosine: Arccos(X) = Atn(-X / Sqr(-X * X + 1)) + 2 * Atn(1)
Inverse Secant: Arcsec(X) = Atn(X / Sqr(X * X – 1)) + Sgn((X) – 1) * (2 * Atn(1))
Inverse Cosecant: Arccosec(X) = Atn(X / Sqr(X * X - 1)) + (Sgn(X) – 1) * (2 * Atn(1))
Inverse Cotangent: Arccotan(X) = Atn(X) + 2 * Atn(1)
Hyperbolic Sine: HSin(X) = (Exp(X) – Exp(-X)) / 2
Hyperbolic Cosine: HCos(X) = (Exp(X) + Exp(-X)) / 2
Hyperbolic Tangent: HTan(X) = (Exp(X) – Exp(-X)) / (Exp(X) + Exp(-X))
Hyperbolic Secant: HSec(X) = 2 / (Exp(X) + Exp(-X))
Hyperbolic Cosecant: HCosec(X) = 2 / (Exp(X) – Exp(-X))
Hyperbolic Cotangent: HCotan(X) = (Exp(X) + Exp(-X)) / (Exp(X) – Exp(-X))
Inverse Hyperbolic Sine: HArcsin(X) = Log(X + Sqr(X * X + 1))
Inverse Hyperbolic Cosine: HArccos(X) = Log(X + Sqr(X * X – 1))
Inverse Hyperbolic Tangent: HArctan(X) = Log((1 + X) / (1 – X)) / 2
Inverse Hyperbolic Secant: HArcsec(X) = Log((Sqr(-X * X + 1) + 1) / X)
Inverse Hyperbolic Cosecant: HArccosec(X) = Log((Sgn(X) * Sqr(X * X + 1) + 1) / X)
Inverse Hyperbolic Cotangent: HArccotan(X) = Log((X + 1) / (X – 1)) / 2
Logarithm to base N: LogN(X) = Log(X) / Log(N)

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif

 
thx cubee for explaining that so well. It seems really complicated. BTW what are these formulas used in programming for? thx again
 
The main ones that I use are Sin & Cos...

You can, however, use ArcTan (ATN) to get an angle from point to point, for example, if you use a mouse routine, and want to get the angle between 2 mouse clicks...

The rest of them might be useful if you are writing a CAD (or similar) program. otherwise, I have no idea...

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top