0% found this document useful (0 votes)
97 views

Computer Graphics Programs

contains dda line drawing algo,mid point line and citcle drawing algo,line clipping algo,transformations
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

Computer Graphics Programs

contains dda line drawing algo,mid point line and citcle drawing algo,line clipping algo,transformations
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

1.

DDA ALGORITHMNS FOR LINE


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void line(float x1,float y1,float x2,float y2)
{ float y=y2-y1;
float x=x2-x1;
float m=y/x;
if(m>=-1 && m<=1)
{ for(int i=x1;i<x2;i++)
{ putpixel(x1,y1,WHITE);
x1++;
y1=floor(y1+m+0.5);
}
}
else
{ for(int i=y1;i<y2;i++)
{ putpixel(x1,y1,WHITE);
y1++;
x1=floor(x1+(1/m)+0.5);
}
}
}
void main()
{ clrscr();
int x1,x2,y1,y2;
cout<<"Enter Initial coordinates :";
cin>>x1>>y1;
cout<<"Enter Final coordinates : ";
cin>>x2>>y2;
int gd=DETECT;
int gm;
initgraph(&gd,&gm,"..\\bgi");
clearviewport();
line(x1,y1,x2,y2);
getch();
closegraph();
}

2. BRESSENHANS MID POINT ALGORITHMNS FOR LINE


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void line1(float x1,float y1,float x2,float y2)
{ float dy=y2-y1;
float dx=x2-x1;
float m=dy/dx;
float x=x1,y=y1;
putpixel(x,450-y,WHITE);
// gotoxy(3,4);
// cout<<"slope is "<<m<<endl;
if(m>=-1 && m<=1)
{ float d=2*dy-dx;
float incrE=2*dy ;
float incrNE=2*(dy-dx);
while(x < x2)
{ if (d<0)
{ d+=incrE;
x++;
}
else
{ d+=incrNE;
x++;
y++;
}
//
cout<<"( "<<x<<" , "<<y<<" )"<<endl;
putpixel(x,450-y,WHITE);
}
}
else

{ float d = dy-2*dx;
float incrN = -2*dx ;
float incrNE=2*(dy-dx);
while(y < y2)
{ if (d>0)
{ d+=incrN;
y++;
}
else
{ d+=incrNE;
y++;
x++;
}
// cout<<"( "<<x<<" , "<<y<<" )"<<endl;
putpixel(x,450-y,WHITE);
}
}
}
void main()
{ clrscr();
int x1,x2,y1,y2;
cout<<"Enter Initial coordinates :";
cin>>x1>>y1;
cout<<"Enter Final coordinates : ";
cin>>x2>>y2;
int gd=DETECT;
int gm;
initgraph(&gd,&gm,"..\\bgi");
clearviewport();
line1(x1,y1,x2,y2);
getch();
//
closegraph();
}

3. BRESSENHANS MID POINT ALGORITHMNS FOR CIRCLE


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

#include<graphics.h>
void circlepoints(int ,int,int,int ,int) ;
void circle1(int,int,int);
void main()
{
clrscr();
int x,y,r;
cout<<" enter x coordinate of centre :"; cin>>x;
cout<<" enter y coordinate of centre :"; cin>>y;
cout<<" enter radius ";
cin>>r;
int gd=DETECT,gm;
initgraph(&gd,&gm,"..\\bgi");
clearviewport();
circle1(x,y,r);
getch();
closegraph();
}
void circle1(int h,int k,int r)
{
int x=0;
int y=r;
double d = ( 5.0/4.0) -r ;
circlepoints(h,k,x,y,WHITE);
while(y>x)
{
if (d<0)
d+= 2.0*x + 3.0;
else
{ d+= 2.0*(x-y) +5.0;
y--;
}
x++;
circlepoints(h,k,x,y,WHITE);
}
}
void circlepoints(int h,int k,int x,int y,int value)
{
putpixel(h+x,k+y,value);
putpixel(h-x,k+y,value);
putpixel(h+x,k-y,value);
putpixel(h-x,k-y,value);
putpixel(h+y,k+x,value);
putpixel(h-y,k+x,value);
putpixel(h+y,k-x,value);
putpixel(h-y,k-x,value);
}

