Thursday, 25 September 2014

Program for implementation of Cohen –Sutherland Line clipping algorithm.

#include <conio.h>
#include <iostream.h>
#include <graphics.h>
typedefstruct coordinate
                {
                intx,y;
                char code[4];
                }PT;
voiddrawwindow();
voiddrawline (PT p1,PT p2,int cl);
PT setcode(PT p);
int visibility (PT p1,PT p2);
PT resetendpt (PT p1,PT p2);
main()
                {
                intgd=DETECT, gm,v;
                PT p1,p2,ptemp;
                initgraph(&gd,&gm,"C:\\TurboC3\\bgi");
                cleardevice();
                cout<<"\n\n\t\tENTER END-POINT 1 (x,y): ";
                cin>>p1.x>>p1.y;
                cout<<"\n\n\t\tENTER END-POINT 2 (x,y): ";
                cin>>p2.x>>p2.y;
                cleardevice();
                drawwindow();
                getch();
                drawline(p1,p2,15);
                getch();
                p1=setcode(p1);
                p2=setcode(p2);
                v=visibility(p1,p2);
                switch(v)
                                {
                                case 0:
                                                cleardevice();    /* Line conpletely visible */
                                                drawwindow();
                                                drawline(p1,p2,15);
                                                break;
                                case 1:
                                                cleardevice();    /* Line completely invisible */
                                                drawwindow();
                                                break;
                                case 2: cleardevice();      /* line partly visible */
                                                p1=resetendpt (p1,p2);
                                                p2=resetendpt(p2,p1);
                                                drawwindow();
                                                drawline(p1,p2,15);
                                                break;
                                }
                getch();
                closegraph();
                return(0);
                }
voiddrawwindow()
                {
                setcolor(RED);
                line(150,100,450,100);
                line(450,100,450,350);
                line(450,350,150,350);
                line(150,350,150,100);
                }
voiddrawline (PT p1,PT p2,int cl)
                {
                setcolor(cl);
                line(p1.x,p1.y,p2.x,p2.y);
                }
PT setcode(PT p)
                {
                PT ptemp;
                if(p.y<100)
                                ptemp.code[0]='1';
                else
                                ptemp.code[0]='0';
                if(p.y>350)
                                ptemp.code[1]='1';
                else
                                ptemp.code[1]='0';
                if (p.x>450)
                                ptemp.code[2]='1';
                else
                                ptemp.code[2]='0';
                if (p.x<150) /* LEFT */
                                ptemp.code[3]='1';
                else
                                ptemp.code[3]='0';
                ptemp.x=p.x;
                ptemp.y=p.y;
                return(ptemp);
                }
int visibility (PT p1,PT p2)
                {
                inti,flag=0;
                for(i=0;i<4;i++)
                                {
                                if((p1.code[i]!='0')||(p2.code[i]!='0'))
                                flag=1;
                                }
                if(flag==0)
                                return(0);
                for(i=0;i<4;i++)
                                {
                                if((p1.code[i]==p2.code[i]) &&(p1.code[i]=='1'))
                                flag=0;
                                }
                if(flag==0)
                                return(1);
                return(2);
                }
PT resetendpt (PT p1,PT p2)
                {
                PT temp;
                intx,y,i;
                floatm,k;
                if( p1.code[3]=='1')
                                x=150;
                if(p1.code[2]=='1')
                                x=450;
                if((p1.code[3]=='1')||(p1.code[2]=='1'))
                                {
                                m=(float) (p2.y-p1.y)/(p2.x-p1.x);
                                k=(p1.y+(m*(x-p1.x)));
                                temp.y=k;
                                temp.x=x;
                                for(i=0;i<4;i++)
                                                temp.code[i]=p1.code[i];
                                if(temp.y<=350&&temp.y>=100)
                                                return(temp);
                                }
                if(p1.code[0]=='1')
                                y=100;
                if(p1.code [1]=='1')
                                y=350;
                if((p1.code[0]=='1')||(p1.code[1]=='1'))
                                {
                                m=(float)(p2.y-p1.y)/(p2.x-p1.x);
                                k=(float)p1.x+(float)(y-p1.y)/m;
                                temp.x=k;
                                temp.y=y;
                                for(i=0;i<4;i++)
                                                temp.code[i]=p1.code[i];
                                return(temp);
                                }
                else
                                return(p1);

                }

