C program to demonstrate rectangles using putpixel and lines

Complete C program to demonstrate rectangles using putpixel and lines

#include<graphics.h> 
#include<conio.h> 
#include<stdlib.h> 
#include<dos.h> 
void main() 
{ 
int gm,gd=DETECT; 
int x1,x2,y1,y2,c,I; 
initgraph(&gd,&gm,’’’’); 
while(!kbhit())/*until pressing any key this loop continues*/ 
{ 
/*for rectangle co-ordinates are taken randomly*/ 
x1=rand()%getmaxx(); 
x2=rand()%getmaxx(); 
y1=rand()%getmaxy(); 
y2=rand()%getmaxy(); 
if(x1>x2) 
{ 
c=x1; /* exchange of x1 and x2 when x1 >x2 */
x1=x2; 
x2=c; 
} 
if(y1>y2) 
{ 
c=y1; /* exchange of y1 and y2 when y1>y2 */ 
y1=y2; 
y2=c; 
} 
c=rand()%16; 
/*to draw rectangle using putpixel*/ 
for(I=x1;I<=x2;++i) 
{ 
putpixel(I,y1,c); 
delay(1); 
} 
for(I=y1;I<=y2;++i) 
{ 
putpixel(x2,I,c); 
delay(1); 
} 
for(I=x2;I>=x1;_- i) 
{ 
putpixel(I,y2,c); 
delay(1); 
} 
for(I=y2;I>y1;-I) 
{ 
putpixel(x1,I,c); 
delay(1); 
} 
delay(200); /* to draw the pixels slowly */ 
} 
getch(); 
closegraph(); /* closes the graph mode */ 
} 

Leave a Comment