Draw an ellipse divided by straight line into two colored part in C++ Graphics

Prerequisite: How to include graphics.h?, graphics.h
The task is to draw an ellipse that is intersected by a straight line passing through the center of the ellipse diagonally. And then color both segments with different colors.

Approach: To run the program we need to include the following header file:  

#include <graphic.h>

We will create the desired ellipse with the help of the following functions: 

Below is the implementation of the above approach: 

C++



filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <graphics.h>
 
// 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;
}

chevron_right


Output: 
 


Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : bansalpriyal45

Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.