Programs using 2­D transformations­ Translations, rotation, scaling.reflection, shear using homogeneous matrix representation.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#include<stdlib.h>
void menu();
void input();
void output();
void translation();
void rotation();
void scaling();
void shearing();
void reflection();
int gd,gm;
int a[10][2],i,x,option,temp,angle,tx,ty,fx,fy,sh,k,n,axis,y;
float sx,sy;
void menu()
{
printf("menu\n");
printf("1.Translation\n");
printf("2.rotation\n");
printf("3.scaling\n");
printf("4.shearing\n");
printf("5.reflection\n");
printf("6.exit\n");
printf("enter the choice:");
scanf("%d",&option);
switch(option)
                                                {
case  1:
input();
translation();
break;
case 2:
input();
rotation();
break;
case 3:
input();
scaling();
break;
case 4 :
input();
shearing();
break;
case 5:
input();
reflection();
break;
case 6:
exit(0);
break;
                                       }
}
void input()
{
printf("enter the number of vertices:" );
scanf("%d",&n);
for(i=0;i<n;i++)
                                                {
printf("enter the coordinates:");
scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i+1][0],&a[i+1][1]);
                                       }
}
void output()
{
cleardevice();
for(i=0;i<n;i++)
                                       {
                                                                   line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
                                                }
}
void translation()
{
output();
printf("enter the tranformation vertex tx,ty:\n");
scanf("%d%d",&tx,&ty);
for(i=0;i<=n;i++)
                                                {
a[i][0]=a[i][0]+tx;
a[i][1]=a[i][1]+ty;
                                                }
output();
delay(10);
menu();
}
void rotation()
{
output();
printf("enter the rotating angle:");
scanf("%d",&y);
printf("enter the pivot point:");
scanf("%d%d",&fx,&fy);
                                                k=(y*3.14)/180;
for(i=0;i<=n;i++)
                                                {
                                                                    a[i][0]=fx+(a[i][0]-fx)*cos(k)-(a[i][1]-fy)*sin(k);
                                                                    a[i][1]=fy+(a[i][0]-fx)*sin(k)-(a[i][1]-fy)*cos(k);
                                                }
output();
delay(10);
menu();
}
void scaling()
{
output();
printf("enter the scaling factor\n");
scanf("%f%f",&sx,&sy);
printf("enter the fixed point:");
scanf("%d%d",&fx,&fy);
for(i=0;i<=n;i++)
                                                {
a[i][0]=a[i][0]*sx+fy*(1-sx);
a[i][1]=a[i][1]*sy+fy*(1-sy);
                                                }
output();
delay(10);
menu();
}
void shearing()
{
output();
printf("enter the shear value:");
scanf("%d",&sh);
printf("enter the fixed point:");
scanf("%d%d",&fx,&fy);
printf("enter the axis for shearing if x-axis then 1 if y-axis the 0:");
scanf("%d",&axis);
for(i=0;i<=n;i++)
                                                {
if(axis==1)
                                                                    {
a[i][0]=a[i][0]+sh*(a[i][1]-fy);
                                                                    }
else
                                                                    {
a[i][1]=a[i][1]+sh*(a[i][0]-fx);
                                                                    }
                                                }
output();
delay(10);
menu();
}
void reflection()
{
output();
for(i=0;i<=n;i++)
                                                {
temp=a[i][0];
a[i][0]=a[i][1];
a[i][1]=temp;
                                                }
output();
delay(10);
menu();
}
void main()
{
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
menu();           getch();   

}



Program to implement curve generation using Bezeir curve algorithm.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int x,y,z;
void main()
{
float u;
int gd,gm,ymax,i,n,c[4][3];
for(i=0;i<4;i++)
{
c[i][0]=0;
c[i][1]=0;
}
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"e:\\tc\\bgi");
printf("\n\n Enter four points : \n\n");
for(i=0; i<4; i++)
{
printf("\t X%d Y%d : ",i,i);
scanf("%d %d",&c[i][0],&c[i][1]);
}
c[4][0]=c[0][0];
c[4][1]=c[0][1];
ymax = 480;
setcolor(13);
for(i=0;i<3;i++)
{
line(c[i][0],ymax-c[i][1],c[i+1][0],ymax-c[i+1][1]);
}
setcolor(3);
n=3;
for(i=0;i<=40;i++)
{
u=(float)i/40.0;
bezier(u,n,c);
if(i==0)
moveto(x,ymax-y);
else
{
lineto(x,ymax-y); }
getch();
}
getch();
}
bezier(u,n,p)
float u;int n; int p[4][3];
{
int j;
float v,b;
float blend(int,int,float);
x=0;y=0;z=0;
for(j=0;j<=n;j++)
{
b=blend(j,n,u);
x=x+(p[j][0]*b);
y=y+(p[j][1]*b);
z=z+(p[j][2]*b);
}
}
float blend(int j,int n,float u)
{
int k;
float v,blend;
v=C(n,j);
for(k=0;k<j;k++)
{ v*=u; }
for(k=1;k<=(n-j);k++)
{ v *= (1-u); }
blend=v;
return(blend);
}C(int n,int j)
{
int k,a,c;
a=1;
for(k=j+1;k<=n;k++) { a*=k; }
for(k=1;k<=(n-j);k++) { a=a/k; }
c=a;
return(c);
}







OUTPUT


Program for implementation of Flood fill & Boundary Fill polygon filling method.

# include <iostream.h>
 # include <graphics.h>
 # include    <conio.h>
 # include     <math.h>
 void Flood_fill(const int,const int,const int,const int);
 void Circle(const int,const int,const int);
 void Triangle(const int,const int,const int,const int,const int,const int);
 void Rectangle(const int,const int,const int,const int);
 void Polygon(const int,const int []);
 void Line(const int,const int,const int,const int);
 int main( )
    {
       int driver=VGA;
       int mode=VGAHI;
       initgraph(&driver,&mode,"..\\Bgi");
         setcolor(15);
                 Circle(175,175,40);
                 Flood_fill(175,175,10,0);
       setcolor(15);
       settextstyle(0,0,1);
                 outtextxy(150,225,"Circle");
       setcolor(15);
                 Rectangle(375,145,475,205);
                 Flood_fill(400,175,9,0);
       setcolor(15);
       settextstyle(0,0,1);
                 outtextxy(390,215,"Rectangle");
       setcolor(15);
                 Triangle(135,360,215,360,175,290);
                 Flood_fill(175,325,8,0);
       setcolor(15);
       settextstyle(0,0,1);
                 outtextxy(145,370,"Triangle");
       int polygon_points[14]={ 365,325, 400,290, 450,290, 485,325,450,360, 400,360, 365,325 };
      setcolor(15);
                 Polygon(7,polygon_points);

                 Flood_fill(425,325,12,0);
       setcolor(15);
       settextstyle(0,0,1);
                 outtextxy(395,370,"Polygon");
       getch( );
       return 0;
    }
 void Flood_fill(const int x,const int y,const int fill_color,const int old_color)
    {
       if(getpixel(x,y)==old_color)
                  {
                     putpixel(x,y,fill_color);
                     Flood_fill((x+1),y,fill_color,old_color);
                     Flood_fill((x-1),y,fill_color,old_color);
                     Flood_fill(x,(y+1),fill_color,old_color);
                     Flood_fill(x,(y-1),fill_color,old_color);
                  }
    }
 void Circle(const int h,const int k,const int r)
    {
       int color=getcolor( );
       int x=0;
       int y=r;
       int p=(1-r);
       do
                  {
                     putpixel((h+x),(k+y),color);
                     putpixel((h+y),(k+x),color);
                     putpixel((h+y),(k-x),color);
                     putpixel((h+x),(k-y),color);
                     putpixel((h-x),(k-y),color);
                     putpixel((h-y),(k-x),color);
                     putpixel((h-y),(k+x),color);
                     putpixel((h-x),(k+y),color);
                     x++;
                     if(p<0)
                                p+=((2*x)+1);
                     else
                                {
                                   y--;
                                   p+=((2*(x-y))+1);
                                }
                  }
       while(x<=y);
    }
 void Triangle(const int x_1,const int y_1,const int x_2,const int y_2,const int x_3,const int y_3)
    {
       Line(x_1,y_1,x_2,y_2);
       Line(x_2,y_2,x_3,y_3);
       Line(x_3,y_3,x_1,y_1);
    }
 void Rectangle(const int x_1,const int y_1,const int x_2,const int y_2)
    {
       Line(x_1,y_1,x_2,y_1);
       Line(x_2,y_1,x_2,y_2);
       Line(x_2,y_2,x_1,y_2);
       Line(x_1,y_2,x_1,y_1);
    }
 void Polygon(const int n,const int coordinates[])
    {
       if(n>=2)
                  {
                     Line(coordinates[0],coordinates[1],coordinates[2],coordinates[3]);
                     for(int count=1;count<(n-1);count++)
Line(coordinates[(count*2)],coordinates[((count*2)+1)],coordinates[((count+1)*2)], coordinates[(((count+1)*2)+1)]);
                  }
    }
 void Line(const int x_1,const int y_1,const int x_2,const int y_2)
    {
       int color=getcolor( );
       int x1=x_1;
       int y1=y_1;
       int x2=x_2;
       int y2=y_2;
       if(x_1>x_2)
                  {
                     x1=x_2;
                     y1=y_2;
                     x2=x_1;
                     y2=y_1;
                  }
       int dx=abs(x2-x1);
       int dy=abs(y2-y1);
       int inc_dec=((y2>=y1)?1:-1);
       if(dx>dy)
                  {
                     int two_dy=(2*dy);
                     int two_dy_dx=(2*(dy-dx));
                     int p=((2*dy)-dx);
                     int x=x1;
                     int y=y1;
                     putpixel(x,y,color);
                     while(x<x2)
                                {
                                   x++;
                                   if(p<0)
                                      p+=two_dy;
                                   else
                                      {
                                                 y+=inc_dec;
                                                 p+=two_dy_dx;
                                      }
                                   putpixel(x,y,color);
                                }
                  }
       else
                  {
                     int two_dx=(2*dx);
                     int two_dx_dy=(2*(dx-dy));
                     int p=((2*dx)-dy);
                     int x=x1;
                     int y=y1;
                     putpixel(x,y,color);
                     while(y!=y2)
                                {
                                   y+=inc_dec;
                                   if(p<0)
                                      p+=two_dx;
                                   else
                                      {
                                                 x++;
                                                 p+=two_dx_dy;
                                      }
                                   putpixel(x,y,color);
                                }
                  }
    }






