SimpleGFX  1.0
Simple Graphics Library for C Programming Education
m-sine-wave.c File Reference

Introduction to trigonometry through sine wave visualization. More...

#include "gfx/simplegfx.h"
#include <math.h>

Functions

int main ()
 

Detailed Description

Introduction to trigonometry through sine wave visualization.

Author
Sukesh Ashok Kumar

This example visualizes the sine function, one of the most important functions in mathematics. Sine waves appear in sound, light, ocean waves, alternating current, and countless natural phenomena.

Mathematical Concepts:

  • Sine function: y = A·sin(B·x + C) + D (general form)
  • Amplitude (A): maximum displacement from center (wave height)
  • Frequency (B): how many cycles per unit distance
  • Period: 2π/B (length of one complete wave cycle)
  • Phase shift (C): horizontal shift left/right
  • Vertical shift (D): moves entire wave up/down
  • Radians: angular measure where 2π radians = 360 degrees
  • Range: sin(x) always stays between -1 and +1

In This Example:

  • A = 100: wave oscillates 100 pixels above and below center
  • B = 0.02: creates about 2 complete waves across 640 pixels
  • C = 0: no phase shift (wave starts at midpoint going up)
  • D = 240: centers wave vertically in middle of window
  • Period = 2π/0.02 ≈ 314 pixels per complete wave

Sine Wave Properties:

  • Periodic: pattern repeats every 2π/B units
  • Smooth and continuous: no sharp corners
  • Oscillates between (D-A) and (D+A)
  • In this example: oscillates between 140 and 340

Programming Concepts:

  • Using sin() function from math.h (requires -lm flag)
  • Floating-point arithmetic for smooth curves
  • Combining multiplication and addition for transformations
  • Understanding radian measure

What you'll learn:

  • How sine creates smooth, periodic oscillations
  • The meaning of amplitude, frequency, and phase
  • Converting between degrees and radians
  • Why trigonometry is essential for graphics
  • How to create wave patterns programmatically
  • Including math library and linking with -lm

Understanding the Parameters:

  • Amplitude (A): distance from center to peak
    • Larger A = taller waves
    • A = 0 = straight horizontal line
  • Frequency (B): how compressed/stretched waves are
    • Larger B = more waves (higher frequency)
    • Smaller B = fewer waves (lower frequency)
  • Phase Shift (C): horizontal slide
    • C > 0 = shift left
    • C < 0 = shift right
  • Vertical Shift (D): moves equilibrium
    • Larger D = wave moves up
    • Smaller D = wave moves down

Experiment Ideas:

  • Change amplitude to 50: smaller waves
  • Change frequency to 0.04: double the waves
  • Change phaseShift to π/2: shifts wave left
  • Change verticalShift to 100: moves wave up

Compile:

gcc m-sine-wave.c gfx/simplegfx.c -o output/m-sine-wave -lX11 -lm

Note: -lm links the math library for sin() function

Run:

./output/m-sine-wave

Function Documentation

◆ main()

int main ( )