Animate Cosine wave using C graphics
#include <conio.h>
#include <math.h>
#include <graphics.h>
#include <dos.h>
int main() {
/* request auto detection */
int gdriver = DETECT, gmode, err;
int i = 0, fq, amp;
double x, y;
/* initialize graphic mode */
initgraph(&gdriver, &gmode, "C:/TURBOC3/BGI");
err = graphresult();
if (err != grOk) {
/* error occurred */
printf("Graphics Error: %s\n",
grapherrormsg(err));
return 0;
}
/* 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();
return 0;
}
Output: (Cosine Wave - Animation using c graphics)
No comments:
Post a Comment