Draw a line in C graphics by R4R Team

- line() is used to draw a line in graphical screen.

Syntax-
line(x1,y1,x2,y2);
where,
(x1,y1) is first coordinate
(x2,y2) is second coordinate


program-

#include< stdio.h>
#include< conio.h>
#include< stdlib.h>
#include< graphics.h>
void main()
{
int gd=DETECT,gm=0,x1,x2,y1,y2;
initgraph(&gd,&gm,"");
x1=100;
y1=100;
x2=200;
y2=200;
line(x1,y1,x2,y2);
getch();
}


output-



-In this program, we just draw a one line with given coordinate.

Now Display 100 random lines with different colors:


program-

#include< stdio.h>
#include< conio.h>
#include< stdlib.h>
#include< graphics.h>
void main()
{
int gd=DETECT,gm=0,x1,y1,x2,y2,i;
initgraph(&gd,&gm,"");
for(i=0;i< 100;i++)
{
setcolor(i);
x1=rand()%500;
y1=rand()%500;
x2=rand()%500;
y2=rand()%500;
line(x1,y1,x2,y2);
}
getch();
}


output:




Leave a Comment: