Computer Graphics Programs
Computer Graphics Programs
{ 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();
}
#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);
}
//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]);
}
}
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);
//
}
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();
}
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]);
}
}