Animate nested rectangles using C graphics
#include <conio.h>
#include <graphics.h>
#include <dos.h>
int main() {
/* request auto detection */
int gdriver = DETECT, gmode, err;
int i, j, midx, midy, x1, y1, x2, y2;
int tmpx1, tmpy1, tmpx2, tmpy2;
/* 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;
}
/* mid position in x and y-axis */
midx = getmaxx() / 2;
midy = getmaxy() / 2;
x1 = midx - 20;
y1 = midy - 10;
x2 = midx + 20;
y2 = midy + 10;
for (i = 1; i <= 15; i++) {
tmpx1 = x1, tmpy1 = y1;
tmpx2 = x2, tmpy2 = y2;
/* drawing nested rectangles in to out */
for (j = i; j > 0; j--) {
setcolor(j);
setfillstyle(SOLID_FILL, j);
rectangle(tmpx1, tmpy1, tmpx2, tmpy2);
floodfill(midx, midy, j);
tmpx1 = tmpx1 + 20;
tmpy1 = tmpy1 + 10;
tmpx2 = tmpx2 - 20;
tmpy2 = tmpy2 - 10;
}
x1 = x1 - 20, x2 = x2 + 20;
y1 = y1 - 10, y2 = y2 + 10;
sleep(1);
}
/* drawing nested rectangles out to in */
for (i = 15; i > 0; i--) {
x1 = x1 + 20;
y1 = y1 + 10;
x2 = x2 - 20;
y2 = y2 - 10;
setcolor(i);
setfillstyle(SOLID_FILL, i);
rectangle(x1, y1, x2, y2);
floodfill(midx, midy, i);
delay(250);
}
getch();
/* deallocate memory allocated for graphic screen */
closegraph();
return 0;
}
Output: (Animate nested rectangles using c graphics)
No comments:
Post a Comment