## Trigonometry

## Trigonometry

(OP)

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?

#### Recommended for you

## RE: Trigonometry

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.

## RE: Trigonometry

## RE: Trigonometry

Trig is easy, once you understand it...

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

Have Fun, Be Young... Code BASIC

-Josh

http://cubee.topcities.com

## RE: Trigonometry

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.

## RE: Trigonometry

You have it backwards...

Use them like this...

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

http://cubee.topcities.com

## RE: Trigonometry

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

http://cubee.topcities.com

## RE: Trigonometry

## RE: Trigonometry

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) * LengthY = 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 = 3BaseY = 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...

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...

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 SHave Fun, Be Young... Code BASIC

-Josh

http://cubee.topcities.com

## RE: Trigonometry

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

## RE: Trigonometry

## RE: Trigonometry

Where...

Line

Ais the Height (Y or Rise)Line

Bis the Length (X or Run)Line

Cis the Hypotenuse (Radius)Angle

ais Opposite LineAAngle

bis Opposite LineBAngle

cis Opposite LineC(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)) / 2Hyperbolic Cosine:HCos(X) = (Exp(X) + Exp(-X)) / 2Hyperbolic 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)) / 2Inverse 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)) / 2Logarithm to base N:LogN(X) = Log(X) / Log(N)Have Fun, Be Young... Code BASIC

-Josh

http://cubee.topcities.com

## RE: Trigonometry

## RE: Trigonometry

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

http://cubee.topcities.com

## RE: Trigonometry

This is a VERY good site for trig help...

http://www.ping.be/~ping1339/gonio.htm

Have Fun, Be Young... Code BASIC

-Josh

http://cubee.topcities.com

## RE: Trigonometry