Friday, 4 May 2018

Computer Graphics Practicals and Programs(CGM)

 
Computer Graphics Practicals and Programs(CGM)
 
AIM - Write a program of line drawing using Digital Differential Analyzer   

(DDA) algorithm.                
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
          clrscr();
          int x1,y1,x2,y2,dx,dy,step,i;
          int gd=DETECT,gm;
          float xinc,yinc,x,y;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cout<< "Enter the coordinates : \n\n";
          cout<<"\n(x1,y1) : ";
          cin>>x1>>y1;
          cout<<"\n(x2,y2) : ";
          cin>>x2>>y2;
dx=x2-x1;
          dy=y2-y1;
if(abs(dx)>abs(dy))
          {        step=abs(dx);
                    xinc=dx/step;
                    yinc=(float)dy/step;
          }
          else
          {
                    step=abs(dy);
                    xinc=(float)dx/step;
                    yinc=dy/step;
          }
          x=x1;
          y=y1;
for(i=0;i<=step;i++)
          {        putpixel(x1,y1,2);
                    x=x+xinc;
                    y=y+yinc;
                    x1=(int)(x+0.5);
                    y1=(int)(y+0.5);
          }
          getch();
          closegraph();
}
AIM – Write a program of line drawing using Bresenhems algorithm.

#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
          clrscr();
          int x1,y1,x2,y2,dx,dy,step,pk,x,y,i;
          int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cout<< "Enter the coordinates :\n\n\n ";
          cout<<"\n(x1,y1) : ";
          cin>>x1>>y1;
          cout<<"\n(x2,y2) : ";
          cin>>x2>>y2;
if(x1>x2)
          {
                    x1=x1+x2;
                    x2=x1-x2;
                    x1=x1-x2;
                    y1=y1+y2;
                    y2=y1-y2;
                    y1=y1-y2;
          }
dx=x2-x1;
          dy=y2-y1;
          if(abs(dx)>abs(dy))
          {
                    step=abs(dx);
          }
          else
          {
                    step=abs(dy);
          }
x=x1;
          y=y1;
if(dy>=0)
          {
                    if(abs(dx)>abs(dy))
                    {
                              cout<<"\n o<=m<1";
                              pk=2*dy-dx;
                              for(i=0;i<=step;i++)
                              {
                                        putpixel(x,y,1);
                                        if(pk<0)
                                        {
                                                  x=x+1;
                                                  pk=pk+2*dy;
                                        }
                                        else
                                        {        x=x+1;
                                                  y=y+1;
                                                  pk=pk+2*dy-2*dx;
                                        }
                              }
                    }
                    else
                    {
                              cout<<"\n m>=1";
                              pk=2*dx-dy;
                              for(i=0;i<=step;i++)
                              {
                                        putpixel(x,y,2);
                                        if(pk<0)
                                        {
                                                  y=y+1;
                                                  pk=pk+2*dx;
                                        }
                                        else
                                        {        x=x+1;
                                                  y=y+1;
                                                  pk=pk+2*dx-2*dy;
                                        }
                              }
                    }
          }
         
else
          {
                    if(abs(dx)>abs(dy))
                    {
                              cout<<"\n -1<m<0";
                              pk=-2*dy-dx;
                              for(i=0;i<=step;i++)
                              {
                                        putpixel(x,y,3);
                                        if(pk<0)
                                        {        x=x+1;
                                                  pk=pk-2*dy;
                                        }
                                        else
                                        {        x=x+1;
                                                  y=y-1;
                                                  pk=pk-2*dy-2*dx;
                                        }
                              }
                    }
                    else
                    {
                              cout<<"\n m<=-1";
                              pk=2*dx+dy;
                              for(i=0;i<=step;i++)
                              {
                                        putpixel(x,y,4);
                                        if(pk<0)
                                        {        y=y-1;
                                                  pk=pk+2*dx;
                                        }
                                        else
                                        {        x=x+1;
                                                  y=y-1;
                                                  pk=pk+2*dx+2*dy;
                                        }
                              }
                    }
          }
          getch();
          closegraph();
}
AIM – Write a program to draw a circle using Mid Point algorithm.
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
          clrscr();
          int x,y,r,pk,x1,y1;
          int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
line(1,240,640,240);
          line(320,1,320,480);
cout<<"Enter the radius \n\n"<<"r = ";
          cin>>r;
          cout<<"\n\nEnter the coordinates of the center \n\n"<<"(x1,y1) : ";
          cin>>x1>>y1;
