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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | //Draw a Line program in Computer Graphics #include<graphics.h> #include<stdio.h> #include<conio.h> void main() { int gdriver = DETECT, gmode; int x1 = 100, y1 = 100; int x2 = 300, y2 = 300; clrscr(); initgraph(&gdriver, &gmode, "c:\turboc3\bgi"); line(x1, y1, x2, y2); getch(); closegraph(); } |
Output
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.
Declaration:
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.
line(x1,y1,x2,y2);
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
2. Bresenham Line Drawing Algorithm
3. Mid Point Line Drawing Algorithm