C Program to Draw Cosine Wave Using C Graphics

Here is a C program to draw cosine wave using graphics.h header file. In this program, we will draw a horizontal cosine wave on screen of amplitude 50 pixels. We will use putpixel functions of graphics.h header file to color a pixel at (x, y).

C program to draw cosine wave using graphics

In this program, we first initialize graphics mode, by passing graphics driver(DETECT), default graphics mode and specifies the directory path where initgraph looks for graphics drivers (*.BGI). Then we will draw a horizontal axis using line function representing the angle in radians. Inside a for loop we calculate the value of cosine for an angle and color the specific pixel using putpixel function. At last we increment the angle by 5.

#include <math.h>
#include <graphics.h>
#include <dos.h>

int main() {
    int gd = DETECT, gm;
    int angle = 0;
    double x, y;

    initgraph(&gd, &gm, "C:\\TC\\BGI");

 line(0, getmaxy() / 2, getmaxx(), getmaxy() / 2);
 /* generate a sine wave */
 for(x = 0; x < getmaxx(); x+=3) {

     /* calculate y value given x */
     y = 50*sin(angle*3.141/180);
     y = getmaxy()/2 - y;

     /* color a pixel at the given position */
  putpixel(x, y, 15);

  /* increment angle */


 return 0;
Program Output C graphics program to draw cosine wave graph


You've successfully created a C program to draw a cosine wave using Turbo C graphics. This tutorial covered the basics of setting up the Turbo C environment, plotting points to form a cosine wave, incorporating colors, and dynamically adjusting the wave based on user input. Graphics programming introduces a visual dimension to mathematical functions, and Turbo C provides a nostalgic yet robust platform for mastering these concepts.

As you continue your exploration into graphics programming, consider expanding this project by adding interactivity, experimenting with different functions, or creating a visual representation of other mathematical concepts. Turbo C graphics, though rooted in the past, continues to be a valuable canvas for learning and experimenting with graphical concepts.

Remember, the beauty of graphics programming lies in the synergy of mathematical precision and creative expression. Turbo C has been a reliable companion for countless programmers, fostering creativity and sparking curiosity. Embrace the skills gained in this tutorial, let your imagination soar, and continue to explore the boundless possibilities that graphics programming unfolds. Whether you are visualizing mathematical functions, designing simulations, or creating interactive applications, Turbo C graphics provides a timeless foundation for your creative programming endeavors.

Related Topics
C program to draw sine wave graph on screen
C Program to draw tangent(tan) graph
C Program to draw bar graph
C program to draw pie chart using graphics
C graphics program to draw digital clock
C program for bouncing ball animation
C Program to draw stars in night sky
List of all C Graphics Programs