📜  如何使用C Graphics创建Heart

📅  最后修改于: 2021-05-28 03:51:23             🧑  作者: Mango

先决条件: graphics.h,如何包含graphics.h?

任务是编写一个C程序以使用C中的图形绘制Heart。

方法:要运行该程序,我们包含以下头文件:

#include 

我们将在以下功能的帮助下创建心脏:

  1. 矩形(x1,y1,x2,y2): graphics.h头文件中的函数负责在屏幕上创建矩形。
  2. 椭圆(x,y,a1,a2,r1,r2): graphics.h头文件中的函数负责在屏幕上创建椭圆。
  3. line (x1,y1,x2,y2):来自graphics.h头文件的函数,该函数绘制一条线。
  4. setfillstyle( pattern,color):头文件graphics.h包含setfillstyle()函数,该函数设置当前填充图案并填充颜色。
  5. floodfill( pattern,color):用于填充封闭区域的函数。当前的填充图案和填充颜色用于填充区域。

以下是使用C语言中的图形绘制Heart的实现:

// C program to create heart on the
// screen using graphics. This program
// would only work in Turbo C compiler
// in DOS compatible machine
#include 
#include 
  
// 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基础课程》。