C Program to create a House using Graphics
Prerequisite: graphics.h, How to include graphics.h in CodeBlocks?
To run the program we have the include the below header file:
#include <graphic.h>
Setting Up the Environment:
- Download the WinBGlm zip file from this link.
- Extract the WinBGlm zip at any desired directory as shown below:
- Copy the header file graphic.h and winbgim.h and paste these file inside the folder Program Files->CodeBlock->MinGW->Include_folder.
- Also copy the libbgi.a and paste inside the folder Program Files->CodeBlock->MinGW-> lib_folder.
- After this open your Code::Blocks and goto the Setting->Compiler->Linker Settings as shown below:
- Add New and Browse the file where the libbgi.a is available which is lib folder.
- And in other linker option paste this: “lbgi lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32“, Click on OK and Exit from Code::Blocks.
Approach: We will create a house with the help of several lines and rectangles. Below are the steps:
- We will draw a line in graphics by passing 4 numbers to line() function as:
line(a, b, c, d)
The above function will draw a line from coordinates (a, b) to (c, d) in the output window. - IWe will draw a rectangle in graphics by passing 4 numbers to rectangle() function as:
line(left, top, right, bottom)
The above function will draw a rectangle with coordinates of left, right, top and bottom. - The setfillstyle() function which sets any fill pattern in any shape created in C program using graphics.
- The floodfill() function is used to fill an enclosed area with any color.
Below is the implementation of the above approach:
// C program to draw a house using // graphics.h library #include <conio.h> #include <graphics.h> #include <stdio.h> // Driver Code void main() { // Initialize of gdriver with // DETECT macros int gdriver = DETECT, gmode; // Initialize structure of // the house initgraph(&gdriver, &gmode, "" ); // Create lines for structure // of the House line(100, 100, 150, 50); line(150, 50, 200, 100); line(150, 50, 350, 50); line(350, 50, 400, 100); // Draw rectangle to give proper // shape to the house rectangle(100, 100, 200, 200); rectangle(200, 100, 400, 200); rectangle(130, 130, 170, 200); rectangle(250, 120, 350, 180); // Set color using setfillstyle() // which take style and color as // an argument setfillstyle(2, 3); // Fill the shapes with colors white floodfill(131, 131, WHITE); floodfill(201, 101, WHITE); // Change the filling color setfillstyle(11, 7); // Fill the shapes with changed colors floodfill(101, 101, WHITE); floodfill(150, 52, WHITE); floodfill(163, 55, WHITE); floodfill(251, 121, WHITE); // Close the initialized gdriver closegraph(); } |
Output:
Below is the output of the above program:
Contact Us