pk=1-r;
x=0;
          y=r;
          while(x<=y)
          {        putpixel(320+x1+x,240-y1-y,2);
                    putpixel(320+x1+y,240-y1-x,2);
                    putpixel(320+x1-x,240-y1+y,2);
                    putpixel(320+x1-y,240-y1+x,2);
                    putpixel(320+x1+x,240-y1+y,2);
                    putpixel(320+x1+y,240-y1+x,2);
                    putpixel(320+x1-x,240-y1-y,2);
                    putpixel(320+x1-y,240-y1-x,2);
                    if(pk<0)
                    {        x=x+1;
                              pk=pk+2*x+1;
                    }
                    else
                    {
                              x=x+1;
                              y=y-1;
                              pk=pk+2*x-2*y+1;
                    }
          }
          getch();
          closegraph();
}
AIM – Write a program to draw a circle using Bresenhems algorithm.
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
          clrscr();
          int x,y,r,pk,x1,y1;
          int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
line(1,240,640,240);
          line(320,1,320,480);
cout<<"Enter the radius \n\n"<<"r = ";
          cin>>r;
          cout<<"\n\nEnter the coordinates of the center \n\n"<<"(x1,y1) : ";
          cin>>x1>>y1;
pk=3-2*r;
x=0;
          y=r;
          while(x<=y)
          {
                    putpixel(320+x1+x,240-y1-y,2);
                    putpixel(320+x1+y,240-y1-x,2);
                    putpixel(320+x1-x,240-y1+y,2);
                    putpixel(320+x1-y,240-y1+x,2);
                    putpixel(320+x1+x,240-y1+y,2);
                    putpixel(320+x1+y,240-y1+x,2);
                    putpixel(320+x1-x,240-y1-y,2);
                    putpixel(320+x1-y,240-y1-x,2);
                    if(pk<0)
                    {
                              x=x+1;
                              pk=pk+4*x+6;
                    }
                    else
                    {
                              pk=pk+4*x-4*y+10;
                              x=x+1;
                              y=y-1;
                    }
          }
          getch();
          closegraph();
}
AIM – Write a program for the translations of an object (Rectangle).
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
          clrscr();
          int x1,x2,y1,y2,x,y;
          int gd=DETECT,gm;
          initgraph(&gd,&gm,"c:\\tc\\bgi");
          cout<<"Enter the value of bottom left coordinates\n (x1,y1) : ";
          cin>>x1>>y1;
          cout<<"\nEnter the value of top right coordinates\n (x2,y2) : ";
          cin>>x2>>y2;
          cleardevice();
          rectangle(x1,y1,x2,y2);
          cout<<"\nEnter the value of translation coordinates\n (x,y) : ";
          cin>>x>>y;
          cout<<" \npress any key to translate";
          getch();
          cleardevice();
          rectangle(x1+x,y1+y,x2+x,y2+y);
          getch();
          closegraph();
}
AIM – Write a program to rotate a line to a given angle.
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
          clrscr();
          float x1,y1,x2,y2,x,y;
          float a,p,q,r,s;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cout<<"Enter the coordinates of a line\n A(x1,y1),B(x2,y2) :";
          cin>>x1>>y1>>x2>>y2;
line(x1,y1,x2,y2);
cout<<"Enter the point to be fixed (x,y), it should be first or last point of
                      line\n”;       
          cout<<"(x,y) :";
          cin>>x>>y;
          cout<<"\nEnter the angle of rotation";
          cin>>a;
x1-=x;          x2-=x;
          y1-=y;          y2-=y;
a=a*(3.14/180);
          p=(x1*cos(a))-(y1*sin(a));
          q=(x1*sin(a))+(y1*cos(a));
          r=(x2*cos(a))-(y2*sin(a));
          s=(x2*sin(a))+(y2*cos(a));
x1=p; y1=q; x2=r;  y2=s;
x1+=x;         x2+=x;
          y1+=y;         y2+=y;
cout<<"\npress any key to rotate";
          getch();
line(x1,y1,x2,y2);
          getch();
          closegraph();
}
 AIM – Write a program to scale a triangle with its one vertex fixed.
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
          clrscr();
          int x1,y1,x2,y2,x3,y3,x,y,a,b;
          int gd=DETECT,gm;
          initgraph(&gd,&gm,"c:\\tc\\bgi");
          cout<<"Enter the coordinates of a triangle :-";
          cout<<"\np(x1,y1) :";
          cin>>x1>>y1;
          cout<<"\nq(x2,y2) :";
          cin>>x2>>y2;
          cout<<"\nr(x3,y3) :";
          cin>>x3>>y3;
          line(x1,y1,x2,y2);
          moveto(x2,y2);
          lineto(x3,y3);
          moveto(x3,y3);
          lineto(x1,y1);
          cout<<"Enter the coordinates of a point to be fixed,it should be either p,q
                      or r\n(x,y) :";
          cin>>x>>y;
          x1-=x;          x2-=x;          x3-=x;
          y1-=y;          y2-=y;          y3-=y;
cout<<"\nEnter the scaling points\n (a,b) :";
          cin>>a>>b;
x1*=a;         x2*=a;         x3*=a;
          y1*=b;         y2*=b;         y3*=b;
          x1+=x;         x2+=x;         x3+=x;
          y1+=y;         y2+=y;         y3+=y;
          setcolor(10);
