Introduction :
In graphics programming, sometime you have to draw lines on the graphics screen to create graphical programs. This basic task can be done using line() function in C or C++ graphics programming language. The line() function is used to draw a straight line between two points on the graphics screen.
In this article, I shall show you how to draw lines from one point to another point using line() function in C or C++ graphics programming language. Here, I also show you the syntax and usage of line() function of graphics.h library in the Turbo C++ IDE.
What is line() function :
The line() function is a function of the graphics.h library. It is commonly used in C or C++ graphics programming language. To draw straight lines from one point to another on the graphics screen, you can use line() function. It is useful in creating graphical elements such as different shapes, charts etc.
Syntax of the line() function :
The following code is the syntax of the line() function in C or C++ graphics programming language.
void line(int x1, int y1, int x2, int y2);
The line() function does not return any value. It takes four parameters. The first (x1) and second (y1) parameters are the x and y coordinate of the starting point of the line. The third (x2) and fourth (y2) parameters are the x and y coordinate of the ending point of the line.
About the program :
This is a simple graphics program that shows you how to use the line() function of graphics.h in Turbo C++ IDE. When you run this program, you see a graphical window will appear with black background color on your PC. You also see a red line will be drawn from (100, 100) to (400, 300) on the screen. If you press any key, the program will be stopped.
Explanation of the program :
You have to include graphics.h and conio.h header files in your program. Then, declare “x1”, “x2”, “y1” and “y2” integer variables in the main() function. Then, declare “graphic_driver” and “graphic_mode” which are used to initialize the graphics system. Now, you have to call initgraph() function to initialize the graphics mode. To set the current drawing color, you can call setcolor() function.
After that, call the line() function with parameters to draw a line on the graphics screen. Here, I use getch() function to take a key from the user. At last, you have to call closegraph() function to close the graphics mode properly.
How run the program :
To run the program, you have to install the Turbo C++ IDE on your PC. Then, open the Turbo C++ IDE and create a C or C++ file. Now, copy the below code and paste in your file in the Turbo C++ IDE. You have to know how to copy paste in the Turbo C++ IDE. Do you know how to use graphics.h in Turbo C++ IDE?
Source code of the program :
The following code is the source code of the program.
/*Developed by Puskar Jasu*/
#include <graphics.h>
#include <conio.h>
int main()
{
int x1 = 100, x2 = 400, y1 = 100, y2 = 300;
int graphic_driver = DETECT, graphic_mode;
initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
setcolor(4);
line(x1, y1, x2, y2);
getch();
closegraph();
return 0;
}
Output of the program :
If you run the program, you can see the output of the program on your PC.

Conclusion :
At last, you have learned how to use line() function in the C or C++ graphics programming language. Now you can use line() function in your programs. Thank you for visiting my site.