Draw a triangle in C++ graphics

Prerequisite: graphics.h, How to include graphics.h in CodeBlocks?

The task is to write a C program to make a triangle with the line function of graphics.

To run the program we have to include the below header file:

#include <graphic.h>

Approach: The idea is to create a triangle with the help of several lines. We will draw a line in graphics by passing 4 numbers to line() function as:

line(a, b, c, d)
The above function will draw a line from coordinates (a, b) to (c, d) in the output window. 

Below is the implementation of the above approach:






// C++ program for drawing a triangle
#include <graphics.h>
#include <iostream>
// Driver code
int main()
    // 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;
    // initgraph initializes
    // the graphics system
    // by loading a graphics
    // driver from disk
    initgraph(&gd, &gm, "");
    // Triangle
    // line for x1, y1, x2, y2
    line(150, 150, 450, 150);
    // line for x1, y1, x2, y2
    line(150, 150, 300, 300);
    // line for x1, y1, x2, y2
    line(450, 150, 300, 300);
    // closegraph function closes
    // the graphics mode and
    // deallocates all memory
    // allocated by graphics system
    // Close the initialized gdriver


Below is the output of the above program:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

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.

Article Tags :
Practice Tags :


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