OUTPUT




# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>
void show_screen( );
void Boundary_fill(const int,const int,const int,const int);
void Circle(const int,const int,const int);
void Triangle(const int,const int,const int,const int,const int,const int);
void Rectangle(const int,const int,const int,const int);
void Polygon(const int,const int []);
void Line(const int,const int,const int,const int);
int main( )
 {
 int driver=VGA;
 int mode=VGAHI;
 initgraph(&driver,&mode,"..\\Bgi");
 show_screen( );
 setcolor(15);
Circle(175,175,40);
Boundary_fill(175,175,10,15);
 setcolor(15);
 settextstyle(0,0,1);
outtextxy(150,225,"Circle");
 setcolor(15);
Rectangle(375,145,475,205);
Boundary_fill(400,175,9,15);
 setcolor(15);
 settextstyle(0,0,1);
outtextxy(390,215,"Rectangle");
setcolor(15);
Triangle(135,360,215,360,175,290);
Boundary_fill(175,325,8,15);
 setcolor(15);
 settextstyle(0,0,1);
outtextxy(145,370,"Triangle");
 int polygon_points[14]={ 365,325, 400,290, 450,290, 485,32,
450,360, 400,360, 365,325 }; setcolor(15);
Polygon(7,polygon_points);
Boundary_fill(425,325,12,15);
 setcolor(15);
 settextstyle(0,0,1);
outtextxy(395,370,"Polygon");
 getch( );
 return 0;
 }
