Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 25

1.

IMPLEMENTATION OF COHEN SUTHERLAND ALGORITHM FOR LINE CLIPPING

#include<stdio.h>

#include<graphics.h>

typedef unsigned int outcode;

enum { TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 };

void lineclip(x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax )

float x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax;

outcode code0,code1,codeout;

int accept = 0, done=0;

code0 = calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);

code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);

do{

if(!(code0 | code1))

{ accept =1 ; done =1; }

else

if(code0 & code1) done = 1;

else

float x,y;

codeout = code0?code0:code1;

if(codeout & TOP)

x = x0 + (x1-x0)*(ywmax-y0)/(y1-y0);

y = ywmax;
}

else

if( codeout & BOTTOM)

x = x0 + (x1-x0)*(ywmin-y0)/(y1-y0);

y = ywmin;

else

if ( codeout & RIGHT)

y = y0+(y1-y0)*(xwmax-x0)/(x1-x0);

x = xwmax;

else

y = y0 + (y1-y0)*(xwmin-x0)/(x1-x0);

x = xwmin;

if( codeout == code0)

x0 = x; y0 = y;

code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);

else

{
x1 = x; y1 = y;

code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);

} while( done == 0);

if(accept) line(x0,y0,x1,y1);

rectangle(xwmin,ywmin,xwmax,ywmax);

getch();

int calcode (x,y,xwmin,ywmin,xwmax,ywmax)

float x,y,xwmin,ywmin,xwmax,ywmax;

int code =0;

if(y> ywmax)

code |=TOP;

else if( y< xwmax)

code |= RIGHT;

else if ( x< xwmin)

code |= LEFT;

return(code);

main()

float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;
int gd,gm;

detectgraph(&gd,&gm);

initgraph(&gd,&gm,"C:\\TC\\BGI");

printf("\n\n\tEnter the co-ordinates of Line :");

printf("\n\n\tX1 Y1 : ");

scanf("%f %f",&x1,&y1);

printf("\n\n\tX2 Y2 : ");

scanf("%f %f",&x2,&y2);

printf("\n\tEnter the co_ordinates of window :\n ");

printf("\n\txwmin , ywmin : ");

scanf("%f %f",&xwmin,&ywmin);

printf("\n\txwmax , ywmax : ");

scanf("%f %f",&xwmax,&ywmax);

line(x1,y1,x2,y2);

rectangle(xwmin,ywmin,xwmax,ywmax);

getch();

cleardevice();

lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax );

getch();

closegraph();

}
                                                                                

                                                                                

                                                                                
2. LINE DRAWING USING BRESENHAM’S LINE ALGORITHM

 #include<iostream.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

void main()

clrscr();

int gd = DETECT,gm;

initgraph(&gd,&gm,"c:\\tc\\bgi");

float xa,ya,xb,yb,dy,dx,x,y,xend,p;

cout<<"enter the starting and ending coordinates:";

cin>>xa>>ya>>xb>>yb;

dx= abs(xa-xb);

dy = abs(ya-yb);

p = 2*dy-dx;

if(xa>xb)

x=xb;

y = yb;

xend = xa;

else

x = xa;
y = ya;

xend = xb;

putpixel(x,y,RED);

while(x<xend)

x++;

if(p<0)

p=p+(2*dy);

else

y++;

p = p+(2*(dy-dx));

putpixel(x,y,RED);

getch();

closegraph();

}                                                               

                                                                                

                                                                                

                                                                                

                                                                                

                                                                                

                                                                                
                                                                                

                                                                                
3. CIRCLE DRAWING USING MID POINT ALGORITHM

#include<iostream.h>

#include<conio.h>

#include<graphics.h>

#include<stdio.h>

void drawcircle(int x,int y,int dx,int dy)

putpixel(x+dx,y+dy,RED);

putpixel(x+dx,y-dy,BLUE);

putpixel(x-dx,y+dy,WHITE);

putpixel(x-dx,y-dy,GREEN);

putpixel(x+dy,y+dx,RED);

putpixel(x+dy,y-dx,BLUE);

putpixel(x-dy,y+dx,WHITE);

putpixel(x-dy,y-dx,GREEN);

void main()

int gd =DETECT,gm;

initgraph(&gd,&gm,"c:\\tc\\bgi");

int x,y,r,p,dx,dy;

cout<<"enter the center coordinate and radius of the circle:";

cin>>x>>y>>r;

p =1-r;

dx = 0;
dy = r;

drawcircle(x,y,dx,dy);

while(dx<dy)

dx++;

if(p<0)

p =p+2*dx+3;

else

dy--;

p = p+2*(dx-dy)+5;

drawcircle(x,y,dx,dy);

getch();

closegraph();

}
                                                                                

                                                                                

                                                                                

                                                                                

                                                                                

                                                                                

                                                                                

                                                                                

                                                                                

                                                                                

                                                                                

                                                                                
4. 2-d TRANSFORMATIONS

#include <iostream.h>
#include<graphics.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>
int *x,*y,i,sides;
float x1,y1,theta;
//function to draw polygon
void drawpolygon(int *x,int *y)
{
int gd = DETECT , gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
for(i=0;i<(sides-1);i++)
line(x[i],y[i],x[i+1],y[i+1]);
line(*(x+(sides-1)),*(y+(sides-1)),*x,*y);
getch();
closegraph();
}
//translation function
void translate(float x1,float y1)
{
for(i=0;i<sides;i++)
{
*(x+i)+=x1;
*(y+i)+=y1;
}
}
//shearing function
void shear(float x1,float y1)
{
int z,a,b;
cout<<"which type of shearing u would like to do:"<<endl;
cout<<"enter 1 for shearing in x direction :"<<endl<<"2 for y direction shearing :"<<endl<<"3 for
both type shearing:"<<endl;
cin>>z;
switch(z)
{
case 1:
for(i=0;i<sides;i++)
{
*(x+i) = *(x+i)+x1*(*(y+i));
*(y+i) = *(y+i);
}
break;
case 2:
for(i=0;i<sides;i++)
{
*(x+i) = *(x+i);
*(y+i) = y1*(*(x+i))+ *(y+i);
}
break;
case 3:
for(i=0;i<sides;i++)
{
a = *(x+i);
b=*(y+i);
a = *(x+i)+x1*(*(y+i));
b = y1*(*(x+i))+ *(y+i);
*(x+i) = a;
*(y+i) = b;
}
break;
default:
exit(0);
}
}
//scaling function
void scale(float x1,float y1)
{
int a,b;
a = *x;
b = *y;
translate(-a,-b);
for(i=0;i<sides;i++)
{
*(x+i) *=x1;
*(y+i) *=y1;
}
translate(a,b);
}
//rotation function
void rotate(float theta)
{
int a,b,c,d;
c = *x;
d = *y;
translate(-c,-d);
for(i=0;i<sides;i++)
{

a = (*(x+i)*cos(theta))-(*(y+i)*sin(theta));
b = (*(x+i)*sin(theta))+(*(y+i)*cos(theta));
*(x+i) = a;
*(y+i) = b;
}
translate(c,d);
}
//main function
void main()
{
clrscr();
x = new int[10];
y = new int[10];
cout<<"enter the number of sides in polygon:";
cin>>sides;
cout<<endl;
cout<<"enter the coordinates of the vertices(x,y)"<<endl;
for(i=0;i<sides;i++)
{
cout<<i+1<<"point:";
cin>>*(x+i)>>*(y+i);
cout<<endl;
}
drawpolygon(x,y);
int ch;
while(ch!=5)
{
cout<<"enter ur option:"<<endl;
cout<<"1 for translation:"<<endl<<"2 for rotation"<<endl<<"3 for scaling"<<endl<< "4 for
shearing"<<endl<<"5 for exit"<<endl;
cout<<"ur choice";
cin>>ch;
cout<<endl;
clrscr();
switch(ch)
{
case 1:
cout<<"translation in x and y direction:";
cin>>x1>>y1;
translate(x1,y1);
drawpolygon(x,y);
break;
case 3:
cout<<"scaling in x and y direction:";
cin>>x1>>y1;
translate(x1,y1);
scale(x1,y1);
drawpolygon(x,y);
break;
case 2:
cout<<"angle of rotation (in anticlockwisw):";
cin>>theta;
theta*= 3.14/180;
rotate(-theta);
drawpolygon(x,y);
break;
case 4:
cout<<"enter the shearing parameters:";
cin>>x1>>y1;
shear(x1,y1);
drawpolygon(x,y);
break;
case 5:
exit(0);
}
}
}
5. REFLECTION

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
main()
{
clrscr();
int gm=DETECT,gd=DETECT;
initgraph(&gm,&gd,"\\turboc3\\bgi");
int vnum,i,j;
int x[10],y[10];
int xr[10],yr[10];
int choice;
cout<<"enter the number of vertices of polygon( max 10) : ";
cin>>vnum;
cout<<"enter the coordinates of vertices in order(x,y) :-";
for(i=0;i<vnum;i++)
{
cout<<"\nenter (x"<<i<<",y"<<i<<")";
cin>>x[i]>>y[i];
}
clrscr();
cout<<"image generated is :-";
for(i=0;i<vnum-1;i++)
{
line(x[i],y[i],x[i+1],y[i+1]);
}
line(x[i],y[i],x[0],y[0]);
line(0,240,640,240);
line(320,0,320,480);
getch();
clrscr();
cout<<"!!!operations!!!\n1.reflection about x axis";
cout<<"\n2.reflection about y axis\n3.reflection about origin";
cout<<"\n enter your choice : ";
cin>>choice;
clrscr();
switch(choice)
{
case 1: for(i=0;i<vnum;i++)
{
xr[i]=x[i];
yr[i]=240+(240-y[i]);
}
break;
case 2: for(i=0;i<vnum;i++)
{
yr[i]=y[i];
xr[i]=320+(320-x[i]);
}
break;
case 3: for(i=0;i<vnum;i++)
{
xr[i]=320+(320-x[i]);
yr[i]=240+(240-y[i]);
}
break;
default:cout<<"incorrect input";
goto end;
}
cout<<"AFTER REFLECTION :-";
for(i=0;i<vnum-1;i++)
{
line(x[i],y[i],x[i+1],y[i+1]);
}
line(x[i],y[i],x[0],y[0]);
line(0,240,640,240);
line(320,0,320,480);
for(i=0;i<vnum-1;i++)
{
line(xr[i],yr[i],xr[i+1],yr[i+1]);
}
line(xr[i],yr[i],xr[0],yr[0]);
end:
getch();
return(0);
}
REFLECTION ABOUT X-AXIS

You might also like