Animate hurricane using C graphics
#include <conio.h>
#include <math.h>
#include <graphics.h>
#include <dos.h>
/* draws hurricane from the given position */
void storm_up(int x, int y) {
int i, sangle, eangle, radius = 20;
sangle = 240; eangle = 300;
/* use upper arcs to draw waves in hurricane */
for (i = 0; i < 100; i++) {
if (i % 7 == 0) {
y = y - 5;
arc(x, y, sangle, eangle, radius);
} else if (i % 5 == 0) {
y = y - 5;
arc(x - 10, y, sangle, eangle, radius);
} else if (i % 3 == 0) {
y = y - 5;
arc(x + 10, y, sangle, eangle, radius);
}
if (i % 2 == 0) {
radius = radius + 2;
} else if (i % 25 == 0) {
radius = radius - 10;
}
}
}
/* draws hurricane from the given position */
void storm_down(int x, int y) {
int i, sangle, eangle, radius = 20;
sangle = 60; eangle = 120;
/* down waves in hurricane */
for (i = 0; i < 100; i++) {
if (i % 7 == 0) {
y = y - 4;
arc(x - 10, y, sangle, eangle, radius);
} else if (i % 5 == 0) {
y = y - 4;
arc(x + 10, y, sangle, eangle, radius);
} else if (i % 3 == 0) {
y = y - 4;
arc(x, y, sangle, eangle, radius);
}
if (i % 2 == 0) {
radius = radius + 2;
} else if (i % 25 == 0) {
radius = radius - 10;
}
}
}
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;
}
x = 10.5;
fq = 2;
amp = 50;
line(0, getmaxy() / 2, getmaxx(), getmaxy() / 2);
/* get the motion of hurricane using cos wave */
while (x < getmaxx()) {
cleardevice();
setcolor(i % 15 ? i % 15 : 1);
y = amp * cos((3.14 * fq * x) / 180);
y = y + getmaxy() / 2 + 200;
setlinestyle(SOLID_LINE, 1, 3);
/* locates waves of hurricane in different position */
if (i % 2 == 0) {
storm_down(x, y);
} else {
storm_up(x, y);
}
x = x + 5;
i++;
}
/* clears the graphic device */
cleardevice();
getch();
/* deallocates memory allocated for graphic screen */
closegraph();
return 0;
}
Output:
No comments:
Post a Comment