📜  在C ++图形中绘制一条由直线分成两个彩色部分的椭圆

📅  最后修改于: 2021-05-31 20:03:42             🧑  作者: Mango

先决条件:如何包含graphics.h ?、 graphics.h
任务是绘制一条椭圆,该椭圆与一条直线相交,该直线通过对角线穿过椭圆的中心。然后用不同的颜色给两个段上色。

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

#include 

我们将借助以下功能来创建所需的椭圆:

  • void ellipse(int x,int y,int from_angle,int to_angle,int x_rad,int y_rad) graphics.h头文件中的函数负责在屏幕上创建椭圆。
  • void line(int x1,int y1,int x2,int y2) graphics.h头文件中的函数,用于绘制一条线。
  • 空隙floodfill(INT的x,INT Y,INT border_color):graphics.h中头文件的函数是负责函数被用于填充的封闭区域。当前的填充图案和填充颜色用于填充区域。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
 
// Function to set the pattern and
// color to be filled in the given
// segment
void fill(int x, int y,
          int color, int boundary)
{  
    //It will sets the current fill
    //pattern and fill color
   
    setfillstyle(SOLID_FILL,color);
   
    // fill the color in segment
    // having point (x, y) and
    // border color = boundary
    floodfill(x, y, boundary);
}
 
// Function to create the ellipse
// using graphic library
void drawEllipse()
{
    // gm is Graphics mode which is a
    // computer display mode that
    // generates image using pixels
 
    // DETECT is a macro defined in
    // "graphics.h" header file
    int gd = DETECT, gm, error;
 
    // initgraph initializes the
    // graphics system
    initgraph(&gd, &gm, "");
 
    // set the color used for drawing
    setcolor(YELLOW);
 
    // Center of ellipse
    int x = 150, y = 150;
 
    // Draw complete ellipse angle will
    // be from 0 to 360
    int from_angle = 0, to_angle = 360;
 
    // Radius of ellipse
    int x_rad = 130, y_rad = 80;
 
    // Ellipse function
    ellipse(x, y, from_angle,
            to_angle, x_rad, y_rad);
 
    // End points of a line passing
    // through center of above ellipse
    int x1 = 80, y1 = 80, x2 = 220, y2 = 220;
 
    // Line for above end points
    line(x1, y1, x2, y2);
 
    // Fill different color in two
    // parts of ellipse, choose point
    // (x-1, y) and (x+1, y)
    // because line is passing diagonally
    fill(x - 1, y, RED, YELLOW);
    fill(x + 1, y, GREEN, YELLOW);
 
    getch();
 
    // closegraph function closes
    // graphics mode and deallocates
    // all memory allocated by graphics
    closegraph();
}
 
// Driver Code
int main()
{
    // Function call
    drawEllipse();
    return 0;
}


输出:

想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”