Confession: I don't understand Trigonometry

Basic and Machine Language

Moderator: Moderators

User avatar
Jeff-20
Denial Founder
Posts: 5759
Joined: Wed Dec 31, 1969 6:00 pm

Confession: I don't understand Trigonometry

Post by Jeff-20 »

I am a product of the desperately under-funded Chicago Public School system. We spent most of our school days dodging fights. As a result, I now find myself under-educated in Math.

I really wish I had the opportunity to study Trig, but by the time I made to a real school (college), I was already into art. I focused on my major area of study. Most of what I know about math is from programming the VIC. I am personally embarrassed to admit this. That knowledge is very limited.

Here are areas of VIC programming I have no knowledge of/access to:

SIN
TAN
COS

Trigonometric functions never were clear to me even from experimenting in programs. Even reading the definitions online does not help me apply the concepts to programming.

Let's say I wanted to make a ball move in a complete circle path around the screen. I'm not really sure, but I suspect I will need Trig to get the job done. No?
User avatar
Mike
Herr VC
Posts: 4831
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Confession: I don't understand Trigonometry

Post by Mike »

Jeff-20 wrote:I'm not really sure, but I suspect I will need Trig to get the job done. No?
Yes. Probably the easiest way:

Code: Select all

10 POKE 36879,8:PRINT "{CLR}"
11 FOR D=0 TO 359 STEP 5  :REM angle in degrees (0 .. 360)
12 R=2*{PI}*(D/360)       :REM convert angle to radians (0 .. 2*pi)
13 X=INT(COS(R)*10+0.5)
14 Y=INT(SIN(R)*10+0.5)
15 A=7680+22*(11-Y)+(11+X):REM rotate counter-clock wise around the center
16 POKE A,81
17 FORT=1TO100:NEXT
18 POKE A,32
19 NEXT
20 GOTO 11
You can find another example here.

Greetings,

Michael
viznut
Robotic Liberation
Posts: 18
Joined: Fri Dec 08, 2006 7:08 am

Post by viznut »

I remember realizing how SIN and COS work several years before they came up at school. However, I didn't realize that PI equals 180 degrees, but I did make some rotating triangles and stuff :)
User avatar
Mayhem
High Bidder
Posts: 3027
Joined: Mon May 24, 2004 7:03 am
Website: http://www.mayhem64.co.uk
Location: London

Post by Mayhem »

Pi, radians, that much I remember from school too heh... along with the trig functions and several "perfect triangle" threesomes (where the three sides of a right angled triangle are exact integers).
Lie with passion and be forever damned...
User avatar
ral-clan
plays wooden flutes
Posts: 3702
Joined: Thu Jan 26, 2006 2:01 pm
Location: Canada

Post by ral-clan »

What's "trigonometry"? :wink:





Actually, I took it in high school, but have never used it since so I remember NOTHING about it. Would have to read a refresher if I ever used them --- hence these VIC commands are a mystery to me too.
Last edited by ral-clan on Fri Mar 02, 2007 10:33 am, edited 1 time in total.
User avatar
Jeff-20
Denial Founder
Posts: 5759
Joined: Wed Dec 31, 1969 6:00 pm

Post by Jeff-20 »

In grade school, I would sit for hours trying different numbers to see if I could figure out how to use these commands...

PRINT COS(1)

PRINT COS(2)

PRINT COS(3)

I just couldn't put together any sort of pattern that seemed useful to game making. I am embarrassed to admit that Mike's wonderful circle program needs explication. I'm going to try to understand it tonight... Thanks Mike.

The wiki illustration seems to explain it all at first...

http://en.wikipedia.org/wiki/Sine

But drawing a circle or creating viznutesque* algorithms for rotating shapes is far from intuitive... at least for me.

-----------------------------------------
*yes, I guess I've just invented a new term: "Viznutesque" is an auctorial descriptive which is used to describe concepts, situations, and ideas which are reminiscent of the programming work of Viznut. :lol:
Centallica
Pinballer
Posts: 1090
Joined: Wed Feb 02, 2005 11:26 am

Post by Centallica »

This is how us pinheads understand ""trigonometry"

http://www.webwidevideo.com/showproject ... ject=P1072

Or maybe I don't understand it and it ain't trigonometry :?

Brian
PaulQ
undead vic
Posts: 1967
Joined: Sun Jan 14, 2007 2:57 pm