4. COHENSUTHERLAND LINE CLIPPING


#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<math.h>
#include<graphics.h>
static int xmin=22,ymin=22,xmax=88,ymax=88 ;
int x[2],y[2];
void assign_the_outcodes(int out[4],int x,int y)
{ if (x < xmin) out[1]=1;
if (x > xmax) out[2]=1;
if (y < ymin) out[3]=1;
if (y > ymax) out[4]=1;
}
void clip_line
(int ,int ,int,int,int [],int []) ;
void main()
{
clrscr();
cout<<" enter rectangle x-min and y-min :";
//cin>>xmin>>ymin;
cout<<" enter rectangle x-max and y-max :";
//cin>>xmax>>ymax;
for (int i=1;i<=2;i++)
{
cout<<"enter the line coordinates "<<i<<" -> :";
cin>>x[i]>>y[i];
}
int gd=DETECT,gm;
initgraph(&gd,&gm,"..\\bgi");
clearviewport();
setcolor(YELLOW);
rectangle (xmin,ymin,xmax,ymax);
setcolor(RED);
line(x[1],y[1],x[2],y[2]);
clip_line(xmin,ymin, xmax, ymax, x, y);
getch();

//closegraph();
}
void clip_line (int xmin,int ymin,int xmax,int ymax,int x[],int y[])
{
int out1[5],out2[5];
for(int i=0;i<=4;i++)
out1[i]=0 , out2[i]=0;
gotoxy(15,6);
// assigning the outcodes to line coordinates;
assign_the_outcodes(out1,x[1],y[1]);
assign_the_outcodes(out2,x[2],y[2]);
/* for (i=1;i<=4;i++)
cout<<out1[i];
cout<<endl;
for (i=1;i<=4;i++)
cout<<out2[i];
*/
int flag_inside=1,flag_intersect=1;
for (i=1;i<=4;i++)
if (out1[i] ==0 && out2[i]==0 )
flag_inside=1;
else flag_inside=0;
for (i=1;i<=4;i++)
if (out1[i] && out2[i])
flag_intersect=0 ;
if (flag_inside==1 )
{
cout<<"no cllipping ";
line(x[1],y[1],x[2],y[2]);
setcolor(WHITE);
}
else if (flag_intersect==0)
cout<<"rejecting the line ";
else
{ cout<<"line is partially visible";
// regions outcodes assigned ..... then and doing cutting;
float dx=x[2]-x[1];
float dy=y[2]-y[1];
float m=dy/dx;
float c=y[1]-m*x[1];
//intercept
int flag=1;
for (i=1;i<=2;i++)
{
if (x[i] >=xmin && x[i] <= xmax)
x[i]=x[i];
else if (x[i] < xmin)
x[i]=xmin;
else if (x[i] > xmax)

x[i]=xmax;
y[i] = m * x[i] + c;
// finding y,, x is given
if (y[i] >=ymin && y[i]<=ymax)
goto complete;
//finalised to complete;
else if ( y[i] < ymin)
y[i] = ymin;
else if (y[i] > ymax)
y[i] = ymax;
x[i] = ( y[i] - c ) / m;
if (x[i] >=xmin && x[i]<=xmax)
goto complete; // now second finalised
else if (x[i] < xmin)
x[i]=xmin;
else if (x[i] > xmax)
x[i]=xmax;
complete:
}
setcolor(WHITE);
line(x[1],y[1],x[2],y[2]);
}
}

5. SUTHERLAND HODGEMAN POLYGON CLIPPING


