C Program to Draw Sine Wave Using C Graphics

Here is the C graphics program to draw sine wave using graphics.h header file. In this program, we will draw a horizontal sine 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 sine 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 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);
  delay(100);

  /* increment angle */
  angle+=5;
 }

 closegraph();

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

Conclusion

In conclusion, this tutorial has taken you on a creative journey into the realm of graphics programming, specifically focusing on the mesmerizing visualization of a sine wave using Turbo C. We began by setting up the Turbo C environment, a nostalgic yet powerful platform that has introduced countless programmers to the magic of graphics. Through the utilization of the sine function, we learned to elegantly craft the oscillating pattern of a sine wave on the digital canvas.

Enhancements such as introducing vibrant colors and dynamic user inputs for amplitude and frequency have not only made the sine wave visually appealing but also showcased the flexibility and interactivity that graphics programming can offer. The ability to dynamically adjust parameters adds a layer of customization, allowing programmers to explore and visualize the nuances of mathematical functions in real-time.

As you embark on your graphics programming journey, consider expanding upon this foundation. Experiment with different mathematical functions, explore diverse color palettes, or delve into the realm of animations to breathe life into your visual creations. Turbo C graphics, although rooted in the past, continues to be a valuable canvas for learning graphics principles that transcend time.

Remember, the allure of graphics programming lies in the delicate balance between mathematical precision and artistic expression. Turbo C has been a reliable companion for countless programmers, fostering creativity and sparking curiosity. Embrace the knowledge gained in this tutorial, let your imagination soar, and continue to explore the boundless possibilities that graphics programming unfolds

Related Topics
C Program to draw cosine(cos) wave graph
C Program to draw tangent(tan) graph
C Program to draw concentric circles screen
C program to draw pie chart using graphics
C Program to draw digital counter on screen
C graphics program for moving car animation
C program for bouncing ball animation
List of all C Graphics Programs