Complete C Program to demonstrate viewport, clipping and lines with different colors, line styles and co- ordinates
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<stdio.h>
void main()
{
int gm, gd=DETECT;
int x1,x2,y1,y2,c,i;
clrscr();
printf(“enter starting co-ordinates of viewport (x1,y1)/n”);
scanf(“%d%d”,&x1,&y1);
printf(“enter ending co-ordinates of viewport(x2,y2)/n”);
scanf(“%d%d”,&x2,&y2);
initgraph(&gd,&gm,””);
rectangle(x1,y1,x2,y2); /*to show the boundary of viewport */
setviewport(x1,y1,x2,y2,1); /* view port is set and any drawing now onwards
must be drawn within the viewport only */
while(1kbhit()) /*until pressing any key this continues */
{
/* Rectangle coordinates are taken randomly */
x1=rand()%getmaxx();
x2=rand()%getmaxx();
y1=rand()%getmaxy();
y2=rand()%getmaxy();
setlinestle(rand()%10, rand()%20);
setcolor(rand()%16); /*to set the line color */
line(x1,y1,x2,y2); /*to draw the line */
delay(200);
}
getch();
closegraph(); /*closes the graph mode */
}