// FILL POLYGON AND CLIPPING
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<math.h>
#include<graphics.h>
void polygon_clip
(int ,int ,int,int,int [],int [],int) ;
void main()
{
clrscr();
int k=0,xmin,ymin,xmax,ymax , xval[10],yval[10],n,polygon[30];
/*
cout<<" enter rectangle x-min and y-min :";
cin>>xmin>>ymin;
cout<<" enter rectangle x-max and y-max :";
cin>>xmax>>ymax;
cout<<"\Nenter the no of coordinates ( A B C -> A ) :";
cin>>n;
for (int i=1;i<=n;i++)
{
cout<<"enter the line coordinates "<<i<<" -> :";
cin>>xval[i]>>yval[i];

polygon[k++]=xval[i];
polygon[k++]=yval[i];
}
*/
// /*
// TEMPORARY ASSIGNMENT
xmin=ymin=30;
ymax=xmax=175;
n=5;
xval[1]=polygon[k++]=25 ;
yval[1]=polygon[k++]= 100;
xval[2]=polygon[k++]=100 ;
yval[2]=polygon[k++]= 170;
xval[3]=polygon[k++]= 200;
yval[3]=polygon[k++]= 10;
xval[4]=polygon[k++]= 231;
yval[4]=polygon[k++]= 391;
xval[5]=polygon[k++]= 25; //first == last
yval[5]=polygon[k++]= 100;
// */
// printing pictures;
int gd=DETECT,gm;
initgraph(&gd,&gm,"..\\bgi");
clearviewport();
setcolor(CYAN);
rectangle (xmin,ymin,xmax,ymax);
setcolor(RED);
setfillstyle(3,CYAN);
fillpoly(4,polygon);

//

polygon_clip(xmin,ymin, xmax, ymax, xval, yval,n);


getch();
closegraph();

}
void polygon_clip
(int xmin,int ymin,int xmax,int ymax,int xval[],int yval[],int n)
{
int x[10],y[10],i,j,newpolygon[30],k=0;
setfillstyle(2,GREEN);
for (j=0;j < n*2;j++)
newpolygon[i]=0;
for (j=1;j < n;j++)
{ float dx=xval[j+1]-xval[j];

float dy=yval[j+1]-yval[j];
float m=dy/dx;
float c=yval[j] - m * xval[j];

//intercept

for (i=j;i<=j+1;i++)
{
if (xval[i] >=xmin && xval[i] <= xmax)
x[i]=xval[i];
else if (xval[i] < xmin)
x[i]=xmin;
else if (xval[i] > xmax)
x[i]=xmax;
y[i] = m * x[i] + c;
// finding y,, x is given
if (y[i] >=ymin && y[i]<=ymax)
goto complete;
//finalised to complete;
else if ( yval[i] < ymin)
y[i] = ymin;
else if (yval[i] > ymax)
y[i] = ymax;
x[i] = ( y[i] - c ) / m;
if (x[i] >=xmin && x[i]<=xmax)
goto complete; // now second finalised
else if (x[i] < xmin)
x[i]=xmin;
else if (x[i] > xmax)
x[i]=xmax;
complete:
newpolygon[k++]=x[i];
newpolygon[k++]=y[i];
}
}
fillpoly(k/2,newpolygon);
// k/2 because k includes both x and y , so use k/2 for (x,y);
}

6. TRANSFORMATION OF TRIANGLE
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<math.h>
#include<graphics.h>