line(x1,y1,x2,y2);
          moveto(x2,y2);
          lineto(x3,y3);
          moveto(x3,y3);
          lineto(x1,y1);
getch();
          closegraph();
}
AIM – Write a program to reflect a triangle with respect to a  

 line(y=mx+c).
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
          clrscr();
          int x1,x2,x3,y1,y2,y3;
          float a1,a2,b1,b2,m,c,m1,m2,m3,m4,x,xx,xxx,y,yy,yyy;
          int gd=DETECT,gm;
          initgraph(&gd,&gm,"c:\\tc\\bgi");
          line(1,240,640,240);
          line(320,1,320,480);
          cout<<"Enter the coordinates of a line ";
          cout<<"\nA(a1,a2) :";
          cin>>a1>>b1;
          cout<<"\nB(b1,b2) :";
          cin>>a2>>b2;
          line(320+a1,240-b1,320+a2,240-b2);
          cout<<"Enter the coordinates of a triangle";
          cout<<"\np(x1,y1) :";
          cin>>x1>>y1;
          cout<<"\nq(x2,y2) :";
          cin>>x2>>y2;
          cout<<"\nr(x3,y3) :";
          cin>>x3>>y3;
          line(320+x1,240-y1,320+x2,240-y2);
          moveto(320+x2,240-y2);
          lineto(320+x3,240-y3);
          moveto(320+x3,240-y3);
          lineto(320+x1,240-y1);
          cout<<"\npress any key to reflect\n";
          getch();
          m=(b2-b1)/(a2-a1);
          c=b1-(m*a1);
          m1=(1-(m*m))/(1+(m*m));
          m2=(2*m)/(1+(m*m));
          m3=(-2*c*m)/(1+(m*m));
          m4=(2*c)/(1+(m*m));
          x=(m1*x1)+(m2*y1)+m3;
          xx=(m1*x2)+(m2*y2)+m3;
          xxx=(m1*x3)+(m2*y3)+m3;
          y= (m2*x1)-(m1*y1)+m4;
          yy= (m2*x2)-(m1*y2)+m4;
          yyy= (m2*x3)-(m1*y3)+m4;
          x1=x; x2=xx;          x3=xxx;
          y1=y; y2=yy;          y3=yyy;
          setcolor(9);
          line(320+x1,240-y1,320+x2,240-y2);
          moveto(320+x2,240-y2);
          lineto(320+x3,240-y3);
          moveto(320+x3,240-y3);
          lineto(320+x1,240-y1);
          getch();
          closegraph();
}
AIM – Write a program to implement Boundary Fill Algorithm.
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void fill(int p1,int q1,int n1,int m1)
{
   int c;
   c=getpixel(p1,q1);
   if(c!=m1&&c!=n1)
   {
                     putpixel(p1,q1,n1);
                     fill(p1+1, q1, n1, m1);
                     fill(p1, q1+1, n1, m1);
                     fill(p1-1, q1, n1, m1);
                     fill(p1, q1-1, n1, m1);
   }
}
void main()
{
          clrscr();
          int x,y,r,p,q,n,m;
          int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cout<<" Enter the midpoint of the circle(x,y) :";
          cin>>x>>y;
          cout<<"Enter the radius of the circle : ";
          cin>>r;
          cout<<"Enter the colour of the circle :";
          cin>>m;
          setcolor(m);
          circle(x,y,r);
cout<<"Enter any arbitary point inside the circle : ";
          cin>>p>>q;
          cout<<"Enter the fill colour :";
          cin>>n;
fill(p,q,n,m);
getch();
          closegraph();
}
AIM - Write a program to implement Flood Fill Algorithm.
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void fill(int p1,int q1,int n1,int c1)
{
   if(getpixel(p1,q1)==c1)
   {    
putpixel(p1,q1,n1);
          fill(p1+1, q1, n1, c1);
          fill(p1, q1+1, n1, c1);
          fill(p1-1, q1, n1, c1);
          fill(p1, q1-1, n1, c1);
   }
}
void main()
{        clrscr();
          int p,q,n,c,x1,x2,x3,y1,y2,y3;;
          int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cout<<"Enter the coordinates of a triangle";
          cout<<"\np(x1,y1) :";
          cin>>x1>>y1;
          cout<<"\nq(x2,y2) :";
          cin>>x2>>y2;
          cout<<"\nr(x3,y3) :";
          cin>>x3>>y3;
cleardevice();
setcolor(1);
          line(x1,y1,x2,y2);
          setcolor(2);
          line(x2,y2,x3,y3);
          setcolor(3);
          line(x3,y3,x1,y1);
cout<<"Enter any  point (p,q) which is inside the triangle  : ";
          cin>>p>>q;
c=getpixel(p,q);
cout<<"\nEnter the fill colour n:";
          cin>>n;
          fill(p,q,n,c);
          getch();
          closegraph();
}


EmoticonEmoticon

Ads

Recent Posts Widget