C program to draw shapes(Circle, Rectangle, Square, Ellipse, Pie slice & Polygon)
#include <conio.h>
#include <graphics.h>
#include <dos.h>
int main() {
/* request auto detection */
int gdriver = DETECT, gmode, err;
int i, radius, x1, y1, val, xrad, yrad;
int x[6], y[6], poly[12];
/* 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;
}
/*
* setting initial position for
* circle, rectangle, square,
* ellipse, arc and polygons
*/
x[0] = x[3] = (getmaxx() / 3) / 2;
x[1] = x[4] = 2 * ((getmaxx() / 2) / 2);
x[2] = x[5] = 3 * ((getmaxx() / 2) / 2);
y[0] = y[1] = y[2] = (getmaxy() / 2) / 2;
y[3] = y[4] = y[5] = 3 * ((getmaxy() / 2) / 2);
radius = 100;
x1 = 60, y1 = 30;
val = 50, xrad = 100, yrad = 50;
poly[0] = x[5], poly[1] = y[5] - 50;
poly[2] = x[5] + 50, poly[3] = y[5];
poly[4] = x[5] + 40, poly[5] = y[5] + 50;
poly[6] = x[5] - 40, poly[7] = y[5] + 50;
poly[8] = x[5] - 50, poly[9] = y[5];
poly[10] = x[5], poly[11] = y[5] - 50;
/* draws nested shapes */
for (i = 1; i < 6; i++) {
setcolor(i);
setfillstyle(SOLID_FILL, i);
/* drawing circle */
circle(x[0], y[0], radius);
floodfill(x[0], y[0], i);
/* draws rectangle */
rectangle(x[1] - x1, y[1] - y1, x[1] + x1, y[1] + y1);
floodfill(x[1] - x1 + 1, y[1] - y1 + 1, i);
/* draws square */
rectangle(x[2] - val, y[2] - val, x[2] + val, y[2] + val);
floodfill(x[2] - val + 1, y[2] - val + 1, i);
/* draws ellipse */
ellipse(x[3], y[3], 0, 360, xrad, yrad);
floodfill(x[3], y[3], i);
/* draws a circular pie slice */
pieslice(x[4], y[4], 30, 150, radius);
/* draws polygon */
drawpoly(6, poly);
floodfill(x[5], y[5], i);
/* manipulating the subsequent positions for all shapes */
poly[1] = poly[1] + 10, poly[2] = poly[2] - 10;
poly[4] = poly[4] - 10, poly[5] = poly[5] - 10;
poly[6] = poly[6] + 10, poly[7] = poly[7] - 10;
poly[8] = poly[8] + 10, poly[11] = poly[11] + 10;
radius = radius - 15;
x1 = x1 - 10, y1 = y1 - 5;
xrad = xrad - 10, yrad = yrad - 5;
val = val - 10;
sleep(1);
}
getch();
/* deallocate memory allocated for graphic screen */
closegraph();
return 0;
}
Output: (Implement nested shapes using c graphics)
No comments:
Post a Comment