How to Draw a Line

How to Draw a Line in Computer Graphics

In this program, you will learn that how to draw a line in computer graphics using line function. Line function is a builtin function that is define in the graphics.h header file.

Program Code to Draw a line in C graphics


How to Draw a Line


Program Explanation

⮞ First step in any graphics program is use to include the graphics.h header file. This header file provides the access of graphics library functions for drawing the lines, circle, rectangles, ovals, polygons and images etc.

⮞ Second step is to initialize the graphics system using the initgraph method of graphics.h library.


void initgraph(int *graphdriver, int *graphmode, char *pathtodriver);

graphdriver :- It is pointer to integer that specifies the graphics driver to be used.

graphmode:- It is pointer to integer that specifies the initial graphics mode. Initgraph sets graphmode to the greatest resolution available for the identified driver when we set graphicsdriver =DETECT.

pathtodriver :- It specifies the directory path where all graphics driver(*.bgi) files are located.

⮞ Third step is used to set the coordinates values
int x1 = 100, y1 = 100;
int x2 = 300, y2 = 300;

⮞ Forth step is used to call parameters to the line function.
Parameter Explanation

x1 – X Coordinate of First Point
y1 – Y Coordinate of First Point
x2 – X Coordinate of Second Point
y2 – Y Coordinate of Second Point

⮞ Final step : In it we have to unload the graphics drivers by calling closegraph function.


Draw Obejcts using Different Drawing Algorithms

1.  DDA Line Drawing Algorithm

2.  Bresenham Line Drawing Algorithm

3.  Mid Point Line Drawing Algorithm

4. Animated Line Drawing in C 

5. Draw Circle in C graphics





Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top