Skip to content
Related Articles
Get the best out of our app
Open App

Related Articles

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

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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++ 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
    // 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
    // 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);
    // closegraph function closes
    // graphics mode and deallocates
    // all memory allocated by graphics
// Driver Code
int main()
    // Function call
    return 0;


My Personal Notes arrow_drop_up
Last Updated : 28 Oct, 2020
Like Article
Save Article
Similar Reads