Draw a triangle in C++ graphics
Last Updated :
02 Sep, 2020
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++
#include <graphics.h>
#include <iostream>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "" );
line(150, 150, 450, 150);
line(150, 150, 300, 300);
line(450, 150, 300, 300);
getch();
closegraph();
}
|
Output:
Below is the output of the above program:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...