Write a program in C to draw a hut and color it using graphics.h header file
In this program, we will draw a hut on screen using line and rectangle function and then fill it with different patterns and colors. We will use below mentioned graphics functions in this program.
Function Argument | Description |
---|---|
initgraph | It initializes the graphics system by loading the passed graphics driver then changing the system into graphics mode. |
setcolor | It changes the current drawing colour. Default colour is white. Each color is assigned a number, like BLACK is 0 and RED is 4. Here we are using colour constants defined inside graphics.h header file. |
setfillstyle | It sets the current fill pattern and fill color. |
rectangle | It draws a rectangle on screen. It takes the coordinates of top left and bottom right corners. |
line | It draws a straight line between two points on screen. |
floodfill | It is used to fill a closed area with current fill pattern and fill color. It takes any point inside closed area and color of the boundary as input. |
closegraph | It unloads the graphics drivers and sets the screen back to text mode. |
C program to draw a hut and color it using graphics
#include<graphics.h> #include<conio.h> int main(){ int gd = DETECT,gm; initgraph(&gd, &gm, "X:\\TC\\BGI"); /* Draw Hut */ setcolor(WHITE); rectangle(150,180,250,300); rectangle(250,180,420,300); rectangle(180,250,220,300); line(200,100,150,180); line(200,100,250,180); line(200,100,370,100); line(370,100,420,180); /* Fill colours */ setfillstyle(SOLID_FILL, BROWN); floodfill(152, 182, WHITE); floodfill(252, 182, WHITE); setfillstyle(SLASH_FILL, BLUE); floodfill(182, 252, WHITE); setfillstyle(HATCH_FILL, GREEN); floodfill(200, 105, WHITE); floodfill(210, 105, WHITE); getch(); closegraph(); return 0; }
Program Output