By Dan Gookin

Trigonometric functions in C — and, indeed, in all programming languages — use radians, not degrees. What’s a radian? Glad you asked. A radian is a measurement of a circle, or, specifically, an arc. It uses the value ð (pi) instead of degrees, where ð is a handy circle measurement.

So instead of a circle having 360 degrees, it has 2ð radians. That works out to 6.2831 (which is 2 × 3.1415) radians in a circle.

image0.jpg

For your trigonometric woes, one radian equals 57.2957795 degrees, and one degree equals 0.01745329 radians. So when you do your trig math, you need to translate between human degrees and C language radians.

CONVERT DEGREES TO RADIANS

#include <stdio.h>
int main()
{
 float degrees,radians;
 printf("Enter an angle in degrees: ");
 scanf("%f",&degrees);
 radians = 0.0174532925*degrees;
 printf("%.2f degrees is %.2f radians.n",
degrees,radians); return(0); }

Exercise 1: Type the source code from Convert Degrees to Radians into your editor. Line 10 is split so that it’s more readable on this page. You don’t need to split that line when you type it. Build and run. Test with the value 180, which should be equal to ð radians (3.14).

Exercise 2: Write a program that converts from radians to degrees.

Though C has many trigonometric functions, the three basic ones are sin(), cos(), and tan(), which calculate the sine, cosine, and tangent of an angle, respectively. Remember that those angles are measured in radians, not degrees.

Oh, and remember that you need the math.h header file to make the compiler happy about using the trig functions.

HAVING FUN WITH TRIGONOMETRY

#include <stdio.h>
#include <math.h>
#define PI 3.14159
#define WAVELENGTH 70
#define PERIOD .1
int main()
{
 float graph,s,x;
 for(graph=0;graph<PI;graph+=PERIOD)
 {
 s = sin(graph);
 for(x=0;x<s*WAVELENGTH;x++)
 putchar('*');
 putchar('n');
 }
 return(0);
}

Exercise 3: Type the source code from Having Fun with Trigonometry into your editor. Before you build and run, try to guess what the output could be.

Exercise 4: Modify the code from Having Fun with Trigonometry so that a cosine wave is displayed. Don’t get lazy on me! A cosine wave looks best when you cycle from 0 to 2ð. Modify your code so that you get a good, albeit character-based, representation of the curve.

No, Exercise 4 isn’t easy. You need to compensate for the negative cosine values when drawing the graph.

  • One radian equals 57.2957795 degrees.

  • One degree equals 0.0174532925 radians.