Open In App

C Program to create a House using Graphics

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: graphics.h, How to include graphics.h in CodeBlocks?

The task is to write C program to create a house using graphics.

To run the program we have the include the below header file:

#include <graphic.h>

Setting Up the Environment:

  1. Download the WinBGlm zip file from this link.
  2. Extract the WinBGlm zip at any desired directory as shown below:
  3. Copy the header file graphic.h and winbgim.h and paste these file inside the folder Program Files->CodeBlock->MinGW->Include_folder.
  4. Also copy the libbgi.a and paste inside the folder Program Files->CodeBlock->MinGW-> lib_folder.
  5. After this open your Code::Blocks and goto the Setting->Compiler->Linker Settings as shown below:
  6. Add New and Browse the file where the libbgi.a is available which is lib folder.
  7. 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:

  1. 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.

  2. 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.

  3. The setfillstyle() function which sets any fill pattern in any shape created in C program using graphics.
  4. 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:



Last Updated : 14 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads