Cosine Wave - Using C graphics


Cosine Wave - Animation using C graphics



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

  void main() {
        /* request auto detection */
        int gdriver = DETECT, gmode, err;
        int i = 0, fq, amp;
        double x, y;

        /* initialize graphic mode */
        initgraph(&gdriver, &gmode, "C:/TC/BGI");

        /* initialize the variables */
        x = 0.5;
        fq = 2;

        /* setting the amplitute to 50 */
        amp = 50;
        line(0, getmaxy() / 2, getmaxx(), getmaxy() / 2);

        /* draws cosine wave */
        while (x < getmaxx()) {
                setcolor(i % 15 ? i % 15 : 1);

                /* calculate y given x value- using cosine wave formula */
                y = amp * cos((3.14 * fq * x)  / 180);
                y = y + getmaxy() / 2;

                /* plot the pixel at (x, y) */
                putpixel(x, y, 15);

                /* draw vertical lines at the current plot */
                line(x, y, x, y - 12);
                line(x, y, x, y + 12);

                /* sleep for 100 milliseconds */
                delay(100);

                /* increment x */
                x = x + 5;
                i++;
        }

        getch();

        /* deallocate memory allocated for graphic screen */
        closegraph();
  }


OUTPUT:-

Comments

Popular posts from this blog

Implement Bouncing Ball Animation Using C Graphics

Walking Stickman -Using C graphics

Sine Wave - Using C Graphics