void Boundary_fill(const int x,const int y,
const int fill_color,const int boundary_color)
 {
 if(getpixel(x,y)!=boundary_color && getpixel(x,y)!=fill_color)
 {
 putpixel(x,y,fill_color);
 Boundary_fill((x+1),y,fill_color,boundary_color);
 Boundary_fill((x-1),y,fill_color,boundary_color);
 Boundary_fill(x,(y+1),fill_color,boundary_color);
 Boundary_fill(x,(y-1),fill_color,boundary_color);
 }
 }
void Circle(const int h,const int k,const int r)
 {
 int color=getcolor( );
 int x=0;
 int y=r;
 int p=(1-r);
 do
 {
 putpixel((h+x),(k+y),color);
 putpixel((h+y),(k+x),color);
 putpixel((h+y),(k-x),color);
 putpixel((h+x),(k-y),color);
 putpixel((h-x),(k-y),color);
 putpixel((h-y),(k-x),color);
 putpixel((h-y),(k+x),color);
 putpixel((h-x),(k+y),color);
 x++;
if(p<0)
p+=((2*x)+1);
 else
{
 y--;
 p+=((2*(x-y))+1);
}
 }
 while(x<=y);
 }
void Triangle(const int x_1,const int y_1,const int x_2,const int y_2,const int x_3,const int y_3)
 {
 Line(x_1,y_1,x_2,y_2);
 Line(x_2,y_2,x_3,y_3);
 Line(x_3,y_3,x_1,y_1);
 }
void Rectangle(const int x_1,const int y_1,const int x_2,const int y_2)
 {
 Line(x_1,y_1,x_2,y_1);
 Line(x_2,y_1,x_2,y_2);
 Line(x_2,y_2,x_1,y_2);
 Line(x_1,y_2,x_1,y_1);
 }
void Polygon(const int n,const int coordinates[])
 {
 if(n>=2)
 {
 Line(coordinates[0],coordinates[1],
 coordinates[2],coordinates[3]);
 for(int count=1;count<(n-1);count++)
Line(coordinates[(count*2)],coordinates[((count*2)+1)],
coordinates[((count+1)*2)],
 coordinates[(((count+1)*2)+1)]);
 }
 }
void Line(const int x_1,const int y_1,const int x_2,const int y_2)
 {
 int color=getcolor( );
 int x1=x_1; int y1=y_1;
 int x2=x_2;
 int y2=y_2;
 if(x_1>x_2)
 {
 x1=x_2;
 y1=y_2;
 x2=x_1;
 y2=y_1;
 }
 int dx=abs(x2-x1);
 int dy=abs(y2-y1);
 int inc_dec=((y2>=y1)?1:-1);
 if(dx>dy)
 {
 int two_dy=(2*dy);
 int two_dy_dx=(2*(dy-dx));
 int p=((2*dy)-dx);
 int x=x1;
 int y=y1;
 putpixel(x,y,color);
 while(x<x2)
{
 x++;
 if(p<0)
 p+=two_dy;
 else
 {
y+=inc_dec;
p+=two_dy_dx;
 }
 putpixel(x,y,color);
}
 }
 else
 {
 int two_dx=(2*dx);
 int two_dx_dy=(2*(dx-dy));
 int p=((2*dx)-dy);
 int x=x1; int y=y1;
 putpixel(x,y,color);
 while(y!=y2)
{
 y+=inc_dec;
 if(p<0)
 p+=two_dx;
 else
 {
x++;
p+=two_dx_dy;
 }
 putpixel(x,y,color);
}
 }
 }
