How to create a Heart using C Graphics - GeeksforGeeks

04 Aug, 2021
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:
rectangle(x1,y1,x2,y2): A function from graphics.h header file is responsible for creating rectangle on the screen.ellipse(x,y,a1,a2,r1,r2): A function from graphics.h header file is responsible for creating ellipse on the screen.line(x1,y1,x2,y2): A function from graphics.h header file which draw a line.setfillstyle(pattern, color): The header file graphics.h contains setfillstyle() function which sets the current fill pattern and fills color.floodfill(pattern, color): function is used to fill an enclosed area. The current fill pattern and fill color is used to fill the area.
rectangle(x1,y1,x2,y2): A function from graphics.h header file is responsible for creating rectangle on the screen.
ellipse(x,y,a1,a2,r1,r2): A function from graphics.h header file is responsible for creating ellipse on the screen.
line(x1,y1,x2,y2): A function from graphics.h header file which draw a line.
setfillstyle(pattern, color): The header file graphics.h contains setfillstyle() function which sets the current fill pattern and fills color.
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
// 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 libraryvoid heartDraw(){    // Initialize graphic driver    int gd = DETECT, gm;    clrscr();         // 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    closegraph();    closegraph();} // Driver Codeint main(){    // Function call    heartDraw();    return 0;}
Output: Below is the output of the above program: 
 
simmytarika5
c-graphics
computer-graphics
C Programs
School Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C Program to read contents of Whole File
Producer Consumer Problem in C
Exit codes in C/C++ with Examples
C program to find the length of a string
Handling multiple clients on server with multithreading using Socket Programming in C/C++
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects