Graphics in c – C Program to Draw Sine Wave Using C Graphics

Write a program in C to draw sine wave using graphics.h header file

Graphics in c: 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.

C Program to Draw Sine Wave Using C Graphics

#include <conio.h>
#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;
 }
 
 getch();
 
 /* deallocate memory allocated for graphics screen */
 closegraph();
 
 return 0;
}

SINE_GRAPH

Test Yourself:

  1. Cprogram to draw star using graphics?
  2. Drawing shapes in c graphics?
  3. Graphics program in c?
  4. Sine wave program in embedded c?
  5. C program to draw fish in computer graphics?
  6. Draw circle in c without graphics?
  7. C program to draw cosine wave?
  8. Sine and cosine program in c?
  9. C program to draw a line without using graphics?
  10. C program to generate sine wave?
  11. C program to draw a circle without using graphics?
  12. C graphics program to draw a line?
  13. C program to draw a triangle using graphics?

Also Read: C Program to Make a Counter Using C Graphics