Как создать сердце с помощью C Graphics

Опубликовано: 4 Марта, 2022

Предпосылка: graphics.h, Как включить graphics.h?

Задача состоит в том, чтобы написать программу на C для рисования Сердца с использованием графики на C.

Подход: для запуска программы мы должны включить заголовочный файл, указанный ниже:

 #include <graphic.h>

Мы создадим Сердце с помощью следующих функций:

  1. rectangle (x1, y1, x2, y2): функция из заголовочного файла graphics.h отвечает за создание прямоугольника на экране.
  2. ellipse (x, y, a1, a2, r1, r2): функция из заголовочного файла graphics.h отвечает за создание эллипса на экране.
  3. line (x1, y1, x2, y2): функция из заголовочного файла graphics.h, которая рисует линию.
  4. setfillstyle ( узор, цвет): Заголовочный файл graphics.h содержит функцию setfillstyle (), которая устанавливает текущий узор заливки и цвет заливки.
  5. floodfill ( узор, цвет): функция используется для заливки замкнутой области. Текущий узор заливки и цвет заливки используются для заливки области.

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()
{
    // Initilize 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 Code
int main()
{
    // Function call
    heartDraw();
    return 0;
}

Выход:
Ниже приведен результат работы вышеуказанной программы:

Хотите узнать о лучших видео и практических задачах, ознакомьтесь с Базовым курсом C для базового и продвинутого C.