Post by PaulQ »

Sin, cos, and tan are short form for Sine, Cosine, and Tangent. These are trigonometric ratios of a similar triangle. We usually use a right angle triangle, which is a similar triangle, when calculating where something is supposed to be. The triangle doesn't really need to exist; It's a tool we use as we would use a ruler to measure something. Since the triangle doesn't really exist, we usually know only two measurements. That's where Sine, Cosine, and Tangent step in to help. Depending on what part of the triangle we're trying to figure out, we would use either Sin, Cos, or Tan.

In this program, we know that there are 360 degrees of angle in a full circle, so we go through all the angles, but in steps of five since the Vic's character display isn't fine enough to resolve each individual angle. Then the COS and SIN functions (Cosine and Sine) are used to calculate the X and Y location of the dot on the screen based on the current angle. These are then used to calculate the screen map area, where the dot is poked to.

I hope this helps.
PaulQ
undead vic
Posts: 1967
Joined: Sun Jan 14, 2007 2:57 pm

Post by PaulQ »

Okay, I thought of something that might make it simpler to understand.

Imagine the face of an analog clock. You have the numbers 1 to 12 around the outside. Now, suppose you want to do this on the Vic 20. Here's how it would be done:

First, you take the 360 degrees of a circle, and divide it by 12. That gives us 30; so our FOR-NEXT loop will be in steps of 30. Now, think about where these angles would exist; imagine if you drew a line between the 12, the center of the clock, and the one. There is your first triangle!

Now, it's important to know that neither the circle nor the triangle actually exist on the Vic's screen; instead, the screen is made up of a grid. We usually think of a grid as an X-Y axis. So far, all we've done is designed the program to calculate 12 angles from the center of our imaginary circle. We need COS and SIN to figure out where these numbers are to appear in our grid array, so we convert the degree into a radian to make it easier to work with. Note that radians represent units of degree of angle.

Once we have the radians, we can use SIN and COS to calculate where, from the center of the imaginary circle, the numbers are to land on the X-Y grid of our screen. Once we have this, we can then POKE our numbers to that location in the screen.
User avatar
Jeff-20
Denial Founder
Posts: 5759
Joined: Wed Dec 31, 1969 6:00 pm

Post by Jeff-20 »

I think the definitions and concepts are clear. Even as I kid, I knew what the tokens represented; I may have been exaggerating when I said "no knowledge"... I was trying to say the the wiki illustration makes the definitions clear, but applying them to the VIC is difficult.

To be more clear, line 12, 13, and 14 in Mike's program do not come naturally to me. These are the lines that need explication. Every other line in the program makes perfect sense to me! It's your very last paragraph in the previous post, DQ. That's the part where I get lost. So close, yet so far... :?
MacbthPSW
Vic 20 Afficionado
Posts: 478
Joined: Wed Apr 06, 2005 1:56 pm

Post by MacbthPSW »

Jeff-20 wrote:To be more clear, line 12, 13, and 14 in Mike's program do not come naturally to me. These are the lines that need explication. Every other line in the program makes perfect sense to me! It's your very last paragraph in the previous post, DQ. That's the part where I get lost. So close, yet so far... :?
I might as well give it a try :)

Code: Select all

12 R=2*{PI}*(D/360)       :REM convert angle to radians (0 .. 2*pi)
Radians totally confused me as a kid trying to program my C-64. I was familiar with the 360 degrees in a circle, and I wanted to be able to describe the heading of a car viewed from overhead this way, instead of having a car that unrealistically just moved up/down/left/right.

As mentioned already, 360 degrees is equal to 2*pi. A couple more examples: 180 degrees is equal to pi. 90 degrees is .5*pi. So line 12 is just changing from degrees to radians, to allow C= BASIC's SIN and COS functions have something to work with.

Code: Select all

13 X=INT(COS(R)*10+0.5)
14 Y=INT(SIN(R)*10+0.5)
You can think of COS() and SIN() as functions that extract or report the X or Y (respectively) components. They always return a value from -1 to +1, so they need to be scaled. With the VIC-20 screen being 22 columns wide, multiplying by 10 as Mike has chosen to do makes good sense (giving a range of -10 to +10). If you were plotting to a hires screen, such as the C-64's 320x200, you might scale by 100 so it will fit.
User avatar
Jeff-20
Denial Founder
Posts: 5759
Joined: Wed Dec 31, 1969 6:00 pm

Post by Jeff-20 »

:idea: Ah... Ok. I understand 0.5*pi is 90 degrees, but why "+0.5" after multiplying the sine/cosine by 10?
MacbthPSW
Vic 20 Afficionado
Posts: 478
Joined: Wed Apr 06, 2005 1:56 pm

Post by MacbthPSW »

Jeff-20 wrote::idea: Ah... Ok. I understand 0.5*pi is 90 degrees, but why "+0.5" after multiplying the sine/cosine by 10?
That's just a common way of turning the INT() function into a ROUND() function. There's a similar example on pages 129-130 of the VIC-20 guide. (I keep wanting to call it the User's Guide but that's what they called it on the C-64).
User avatar
pitcalco
just pitcalco
Posts: 1272
Joined: Wed Dec 28, 2005 4:13 pm
Location: Berlin

Trigonometric Functions

Post by pitcalco »

Trigonometry is a beautiful mathematical discipline. Once you have mastered it, you will wonder what you have even done without it.

Don't blame your public school systems...it is up to you to learn things on your own.

Anyway, more to the point:

VIC is only preset with SIN, COS, TAN and I think ATN. Other trig functions must be derived from them. However, in the VIC manual which comes with the computer, the list of derived functions has some serious errors in it which result in division by zero errors and taking square roots of negative numbers.

Has anyone been able to rewrite these so that they work? You could use transforms but they would be so long and use up so much memory.
There are only three kinds of people in the world: those who can count and those who can't.

Paul Lambert
Berlin
Federal Republic of Germany
carlsson
Class of '6502
Posts: 5516
Joined: Wed Mar 10, 2004 1:41 am

Post by carlsson »

I checked the manuals from VIC-20 (1981), breadbox C64 (1983) and C64C (1985). By 1985, they had changed the page heading, but all formulas are identical

Secant: SEC(X) = 1/COS(X)
Cosecant: CSC(X) = 1/SIN(X)
Cotangent: COT(X) = 1/TAN(X)
Inverse sine: ARCSIN(X) = ATN(X/SQR(-X*X+1))
Inverse cosine: ARCCOS(X) = -ATN(X/SQR(-X*X+1)) + pi/2
Inverse secant: ARCSEC(X) = ATN(X/SQR(X*X-1))
Inverse cosecant: ARCCSC(X) = ATN(X/SQR(X*X-1)) + (SGN(X)-1)*pi/2
Inverse cotangent: ARCCOT(X) = ATN(X) + pi/2
Hyperbolic sine: SINH(X) = (EXP(X) - EXP(-X))/2
Hyperbolic cosine: COSH(X) = (EXP(X) + EXP(-X))/2
Hyperbolic tangent: TANH(X) = EXP(-X)/(EXP(X) + EXP(-X))*2+1 (!)
Hyperbolic secant: SECH(X) = 2/(EXP(X) + EXP(-X))
Hyperbolic cosecant: CSCH(X) = 2/(EXP(X) - EXP(-X))
Hyperbolic cotangent: COTH(X) = EXP(-X)/(EXP(X)-EXP(-X))*2+1 (!)
Inverse hyperbolic sine: ARCSINH(X) = LOG(X + SQR(X*X+1)
Inverse hyperbolic cosine: ARCCOSH(X) = LOG(X +SQR(X*X-1)
Inverse hyperbolic tangent: ARCTANH(X) = LOG((1+X)/(1-X))/2
Inverse hyperbolic secant: ARCSECH(X) = LOG((SQR(-X*X+1)+1)/X)
Inverse hyperbolic cosecant: ARCCSCH(X) = LOG(SGN(X)*SQR(X*X+1)/X)
Inverse hyperbolic cotangent: ARCCOT(X) = LOG((X+1)/(X-1))/2

When I typed in these formulas, I noticed several have unbalanced parenthesis, which I tried to fix above. Those marked with an exclamation mark am I uncertain how they should be balanced, as two similar formulas exists with different parenthesis.

Mainly, I think it is in the case of X=0 that several of these formulas will fail. I don't know which values each function should yield at 0. Since SIN(0) and TAN(0) both are zero, I suppose CSC(0) and COT(0) are undefined (or infinity).
Anders Carlsson

Image Image Image Image Image
Post Reply