Implement nested squares 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 - 15;
y1 = midy - 15;
x2 = midx + 15;
y2 = midy + 15;
for (i = 1; i <= 15; i++) {
tmpx1 = x1, tmpy1 = y1;
tmpx2 = x2, tmpy2 = y2;
/* drawing nested squares 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 + 15;
tmpy1 = tmpy1 + 15;
tmpx2 = tmpx2 - 15;
tmpy2 = tmpy2 - 15;
}
x1 = x1 - 15, x2 = x2 + 15;
y1 = y1 - 15, y2 = y2 + 15;
sleep(1);
}
/* drawing nested squares out to in */
for (i = 15; i > 0; i--) {
x1 = x1 + 15;
y1 = y1 + 15;
x2 = x2 - 15;
y2 = y2 - 15;
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 squares using c graphics)
No comments:
Post a Comment