int X_origin=320,Y_origin=245,X_max=640,Y_max=490;
// i.e. the date flows as
A -->> B in following function
void actual_to_origin_translate( int matrix[8],int temporary[8]);
void homogenous_to_polygon_array (int matrix [3][3],int temporary[8]);
void polygon_array_to_homogenous (int matrix [8],int temporary[3][3]);
void multiply_the_matrices
( int A_matrix[3][3],float B_matrix[3][3] ,int temporary[3][3]);
void translation(int [3][3]) ;
void rotation(int [3][3]) ;
void reflection(int [8] );
void scaling(int [3][3]);
void shearing(int [3][3]);
void draw_2_lines()
{ line(320,0,320,490); line(0,245,640,245); }
int actual[8],origin[8];
void main()
{
clrscr();
int homo[3][3], choice;
// printing pictures;
int gd=DETECT,gm;
initgraph(&gd,&gm,"..\\bgi");
clearviewport();
cleardevice();
setcolor(YELLOW);
//drawpoly (4,actual);
cout<<"enter coordinates according to actual cartesian (x,y) plain\n"
" range is \n\t\tX_max = (+ and -) 320\n\t\t Y_max= (+ and -)245\n";
cout<<"\nenter ACTUAL cartesian coordinates [ A B C A] (x,y) :";
/* for (int i=0;i<8;i++)
{
cout<<"enter the coordinate "<<i<<" -> :";
cin>>actual[i];
}
*/
// /*
//assignment
actual[0]= 10;
actual[1]=10;
actual[2]= 150;
actual[3]= 120;
actual[4]= 100;
actual[5]= 180;
actual[6]= 10;
actual[7]= 10;
//
*/
actual_to_origin_translate( actual, origin);
draw_2_lines();

drawpoly(4,origin);
cout<<" the homegenous matrix is ";
polygon_array_to_homogenous ( actual , homo );
for (int i=0;i<3;i++)
for (int j=0;j<3;j++)
cout<<" "<<homo[i][j];
getch();
do
{
clrscr();
cleardevice();
cout<<"\n\t\t1. Translation \n\t\t2. Rotation \n\t\t"
"3. Reflection \n\t\t4. scaling \n\t\t"
"5. shearing \n\t\t6. exit\n\t\t enter your choice ";
cin>> choice;
switch( choice )
{
case 1: translation(homo); break;
case 2: rotation(homo); break;
case 3: reflection(actual); break;
case 4: scaling (homo); break;
case 5: shearing(homo);
break;
case 6: exit(0);
}
cleardevice();
}while( choice <=5);
getch();
closegraph();

//
}
void translation ( int homo[3][3] )
{
float tr[3][3];
int newhomo[3][3],new_actual[8],t1,t2,new_origin[8];
for (int i=0;i<3;i++)
for ( int j=0;j<3;j++)
if (i==j)
tr[i][j]=1;
else
tr[i][j]=0;
cout<<" enter the translation vectors";
cin>>tr[2][0]>>tr[2][1];
cleardevice();
multiply_the_matrices ( homo, tr, newhomo); //newhomo = homo * tr
homogenous_to_polygon_array ( newhomo , new_actual ); // new_actual =
newhomo
drawpoly (4,origin);
draw_2_lines();
getch();
actual_to_origin_translate( new_actual, new_origin );

drawpoly(4,new_origin);
getch();
}
void rotation (int homo[3][3] )
{
cleardevice();
float ch,rot[3][3],angle;
int newhomo[3][3], new_actual[8],new_origin[8];
cout<<"\n enter the angle of rotation in degree \n";
cin>> angle;
float sinp=sin ( (angle * M_PI) / 180 );
float cosp=cos ( (angle * M_PI) / 180 );
for (int i=0;i<3;i++)
for ( int j=0;j<3;j++)
rot[i][j]=0;
//assigning main values
rot[0][0]= cosp;
rot[0][1]= sinp;
rot[1][0]= -sinp;
rot[1][1]= cosp;
drawpoly(4,origin);
multiply_the_matrices( homo, rot, newhomo); //newhomo = homo * rot
homogenous_to_polygon_array ( newhomo , new_actual ); // new_actual =
newhomo
draw_2_lines();
getch();
actual_to_origin_translate( new_actual,new_origin );
drawpoly(4,new_origin);
getch();
}
void reflection (int actual[8])
{
cleardevice();
int new_actual[8],cc,new_origin[8];
for (int i=0;i<8;i++)
new_actual[i]=actual[i];
cout<<" enter choice reflection about 1.x=0\n2.y=0\n3.x=y\n4.x=-y ";
cin>> cc;
if ( cc==1 )
{ for (int i=0;i<8;i++)
if (i%2 == 0)
new_actual[i] = -new_actual[i];
}
else if (cc==2)
{ for (int i=0;i<8;i++)
if (i%2 == 1)
new_actual[i] = -new_actual[i];

}
else if (cc==3)
for (i=0;i<7;++i,++i)
{
int temp= new_actual[i];
new_actual[i]=new_actual[i+1];
new_actual[i+1]=temp;
}
else if (cc==4)
{
for (i=0;i<7;i++,i++)
{
int temp= new_actual[i];
new_actual[i]=new_actual[i+1];
new_actual[i+1]=temp;
}
for (i=0;i<8;i++)
new_actual[i]= -new_actual[i];
}
drawpoly(4,origin);
draw_2_lines();
getch();
actual_to_origin_translate( new_actual, new_origin );
drawpoly(4,new_origin);
getch();
}
void scaling (int homo[3][3])
{
cleardevice();
float sc,scale[3][3];
int newhomo[3][3] ,new_actual[8],new_origin[8];
cout<<" enter the scaling factor [sc] floating: "; cin>>sc;
for (int i=0;i<3;i++)
for ( int j=0;j<3;j++)
if (i==j)
scale[i][j]=sc;
else
scale[i][j]=0;
drawpoly (4,origin);
draw_2_lines();
multiply_the_matrices( homo, scale, newhomo); //newhomo = homo * tr
homogenous_to_polygon_array ( newhomo , new_actual ); // new_actual =
newhomo
actual_to_origin_translate( new_actual, new_origin );
drawpoly(4,new_origin);
getch();
}

void shearing (int homo[3][3])


{
cleardevice();
float sh1,sh2,shear[3][3];
int newhomo[3][3] ,new_actual[8],new_origin[8];
cout<<" enter the shearing factor [ sh1, and sh2 ] : ";
cin>>sh1>>sh2;
for (int i=0;i<3;i++)
for ( int j=0;j<3;j++)
if (i==0 &&j==0)
shear[i][j]=sh1;
else if (i==1 && j==1)
shear[i][j]=sh2;
else
shear[i][j]=0;
drawpoly (4,origin); //printing origin_translated coord.
draw_2_lines();
multiply_the_matrices (homo, shear, newhomo);//newhomo = homo * tr
homogenous_to_polygon_array ( newhomo , new_actual ); // new_actual =
newhomo
actual_to_origin_translate( new_actual, new_origin );
drawpoly(4,new_origin); getch();
}
void actual_to_origin_translate( int matrix[8], int temporary[8])
{ for(int i=0;i<8;i++)
if (i%2==0)//even numbers
temporary[i]= X_origin + matrix[i];
else
{
temporary[i]= Y_origin + matrix[i];
temporary[i]= Y_max - temporary[i];//
// note that here second line transformsto actual cartesian plain
}
}
void homogenous_to_polygon_array (int matrix [3][3], int temporary[8] )
{
int k=0;
for(int i=0;i<3;i++)
for (int j=0;j<3;j++)
if (j!=2)
{
temporary[k] = matrix[i][j]; k++;
}
temporary[k]=temporary[0] ; temporary[k+1]=temporary[1];
}
void polygon_array_to_homogenous (int matrix [8],int temporary[3][3] )
{
int k=0;
for(int i=0;i<3;i++)
for (int j=0;j<3;j++)
if (j==2)

temporary[i][j] = 1;
else
{
temporary[i][j] = matrix[k];
k++;
}
}
void multiply_the_matrices
( int A_matrix[3][3],float B_matrix[3][3] , int temporary[3][3] )
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{ temporary[i][j]=0;
for (int k=0;k<3;k++)
temporary[i][j] += int (A_matrix[i][k] * B_matrix[k][j]);
}
}

You might also like