void show_screen( )
 {
 setfillstyle(1,1);
bar(220,26,420,38);
 settextstyle(0,0,1);
setcolor(15);
outtextxy(5,5,"*****************************************************************
*************");
outtextxy(5,17,"****************************************************************
***********-*");
outtextxy(5,29,"*-------------------------------------------------*");
outtextxy(5,53,"****************************************************************
***********-*");
setcolor(11);
 outtextxy(228,29,"Boundary Fill Algorithm");
setcolor(15);
 for(int count=0;count<=30;count++)
 outtextxy(5,(65+(count*12)),"*-* *-*");
outtextxy(5,438,"***************************************************************
************-*");outtextxy(5,450,"*--------------------------------------------------*");
outtextxy(5,462,"***************************************************************
***************");
setcolor(12);
 outtextxy(229,450,"Press any Key to exit.");
 }










OUTPUT



Program for Concentric Circles Generation using midpoint algorithm

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void draw_circle(int,int,int);
void symmetry(int,int,int,int);
void main(){
int xc,yc,R;
int driver,mode;
driver = DETECT;
initgraph(&driver,&mode,"c:\\tc\\bgi");
printf("Enter the center of the circle:\n");
printf("Xc =");
scanf("%d",&xc);
printf("Yc =");
scanf("%d",&yc);
printf("Enter the radius of the circle :");
scanf("%d",&R);
draw_circle(xc,yc,R);
getch();
closegraph();
}
void draw_circle(int xc,int yc,int rad){
int x = 0;
int y = rad;
int p = 1-rad;
symmetry(x,y,xc,yc);
for(x= 0;y>x;x++) {
 if(p<0)
 p += 2*x + 3;
 else {
 p += 2*(x-y) + 5;
 y--;
 }
 symmetry(x,y,xc,yc);
 }
}
void symmetry(int x,int y,int xc,int yc){
putpixel(xc+x,yc-y,EGA_WHITE);
putpixel(xc+y,yc-x,EGA_WHITE);
putpixel(xc+y,yc+x,EGA_WHITE);
putpixel(xc+x,yc+y,EGA_WHITE);
putpixel(xc-x,yc+y,EGA_WHITE);
putpixel(xc-y,yc+x,EGA_WHITE);
putpixel(xc-y,yc-x,EGA_WHITE);
putpixel(xc-x,yc-y,EGA_WHITE);
}












OUTPUT

Program for Line Drawing using Bresenham’s algorithm

# include <iostream.h>
# include <conio.h>
# include <graphics.h>
# include <math.h>
char IncFlag;
void Bresenham(int x1,int x2,int y1,int y2);
void DrawLine(int X,int Y,int End,int PInc,int NInc,int P,int XInc,int YInc);
void main()
{
int gDriver=DETECT, gMode;
int x1,x2,y1,y2;
void Bresenham(int,int,int,int);
initgraph(&gDriver,&gMode,"c:\\tc\\bgi");
cout<<endl<<"x1 : ";
cin>>x1;
cout<<endl<<"y1 : ";
cin>>y1;
cout<<endl<<"x2 : ";
cin>>x2;
cout<<endl<<"y2 : ";
cin>>y2;
line(320,0,320,480);
line(0,240,640,240);
Bresenham(x1,x2,y1,y2);
getch();
}
void Bresenham(int x1,int x2,int y1,int y2)
{
int S,O,End;
int P;
int dx = abs(x1 - x2);
int dy = abs(y1 - y2);
float Slope;
int PInc,NInc,XInc,YInc;
if (dx == 0) //Slope Infinite
{
}
else
{
Slope = (float)(y1 - y2) / (x1 - x2);
if (Slope>-1 && Slope<1)
{
IncFlag = 'X';
PInc = 2 * (dy - dx);
NInc = 2 * dy;
P = 2 * dy - dx;
if (x1>x2)
{
S = x2;
O = y2;
End = x1;
}
else
{
S = x1;
O = y1;
End = x2;
}
// DrawLine(x,y,End,PInc,NInc,P,XInc,YInc);
}
else
{
IncFlag = 'Y';
PInc = 2 * (dx - dy);
NInc = 2 * dx;
P = 2 * dx - dy;
if (y1 > y2)
{
O = x2;
S = y2;
End = y1;
}
else
{
O = x1;
S = y1;
End = y2;
}
}
if (IncFlag == 'X')
putpixel(320+S,240-O,12);
else
putpixel(320+O,240-S,12);
while (S <= End)
{
S++;
if (P<0)
P = P + NInc;
else
{
P = P + PInc;
if (Slope>0.0)
O++;
else
O--;
}
if (IncFlag == 'X')
putpixel(320+S,240-O,12);
else
putpixel(320+O,240-S,12);
}
}
}












OUTPUT