Open In App
Related Articles

How to create a Heart using C Graphics

Like Article
Save Article
Report issue

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

The task is to write a C program to draw a Heart using graphics in C.

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

#include <graphic.h>

We will create a Heart with the help below functions:

  1. rectangle(x1,y1,x2,y2): A function from graphics.h header file is responsible for creating rectangle on the screen.
  2. ellipse(x,y,a1,a2,r1,r2): A function from graphics.h header file is responsible for creating ellipse on the screen.
  3. line(x1,y1,x2,y2): A function from graphics.h header file which draw a line.
  4. setfillstyle(pattern, color): The header file graphics.h contains setfillstyle() function which sets the current fill pattern and fills color.
  5. floodfill(pattern, color): function is used to fill an enclosed area. The current fill pattern and fill color is used to fill the area.

Below is the implementation of to draw Heart using graphics in C:


// C program to create heart on the
// screen using graphics. This program
// would only work in Turbo C compiler
// in DOS compatible machine
#include <graphics.h>
#include <stdio.h>
// Function to create heart using
// graphic library
void heartDraw()
    // Initialize graphic driver
    int gd = DETECT, gm;
    // Initialize graphics mode by passing
    // three arguments to initgraph function
    // &gdriver is the address of gdriver
    // variable, &gmode is the address of
    // gmode and  "C:\\Turboc3\\BGI" is the
    // directory path where BGI files
    // are stored
    initgraph(&gd, &gm, "c:\\turboc3\\bgi");
    // Draw rectangle
    rectangle(150, 50, 450, 350);
    // Draw ellipse
    ellipse(250, 150, 0, 190, 50, 70);
    ellipse(350, 150, -10, 180, 50, 70);
    // Draw line
    line(200, 160, 300, 310);
    line(400, 160, 300, 310);
    // Set rectangle color
    setfillstyle(10, 4);
    // To fill color
    floodfill(155, 200, WHITE);
    // Set heart color
    setfillstyle(1, 4);
    // To fill color
    floodfill(300, 200, WHITE);
    // closegraph function closes the
    // graphics mode and deallocates
    // all memory allocated by
    // graphics system
// Driver Code
int main()
    // Function call
    return 0;


Below is the output of the above program: 


Last Updated : 04 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads