Computer Graphics easy to learn programming / code



1.       Program converting a line using Digital Differential Algorithm.

#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
void linedda(int xa,int ya,int xb,int yb);
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"\\tc\\bgi\\");
int xa,ya,xb,yb;
cout<<"enter the coordinates"<<endl;
cin>>xa>>ya>>xb>>yb;
linedda(xa,ya,xb,yb);
getch();
closegraph();
restorecrtmode();
}
void linedda(int xa,int ya,int xb,int yb)
{
int step,x,y,k;
float dx,dy,xinc,yinc;
dx=abs(xb-xa);
dy=abs(yb-ya);
if(dx>dy)
step=dx;
else
step=dy;
xinc=dx/step;
yinc=dy/step;
x=xa,y=ya;
putpixel(x,y,5);
for(k=0;k<step;k++)
{
if(xa>xb && ya>yb)
{
x=x-xinc;
y=y-yinc;
putpixel(x,y,5);
delay(50);
}
else if(xa>xb && ya<yb)
{
x=x-xinc;
y=y+yinc;
putpixel(x,y,5);
delay(50);
}
else if (xa<xb && ya>yb)
{
x=x+xinc;
y=y-yinc;
putpixel(x,y,5);
delay(50);
}
else
{
x=x+xinc;
y=y+yinc;
putpixel(x,y,5);
delay(50);
}
}
}












2.       Program to draw a circle using the Mid-point algorithm.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>

float x0,y0;
void circlepoints(float x,float y)
{
putpixel(x0+x,y0+y,1);
putpixel(x0+y,y0+x,2);
putpixel(x0+y,y0-x,3);
putpixel(x0+x,y0-y,4);
putpixel(x0-x,y0-y,5);
putpixel(x0-y,y0-x,6);
putpixel(x0-y,y0+x,7);
putpixel(x0-x,y0+y,8);
}
void main()
{
int a=DETECT,b;
initgraph(&a,&b,"\\tc\\bgi\\");
float x,y,radius,d;
cout<<"\n Enter the radius of the circle:-";
cin>>radius;
cout<<"\n Enter the centre of the circle (x,y):-";
cin>>x0>>y0;
x=0;
y=radius;
d=(5.0/4.0)-radius;
setcolor(YELLOW);
settextstyle(3,0,5);
outtextxy(180,70,"The Circle");
putpixel(x0,y0,13);
circlepoints(x,y);

while(x<y)
{
 if (d<0) //select the lower point i.e. E
   {
   d=d=(2.0*x)+3.0;
   }
 else //select the upper point i.e. SE
   {
    d=d+2.0*(x-y)+5.0;
    y--;
    }
    x++;
    circlepoints(x,y);
    }
    getch();
    closegraph();
 }//end o main

























3.       Program to draw an ellipse using the Mid-point algorithm.



#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>

void ELLIPSE(int x,int y,int a,int b);
void plot(int x,int y,int x1,int y1);

void main()
{
          int driver = DETECT,mode;
          int x,y;
          int a,b;
          clrscr();
          printf("\nEnter x co. for center of Ellipse : ¯ ");
          scanf("%d",&x);
          printf("\nEnter y co. for center of Ellipse : ¯ ");
          scanf("%d",&y);
          printf("\nEnter length of Horizontal axis `2a' : ¯ ");
          scanf("%d",&a);
          a=a/2;
          printf("\nEnter length of Vertical axis `2b' : ¯ ");
          scanf("%d",&b);
          b=b/2;
          printf("\n\n\nPress any key to see the Ellipse......");
          getch();
          initgraph(&driver,&mode,"c:\\tc\\bgi");
          clearviewport();
          ELLIPSE(x,y,a,b);
          getch();
          closegraph();
}

void ELLIPSE(int x1,int y1,int a,int b)
{
          int x=0,y=b;
          double d1,d2;
          d1=pow(b,2)-(pow(a,2)*b)+(0.25*pow(a,2));
          while(pow(a,2)*(y-0.5)>=pow(b,2)*(x+1))
          {
                   plot(x,y,x1,y1);
                   if(d1<0)
                             d1+=pow(b,2)*(2*x+3);
                   else
                   {
                             d1+=pow(b,2)*(2*x+3)+pow(a,2)*(-2*y+2);
                             y--;
                   }
                   x++;
          }
          d2=pow(b,2)*pow(x+0.5,2)+pow(a,2)*pow(y-1,2)-(pow(a,2)*pow(b,2));
          while(y>=0)
          {
                   plot(x,y,x1,y1);
                   if(d2<0)
                   {
                             d2+=pow(b,2)*(2*x+2)+pow(a,2)*(-2*y+3);
                             x++;
                   }
                   else
                             d2+=pow(a,2)*(-2*y+3);
                   y--;
          }
}

void plot(int x, int y,int x1,int y1)
{
          putpixel((x+x1),(y+y1),YELLOW);
          putpixel((x+x1),(-y+y1),YELLOW);
          putpixel((-x+x1),(y+y1),YELLOW);
          putpixel((-x+x1),(-y+y1),YELLOW);
}

4.       Program to rotate a coin on table.

#include<graphics.h>
#include<conio.h>
#include<iostream.h>
#include<dos.h>
#include<stdio.h>
#include <stdlib.h>
int main(void)
{
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;
   int midx, midy;
   int stangle = 0, endangle = 360;
   int yradius = 30;

   /* initialize graphics, local variables */
   initgraph(&gdriver, &gmode, "\\tc\\bgi\\");

   /* read result of initialization */
   errorcode = graphresult();
   if (errorcode != grOk)
   /* an error occurred */
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);
   /* terminate with an error code */
   }

   midx = getmaxx() / 2;
   midy = getmaxy() / 2;
   setcolor(BROWN);

   /* draw ellipse */
   settextstyle(8,0,4);
   outtextxy(100,100,"PRESS SPACEBAR TO EXIT ");
   line(midx-80,midy-40,midx+120,midy-40);
   line(midx-100,midy+60,midx-80,midy-40);
   line(midx+100,midy+60,midx+120,midy-40);
   line(midx-100,midy+60,midx+100,midy+60);
   rectangle(midx-100,midy+60,midx-80,midy+160);
   rectangle(midx+100,midy+60,midx+80,midy+160);
   rectangle(midx-80,midy-40,midx-60,midy+40);
   rectangle(midx+120,midy-40,midx+100,midy+40);
   l:
   for(int i=30;i>=0;i--)
   {
   delay(30);
   ellipse(midx, midy, stangle, endangle,i, yradius);
   setfillstyle(1,LIGHTGRAY);
   fillellipse(midx,midy,i,yradius);
   setcolor(BLACK);
   ellipse(midx, midy, stangle, endangle,i, yradius);
   }
   for( i=0;i<=30;i++)
   {
     delay(30);

     ellipse(midx, midy, stangle, endangle,i, yradius);
     setfillstyle(1,LIGHTBLUE);
     fillellipse(midx,midy,i,yradius);
     setcolor(BLACK);
     ellipse(midx, midy, stangle, endangle,i, yradius);

   }
    char ch= getch();
    while(ch!=32)
    {
      goto l;
   }
  /* clean up */
   closegraph();
   return 0;
}


5.       Program to design output of a ball hitting the Four Corners of a window.        

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<dos.h>
int main(void)
{
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;

   /* initialize graphics and local variables */
   initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

   /* read result of initialization */
   errorcode = graphresult();
   if (errorcode != grOk)  /* an error occurred */
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1); /* terminate with an error code */
   }
   setcolor(YELLOW);

   /* draw the circle */
   rectangle(85,90,313,313);

   setcolor(RED);
   while(!kbhit()){
   for ( int i=100,j=100;i<=300,j<=300;i=i+6,j=j+6)
   {
     setcolor(WHITE);
     circle(i, j, 8);
     delay(100);
     setcolor(BLACK);
     circle(i,j,8);

   }

   for (  j=300;j>=100;j=j-6)
   {
     setcolor(WHITE);
     circle(i, j, 8);
     delay(100);
     setcolor(BLACK);
     circle(i,j,8);
   }
   for (  i=300,j=100;i>=100,j<=300;i=i-6,j=j+6)
   {
     setcolor(WHITE);
     circle(i, j, 8);
     delay(100);
     setcolor(BLACK);
     circle(i,j,8);

   }
   for (  j=300;j>=100;j=j-6)
   {
      setcolor(WHITE);
      circle(i, j, 8);
      delay(100);
      setcolor(BLACK);
      circle(i,j,8);
/* clean up */
   }
   }
   getch();
   closegraph();  
return 0;
}





6.       Program to draw a rectangle using line drawing algorithm.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void main()
{
          int driver = DETECT,mode;
          int x1,x2,y1,y2,t,x[5],y[5];
          float m,i,j,s;
          printf("\nEnter LEFT : ¯ ");
          scanf("%d",&x1);
          printf("\nEnter TOP : ¯ ");
          scanf("%d",&y1);
          printf("\nEnter RIGHT : ¯ ");
          scanf("%d",&x2);
          printf("\nEnter BOTTOM : ¯ ");
          scanf("%d",&y2);
          printf("\n\n\nPress any key to see the line...........");
          getch();
          clrscr();
          initgraph(&driver,&mode,"c:\\tc\\bgi");
          clearviewport();
          x[0]=x1;
          y[0]=y1;
          x[1]=x2;
          y[1]=y1;
          x[2]=x2;
          y[2]=y2;
          x[3]=x1;
          y[3]=y2;
          int l,flag=1;
       int        k;

          for(k=0;k<=3;k++){
          l=k+1;
          if(l==4)
          {
          k=2;
          l=0;
          flag=0;

          }

          m=((1.0*(y[l]-y[k]))/(x[l]-x[k]));

          if((m<=1)&&(x[k]!=x[l]))
          {
                   if(x[k]>x[l])
                   {
                             t=x[k];
                             x[k]=x[l];
                             x[l]=t;
                             t=y[k];
                             y[k]=y[l];
                             y[l]=t;
                   }
                   j=y[k];
                   for(i=x[k];i<=x[l];i++)
                   {
                             putpixel(i,j,YELLOW);
                             j=j+m;
                   }
          }

          else
          {
                   if(y[k]>y[l])
                   {
                             t=x[k];
                             x[k]=x[l];
                             x[l]=t;
                             t=y[k];
                             y[k]=y[l];
                             y[l]=t;
                   }
                   i=x[k];
                   if((x[l]-x[k])!=0)
                             s=1/m;
                   else
                             s=0;
                   for(j=y[k];j<=y[l];j++)
                   {
                             putpixel(i,j,YELLOW);
                             i=i+s;
                   }
          }
          if(flag==0)
          k=4;

          }
          getch();
          closegraph();
}






















7.       Program to rotate a circle alternately inside and outside around the circumference of another circle.


#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
#include<stdlib.h>
void erase1(int,int);
void erase2(int,int);
int r=15;
void main()
{
          int gdriver = DETECT,gmode,errcode;
          initgraph(&gdriver,&gmode,"C:\\TC");
          errcode=graphresult();
          if(errcode != 0)
          {
                   cout<<grapherrormsg(errcode);
                   exit(0);
          }
          delay(400);
          circle(319,239,100);
          erase1(319,159);
          erase2(369,119);
          erase1(369,189);
          erase2(439,189);
          erase1(399,239);
          erase2(439,289);
          erase1(369,289);
          erase2(369,359);
          erase1(319,319);
          erase2(259,359);
          erase1(269,289);
          erase2(199,289);
          erase1(239,239);
          erase2(199,189);
          erase1(259,189);
          erase2(259,119);
          erase1(319,159);
          circle(319,159,r);
          getch();
          closegraph();
}
void erase1(int a,int b)
{
          for(int i=r;i>=0;i--)
          {
                   setcolor(WHITE);
                   ellipse(a,b,0,360,i,r);
                   delay(30);
                   setcolor(BLACK);
                   ellipse(a,b,0,360,i,r);
                   setcolor(WHITE);
          }
}
void erase2(int a,int b)
{
          for(int i=5;i<=r;i++)
          {
                   setcolor(WHITE);
                   ellipse(a,b,0,360,i,r);
                   delay(30);
                   setcolor(BLACK);
                   ellipse(a,b,0,360,i,r);
                   setcolor(WHITE);
          }
}









8.       Program to design flying colored balloons.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"\\tc\\bgi\\");

for(int i=250;i>=0;i--)
{
circle(200+i,150+i,50);
setfillstyle(SOLID_FILL,RED);
floodfill(200+i,150+i,getmaxcolor());
outtextxy(170+i,130+i,"Happy");
outtextxy(170+i,150+i,"DIWALI");
arc(200+i,225+i,75,200,25);
arc(170+i,259+i,300,75,25);

circle(300+i,250+i,50);
setfillstyle(SOLID_FILL,13);
floodfill(300+i,250+i,getmaxcolor());
outtextxy(270+i,230+i,"Happy");
outtextxy(270+i,250+i,"Birthday");
arc(300+i,325+i,75,230,25);
arc(270+i,364+i,300,70,25);
delay(35);
cleardevice();
}
closegraph();
restorecrtmode();
}





9.       Program to perform shearing transformation on a rectangle.


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<sdlib.h>
#include<math.h>
#include<dos.h>
void main()

{
          clrscr();
         
          int gdriver=DETECT,gmode, errcode, left, top, right, bottom;

          initgraph(&gdrive, &gmode, “ \\tc\\bgi”);
          errcode=grphresult();
          if(errcode!=0)
          {
          cout<<”Graphics error:- “;
          cout<<grapherrormsg(errcode);
          exit(0);
          }
          cout<<”\n Enter left value for rectangle :- “;
          cin>> left;
          cout<<”\n Enter top value for rectangle:- “;
          cin>>top;
          cout<<”\n Enter right value for rectangle:- “;
          cin>>right;
          cout<<”\n Enter bottom  value for rectangle:- “;
          cin>>bottom;
          cout<<”\n Press any key to see rectangle:- ……….“;
          getch();
          clrscr();
          clearviewport();
          char ch;


int s, lx, ly, i;
rectangle(left, top, right, bottom);
lx=right-left;
ly=bottom-top;
while(1)
{
          cout<<” You want to shear along x axis or about y axis(x or y)?: “;
          cin>>ch:
          if(ch!=’x’&& ch!=’X’&&  ch!’y’ && ch!=’Y’)
          {
                   cout<<”\n Wrong Choice, Press any key”;
                   getch();
          }
          else
                   break;
}
clearviewport();
cout<<”\n\n Enter the value by which it is  to be sheared: “;
fflush(stdin);
cin>>s;
cout<<”\n Press any key to see original and transformed rectangle”;
getch();
clrscr();
clearviewport();
rectangle(left, top, right, bottom);
delay(500);
          if(ch==’x’ || ch==’X’)
          for(i=0-;i<=s;i++)
                   {
                   delay(100);
                   clearviewport();
                   move(left+i,top,left+lx+i,top);
                   move(left+lx+i,top, left+lx, top+ly);
                   move(left+lx, top+ly, left, top+ly);
                   move(left,top+ly,left+i,top);
                   }



if(ch==’y’||ch++’Y’)
                   for(i=0;i<=s;i++)
                   {
                             delay(100);clearviewport();
                             move(left,top,left+lx,top+i);
                             move(left+lx,top+i,left+lx,top+ly+i);
                             move(left+lx,top+ly,left,top);
                            
                     }
getch();
clearviewport();
}
void move(int x1,int y1,int x2, int y2)
{
         
            float x,y,xx,yy,xinc,yinc,dx,dy,step;
            dx=x2-x1;
            dy=y2-y1;
            if(abs(dx)>abs(dy))
                             step=abs(dx);
            else
                             step=abs(dy);
            xinc=dx/step;
            xx=x1+0.5;
            yy=y1+0.5;
            for(x=x1,y=y1;(xx!=x2) || (yy!=y2);x+=yinc)
            {
                             putpixel(x,y,WHITE);
                             xx=(int)(x+0.5);
                             yy=(int)(y+0.5);
            }
}






         
10.  Program to make bar chart for a student’s result of 5 years.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
void axis();
void main()
{
int c,d,e,f,g;
int a=DETECT,b;
initgraph(&a,&b,"\\tc\\bgi\\");
cout<<"\n Enter the Marks of the First Year (In %):-";
cin>>c;
cout<<"\n Enter the Marks of the Second Year (In %):-";
cin>>d;
cout<<"\n Enter the Marks of the Third Year (In %):-";
cin>>e;
cout<<"\n Enter the Marks of the Fourth Year (In %):-";
cin>>f;
cout<<"\n Enter the Marks of the Fifth Year (In %):-";
cin>>g;
if ((c>100)||(d>100)||(e>100)||(f>100)||(g>100))
{
cout<<"\n\n\n\n\t\t Result can't be more than 100%";
getch();
exit(0);
}
axis();
bar3d(163,400,150,((100-c)*3+110),5,10); //1st year bar
bar3d(223,400,210,((100-d)*3+110),5,10); //2nd year bar
bar3d(283,400,270,((100-e)*3+110),5,10); //3rd year bar
bar3d(343,400,330,((100-f)*3+110),5,10);  //4th year bar
bar3d(403,400,390,((100-g)*3+110),5,10); //5th year bar
getch();
closegraph();
}


void axis()
{
cleardevice();
//y-axis
line(100,400,100,50);
//x-axis
line(100,400,500,400);
outtextxy(10,250,"Marks(%)");
outtextxy(170,430,"Students Result (In Year)");
//numbers on x-axis
line(160,405,160,395);
outtextxy(150,408,"1st");
line(220,405,220,395);
outtextxy(210,408,"2nd");
line(280,405,280,395);
outtextxy(270,408,"3rd");
line(340,405,340,395);
outtextxy(330,408,"4th");
line(400,405,400,395);
outtextxy(390,408,"5th");
//numbers on y-axis
line(95,370,105,370);
outtextxy(76,367,"10");
line(95,340,105,340);
outtextxy(76,337,"20");
line(95,310,105,310);
outtextxy(76,307,"30");
line(95,280,105,280);
outtextxy(76,277,"40");
line(95,250,105,250);
outtextxy(76,247,"50");
line(95,220,105,220);
outtextxy(76,217,"60");
line(95,190,105,190);
outtextxy(76,187,"70");
line(95,160,105,160);
outtextxy(76,157,"80");
line(95,130,105,130);
outtextxy(76,127,"90");
line(95,100,105,100);
outtextxy(70,97,"100");

}



































11.  Program to rotate a teddy bear in 3D.

#include<iostream.h>
#include<graphics.h>
#include<stdlib.h>
#include<conio.h>
#include<dos.h>

void teddy1(void);
void teddy2(void);
void teddy3(void);
void teddy4(void);
void teddy5(void);
void teddy6(void);

int main(void)
{
          int gdriver=DETECT,gmode,errorcode;
          initgraph(&gdriver,&gmode,”\\tc\\bgi”);
          errorcode = graphresult();
          if(errorcode != grOk)  // an error occurred   
          {
                   cout<<”Graphics error”<grapherrormsg(errrorcode)<<”\n”;
                  
                   getch(); exit(1);
          }
         
          while(!kbhit())
          {
                   if(!kbhit())
                   {
                             teddy1();
                             delay(700); clearviewport();
                   }
                   if(!kbhit())
                   {
                             teddy3();
         
         
                    delay(700); clearviewport();
                  
}

if(!kbhit())
{
    teddy4();
    delay(700);
    clearviewport();
}
if(!kbhit())
{
    teddy2();
    delay(700); clearviewport();
}
if(!kbhit())
{
    teddy5();
    delay(700);clearviewport();
}
}
getch();
closegraph();
return 0;

}


void teddy1()
{
          ellipse(300,100,0,360,70,70);
          ellipse(300,280,,0,360,100,110);
          circle(270,65,10);
          circle(330,65,10);
          circle(300,120,40);
          circle( 300,105,10);
          ellipse(300,110,230,310,30,30);


line(380,210,450,170);
line(400,250,460,220);
line(220,210,150,170);
line(200,250,140,220);
ellipse(455,195,280,100,25,25);
ellipse(145,195,80,260,25,25);
line(390,325,470,325);
line(365,370,460,370);
line(210,325,130,325);
line(235,370,140,370);
ellipse(465,348,260,70,23,23);
ellipse(136,348,110,280,23,23);
}

void teddy2()
{
ellipse(300,100,0,360,70,70);
ellipse(300,280,0,360,100,110);
line(380,210,450,170);
line(400,250,460,220);
line(220,210,150,170);
line(200,250,140,220);
ellipse(455,195,280,100,25,25);
ellipse(145,195,80,260,25,25);
line(390,325,470,325);
line(365,370,460,370);
line(210,325,130,325);
line(225,370,140,370);
ellipse(465,348,260,70,23,23);
ellipse(136,348,110,280,23,23);
}

void teddy3()
{
ellipse(300,100,0,360,60,70);
ellipse(300,280,0,360,90,110);
circle(280,65,10);

ellipse(260,120,270,130,20,30);
ellipse(260,105,0,360,5,10);
ellipse(260,210,250,290,30,30);
line(320,210,370,230);
line(320,250,270,90,20,20);
ellipse(370,250,270,90,20,20);
line(320,320,370,340);
line(320,360,370,380);
ellipse(370,360,270,90,20,20);
}

void teddy4()
{
ellipse(300,100,0,360,60,70);
ellipse(300,280,0,360,90,110);
line(280,210,230,230);
line(280,250,230,270);
ellipse(230,250,90,270,20,20);
line(280,320,230,340);
line(280,360,230,380);
ellipse(230,360,90,270,20,20);
}

void teddy5()
{
ellipse(300,100,0,360,60,70);
ellipse(300,280,0,360,90,110);
line(320,210,370,230);
line(320,250,370,270);
ellipse(370,250,270,90,20,20);
line(320,320,370,340);
line(320,360,370,380);
ellipse(370,360,270,90,20,20);
}






void teddy6()
{
ellipse(300,100,0,360,60,70);
ellipse(300,280,0,360,90,110);
circle(320,65,10);
ellipse(340,120,40,270,20,30);
ellipse(340,105,0,360,5,10);
ellipse(340,110,250,290,30,30);
line(280,210,230,230);
line(280,250,230,270);
ellipse(230,250,90,270,20,20);
line(280,320,230,340);
line(280,360,230,380);
ellipse(230,390,90,270,20,20);
}
























12.  Program to rotate a doll in 2D plane.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<stdlib.h>

void top();
void bottom();
void left();
void right();

void main()
{
          clrscr();
          int gdriver=DETECT,gmode,errcode;
          initgraph (&gdriver,gmode,”\\tc\\bgi”);

          if(errorcode != grOk)  // an error occurred   
          {
                   cout<<”Graphics error”<grapherrormsg(errrorcode)<<”\n”;
                  
                   getch(); exit(0);
          }
         
          int n,time=200;
          cout<<”Enter the number of time Your want rotate doll \n”;
          cin>> n;
         
          for(int i=0; i<n;i++)
                 {
                 top();
                 delay(time); clearviewport();
                 right();
                 delay(time); clearviewport();
                 bottom();
                 delay(time); clearviewport();
                 left();
                 delay(time);  clearviewport();
                 }
                 top();
                 getch();
                 closegraph();
}

void top()
{
                 setfillstyle(1,WHITE); setcolor(WHITE);
                 fillellipse(320,240,20,30);
                 line(320,260,320,400);
                 line(320,290,280,310);
                 line(320,290,360,310);
                 line(320,400,360,430);
                 line(320,400,280,430);
                 setfillstyle(1,BLACK);
                 fillellipse(310,225,3,5);
                 fillellipse(330,225,3,5);
                
                 setcolor(BLACK);
                 line(315,255,325,255);
                 line(320,235,320,245);
}

void right()
{
                 setfillstyle(1,WHITE); setcolor(WHITE);
                 fillellipse(320,240,30,20);
                 line(340,240,480,240);
                 line(370,240,400,200;
                 line(370,240,400,280);
                 line(480,240,510,200);
                 line(480,240,510,270);
                 setfillstyle(1,BLACK);


                 fillellipse(305,230,53);
                 fillellipse(305,250,5,3);
                 setfillstyle(BLACK);
                 line(315,240,325,240);
                 line(335,235,335,245);

}
void bottom()
{
                 setfillstyle(1,WHITE); setcolor(WHITE);
                 fillellipse(320,240,20,30);
                 line(320,220,320,80);
                 line(30,190,350,170);
                 line(320,190,290,170);
                 line(30,80,290,40);
                 line(320,80,350,40);
                 setfillstyle(1,BLACK);
                 fillellipse(310,225,3,5);
                 fillellipse(330,255,3,5);
                 setcolor(BLACK);
                 line(315,225,325,225);
                 line(320,235,320,245);
}
void left()
{
setfillstyle(1,WHITE); setcolor(WHITE);
fillellipse(320,240,30,20);
line(290,240,150,240,);
line(260,240,230,280);
line(260,240,230,200);
line(150,240,110,280);
line(150,240,110,200);
setfillstyle(1,BLACK);
fillellipse(335,230,5,3);
fillellipse(335,250,5,3);
setcolor(BLACK);
line(315,240,325,240);
line(305,235,305,245);
}

13.  Program to implement Cohen-Sutherland line clipping algorithm.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
float xmin,xmax,ymin,ymax;

class outcode
{
public:
          int top,bottom,right,left;
          void compcode(int x,int y)//computes the outcode for a particular point
          {
           if (y>ymax)
             top=1;
           else
             top=0;
           if (y<ymin)
             bottom=1;
           else
             bottom=0;
           if (x>xmax)
             right=1;
           else
             right=0;
           if (x<xmin)
             left=1;
           else
             left=0;
          }
    void show();
};

void outcode::show()
 {
  cout<<top<<bottom<<right<<left;
 }


int check1(outcode A,outcode B)   //checks for trivial acceptance
{
 if ((!A.right && !B.right) && (!A.left && !B.left) && (!A.top && !B.top) && (!A.bottom && !B.bottom)==1)
 return 1;
 else
 return 0;
}

int check2(outcode A,outcode B)   //checks for trivial acceptance
{
 if ((A.right && B.right) && (A.left && B.left) && (A.top && B.top) && (A.bottom && B.bottom)==1)
 return 1;
 else
 return 0;
}

outcode choose(outcode A,outcode B) //to choose the outer point
{
 if ((A.left==0)&&(A.right==0)&&(A.top==0)&&(A.bottom==0))
   return B;
 else
 return A;
}

int see(outcode A,outcode B)
{
 if ((A.left==B.left)&&(A.right==B.right)&&(A.top==B.top)&&(A.bottom==B.bottom))
    return 1;
 else
   return 0;
}

void main()
{
          clrscr();
          float x0,y0,x1,y1,accept=0,done=0;
          float x,y,m;
          int driver,a;
          driver=DETECT;
          initgraph(&driver,&a,"\\tc\\bgi\\");

          cout<<"\n Enter the diagonal coordinates of the rectangle:-";
          cout<<"\n Enter the left top coordinate, xmin , ymin:-";
          cin>>xmin>>ymin;
          cout<<"\n Enter the right bottom coordinate , xmax ,ymax:-";
          cin>>xmax>>ymax;
          cout<<"\n Enter the end coordinate of line:-";
          cin>>x0>>y0>>x1>>y1;
          m=(x1-x0)/(y1-y0);
          outcode outcode0,outcode1,outcodeout;
          outcode0.compcode(x0,y0);   // computing the outcode for the 1st end coordinate
          outcode1.compcode(x1,y1); //  -- do-- for 2nd coordinate

     while(done==0)
          {
           if (check1(outcode0,outcode1)) //check for trivial acceptance
               {
                    done=1;
                     accept=1;
               }
           if ((check2(outcode0,outcode1))) // check for trivial rejection
               {
                       done=1;
                     accept=0;
                    cout<<"\n\n The line has been trivially rejected";
               }
           if (done!=1)
              {
                    //failed both the tests , so calculate the line segment to clip
                    //atleast one end point is outside the clip rectangle so find it
                    outcodeout=choose(outcode0,outcode1);
                    //finding intersection points

                    if (outcodeout.top==1) //divide line at top of clip rectangle
                    {
                    x=x0+(ymax-y0)*m;
                    y=ymax;
                    }
                    if (outcodeout.bottom==1) //divide line at bottom edge of clip rectangle
                    {
                    x=x0+(ymin-y0)*m;
                    y=ymin;
                    }
                    if (outcodeout.right==1) //divide line at right edge of clip rectangle
                    {
                    y=y0+(xmax-x0)*m;
                    x=xmax;
                    }
                    if (outcodeout.top==1) //divide line at top of clip rectangle
                    {
                    y=y0+(xmin-x0)*m;
                    x=xmin;
                    }
                    // move from outside point to the intersection point
                    if (see(outcodeout,outcode0))
                    {
                    x0=x;
                    y0=y;
                    outcode0.compcode(x0,y0);
                    }
                    else
                    {
                    x1=x;
                    y1=y;
                    outcode1.compcode(x1,y1);
                    }
              }
       }//end of while loop



   if (accept==1)
    {
          cout<<"\n\n The intersection points of the line with the rectangle"<<"are: "<<x0<<","<<y0<<" and "<<x1<<","<<y1;
           setlinestyle(0,0,6);
           setcolor(BLUE);
           rectangle(xmin,ymin,xmax,ymax);
           setcolor(12);
           line(x0,y0,x1,y1);
    }
 getch();
 closegraph();
 }//end of main