C Program to Draw a Line in Computer Graphics

There is a predefined function named line which is used to draw a line on the output screen.

It takes 4 arguments,first two parameters represent an initial point and the last two arguments are for the final points of the line.

What should you know about line function in computer graphics ?

line() is a library function used to draw a line using given coordinates. It comes under Graphic.h header file. It uses two coordinate points (x,y) as initial point and (x1,y1) as end point to draw a line on output screen.

Syntax:

void line( int x,int y,int x1,int y1);

The code given below is a simple graphic program that  draws a line.

Write a C program to draw a line in Computer Graphics?

Program:

#include<conio.h>
#include<graphics.h>
#include<stdio.h>
void main()
{
int gd=DETECT,gm;
int x_initial,y_initial,x_final,y_final;
clrscr();
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\n Please enter an initial coordinate of the line = ");
scanf("%d %d", &x_initial,&y_initial);
printf("\n Now, \n enter final coordinate of the line = ");
scanf("%d %d",&x_final,&y_final);
printf("\n***** LINE ******");
line(x_initial,y_initial,x_final,y_final);

getch();
closegraph();
}

 

Output:

draw a line

Leave a Reply