C
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Arrays and Strings
-
Section 5: Pointers
-
Section 6: Memory Management
-
Section 7: Structures and Unions
-
Section 8: The Preprocessor and Build Process
-
Section 9: Standard Library: stdio.h
-
Section 10: Standard Library: stdlib.h
-
Section 11: Standard Library: string.h
-
Section 12: Standard Library: ctype.h and wctype.h
-
Section 13: Standard Library: math.h, complex.h, fenv.h, tgmath.h
-
Section 14: Standard Library: Type and Limit Headers
-
Section 15: Standard Library: Error Handling and Debugging
-
Section 16: Standard Library: Localization and Encoding
-
Section 17: Standard Library: time.h
-
Section 18: Standard Library: Concurrency (C11)
-
Section 19: POSIX and System Programming (unistd.h)
-
Section 20: More Data Structures
-
Section 21: Algorithms in C
-
Section 22: Bitwise Operations
-
Section 23: Command-Line Programs
-
Section 24: Debugging and Best Practices
-
Section 25: Compiler and Language Internals
-
Section 26: Embedded and Cross-Platform Considerations
-
Section 27: Networking Basics
-
Section 28: Practical Projects
-
Section 29: Interview Practice
-
Section 30: C23 Modern Features
-
Section 31: More Practice and Review
104: Trigonometric Functions
I've been messing around with some basic 2D graphics lately, and I wanted to make a point orbit around a center coordinate—you know, like a planet around a sun or a cursor moving in a circle. I figured I'd just throw some sin() and cos() calls at it and call it a day, but as is usually the case with C, the first attempt was a disaster.
Wait, where did my point go?
Here was my first attempt. I wanted the point to be at 90 degrees (straight up) from the center of the screen.
#include <stdio.h>
#include <math.h>
int main() {
double angle = 90.0;
double radius = 100.0;
double x = radius * cos(angle);
double y = radius * sin(angle);
printf("Coordinates: x=%.2f, y=%.2f\n", x, y);
return 0;
}
I expected the output to be x=0.00, y=100.00 because at 90 degrees, the cosine is 0 and the sine is 1. But when I ran this, I got something completely bizarre: x=-44.81, y=89.40. I stared at the screen for a minute, wondering if my math teacher had lied to me for three years of high school.
The Radians Trap
Then it hit me. I'm thinking in degrees, but the C standard library—and almost every other programming language—thinks in radians. If you pass 90.0 into sin(), C isn't calculating the sine of 90 degrees; it's calculating the sine of 90 radians (which is about 14.3 full rotations plus a bit more).
To fix this, I need to convert degrees to radians. The formula is simple: radians = degrees * (PI / 180). Now, C doesn't actually provide a PI constant in the standard math.h on all systems (though some have M_PI), so I'll just define it myself to be safe.
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846
int main() {
double degrees = 90.0;
double radius = 100.0;
// Convert degrees to radians first
double radians = degrees * (PI / 180.0);
double x = radius * cos(radians);
double y = radius * sin(radians);
printf("Coordinates: x=%.2f, y=%.2f\n", x, y);
return 0;
}
Running this now gives me x=-0.00, y=100.00. That negative zero is just a floating-point quirk, but for all intents and purposes, it's exactly where I wanted it.
Handling the Linker Headache
If you're compiling this on a Linux system with GCC, you might have noticed a weird "undefined reference to 'sin'" error. This is a classic C gotcha. The math functions aren't in the standard C library; they're in a separate library called libm. You have to explicitly tell the compiler to link it by adding -lm to the end of your command:
gcc main.c -o orbit -lm
Without that flag, the compiler knows the function exists (thanks to the header), but the linker has no idea where the actual machine code for those functions is hiding.
Moving Beyond the Unit Circle
Now that the math is working, I realized that having the point orbit (0,0) isn't very useful if my screen center is actually at (400, 300). To shift the orbit, I just need to add the center offsets to the final result.
double centerX = 400.0;
double centerY = 300.0;
double x = centerX + (radius * cos(radians));
double y = centerY + (radius * sin(radians));
With this, I can loop through angles from 0 to 360 and plot a perfect circle anywhere on the screen. It's a simple pattern, but it's the foundation for everything from character rotation in games to drawing a clock face.
📋 Practical Task
Project: Analog Clock Hand Coordinate Generator
Your task is to write a program that calculates the (x, y) coordinates for the tip of a clock hand.
Given a clock center at (200, 200) and a hand length of 150 units, calculate the coordinates for the tip of the hand at three specific times: 3 o'clock, 6 o'clock, and 12 o'clock.
Important Constraints:
- Use
math.hand define your ownPIconstant. - Remember that in standard trigonometry, 0 degrees is to the right (3 o'clock), and angles increase counter-clockwise. However, on a clock, 12 o'clock is the top. You will need to adjust your angles accordingly (e.g., 12 o'clock is 90 degrees in a standard Cartesian system).
- Print the resulting X and Y coordinates for each of the three positions.
There are no comments for now.