You are on page 1of 34

INTEGRATED ACADEMY OF

MANAGEMENT AND TECHNOLOGY

PRACTICAL FILE

Computer Graphics & Animation Lab

RCA-551

MCA-5th SEMESTER

SUBMITTED TO: SUBMITTED BY:


Mr. Vivek Sen Saxena Sanjana
Roll No.- 1901130149034
INDEX

S.NO. NAME OF PROGRAM DATE REMARK

1. Study Graphics Library and inbuilt functions


for various shapes
2. Write a program for DDA line generation
3. Write a program for Bresenham’s Line
Drawing Algorithm.
4. Write a program for Bresenham’s Circle
Drawing Algorithm.
5. Write a program for Midpoint Circle Drawing
Algorithm.
6. Write a program for Scan line polygon filling
Algorithm.
7. Write a program for Boundary filled
Algorithm
8. Write a program for Cohen-Sutherland line
Clipping Algorithm.
9. Write a program for Cyrus Beck line Clipping
Algorithm.
10. Write a program for Sutherland-Hodgeman
Polygon Clipping Algorithm.
11. Write a menu-based program for
implementing 2D Transformations
12. Implementing Composite transformations(2D)
13. Implement 3D Translation by moving Car
using Flash.
14. Implement Sun rise and Sun set using Flash.
15. Implement 3D Rotation using Flash.

1. DDA Line Generation Algorithm.


#include<graphics.h>  
#include<conio.h>  
#include<stdio.h>  
void main()  
{  
    intgd = DETECT ,gm, i;  
    float x, y,dx,dy,steps;  
 int x0, x1, y0, y1;  
    initgraph(&gd, &gm, "C:\\TC\\BGI");  
    setbkcolor(WHITE);  
    x0 = 100 , y0 = 200, x1 = 500, y1 = 300;  
    dx = (float)(x1 - x0);  
    dy = (float)(y1 - y0);  
    if(dx>=dy)  
           {   steps = dx;  
    }  
    else  
           {  
        steps = dy;  
    }  
    dx = dx/steps;  
    dy = dy/steps;  
    x = x0;  
    y = y0;  
    i = 1;  
    while(i<= steps)  
    {  
        putpixel(x, y, RED);  
        x += dx;  
        y += dy;  
        i=i+1;  
    }  
    getch();  
    closegraph();  
}  

Output:-
2. Bresenham’s Line Drawing Algorithm.
#include<stdio.h>  
#include<graphics.h>  
void drawline(int x0, int y0, int x1, int y1)  
{  
    int dx, dy, p, x, y;  
    dx=x1-x0;  
    dy=y1-y0;  
    x=x0;  
    y=y0;  
    p=2*dy-dx;  
    while(x<x1)  
    {  
        if(p>=0)  
        {  
            putpixel(x,y,7);  
            y=y+1;  
            p=p+2*dy-2*dx;  
        }  
        else  
        {  
            putpixel(x,y,7);  
            p=p+2*dy;}  
            x=x+1;  
        }  
}  
int main()  
{  
  int gdriver=DETECT, gmode, error, x0, y0, x1, y1;  
    initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");  
    printf("Enter co-ordinates of first point: ");  
    scanf("%d%d", &x0, &y0);  
    printf("Enter co-ordinates of second point: ");  
    scanf("%d%d", &x1, &y1);  
    drawline(x0, y0, x1, y1);  
    return 0;  
}  

Output
3. Bresenham’s Circle Drawing Algorithm.
#include <graphics.h>  
#include <stdlib.h>  
#include <stdio.h>  
#include <conio.h>  
#include <math.h>    
    void    EightWaySymmetricPlot(int xc,int yc,int x,int y)  
   {  
    putpixel(x+xc,y+yc,RED);  
    putpixel(x+xc,-y+yc,YELLOW);  
    putpixel(-x+xc,-y+yc,GREEN);  
    putpixel(-x+xc,y+yc,YELLOW);  
    putpixel(y+xc,x+yc,12);  
    putpixel(y+xc,-x+yc,14);  
    putpixel(-y+xc,-x+yc,15);  
    putpixel(-y+xc,x+yc,6);  
   }  
    void BresenhamCircle(int xc,int yc,int r)  
   {  
    int x=0,y=r,d=3-(2*r);  
    EightWaySymmetricPlot(xc,yc,x,y);  
  
    while(x<=y)  
     {  
      if(d<=0)  
             {  
        d=d+(4*x)+6;  
      }  
     else  
      {  
        d=d+(4*x)-(4*y)+10;  
        y=y-1;  
      }  
       x=x+1;  
       EightWaySymmetricPlot(xc,yc,x,y);  
      }  
    }  
  

    int  main(void)  
   {  
    /* request auto detection */  
    int xc,yc,r,gdriver = DETECT, gmode, errorcode;  
    /* initialize graphics and local variables */  
     initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");  
  
     /* read result of initialization */  
     errorcode = graphresult();  
  
      if (errorcode != grOk)  /* an error occurred */  
     {  
        printf("Graphics error: %s\n", grapherrormsg(errorcode));  
        printf("Press any key to halt:");  
        getch();  
        exit(1); /* terminate with an error code */  
     }  
       printf("Enter the values of xc and yc :");  
       scanf("%d%d",&xc,&yc);  
       printf("Enter the value of radius  :");  
       scanf("%d",&r);  
       BresenhamCircle(xc,yc,r);  
  
     getch();  
     closegraph();  
     return 0;  
    }  

Output:
4. Midpoint Circle Drawing Algorithm.
#include<stdio.h>
# include<graphics.h>#include<conio.h>
#include<math.h>
voidbresenham_circle(constinth,constintk,constintr)
{
intx=0,y=r,p=(3-(2*r));
// cleardevice();line(320,1,320,480);line(1,240,640,240);
do
{
delay(15);putpixel((h+x),(k+y),25);
putpixel((h+y),(k+x),15);
putpixel((h+y),(k-x),25);
putpixel((h+x),(k-y),15);
putpixel((h-x),(k-y),25);
putpixel((h-y),(k-x),15);
putpixel((h-y),(k+x),25);
putpixel((h-x),(k+y),15);
x++;
if(p<0)p+=((4*x)+6);
else
{
y--;
p+=((4*(x-y))+10);
}
}
while(x<=y);
}
voidmain(void)
{
intdriver=VGA,mode=VGAHI,h,k,r;

initgraph(&driver,&mode,"c:\tc\bgi");
printf("\nENTERTHEVALUEOF[H-COORDINATE]:");
scanf("%d",&h);
printf("\nENTERTHEVALUEOF[K-COORDINATE]:");
scanf("%d",&k);
printf("\nENTERTHEVALUEOFTHERADIUS:");
scanf("%d",&r);bresenham_circle(320+h,240-k,r);do
{
r--;
bresenham_circle(320+h,240-k,r);
}while(r!=0);
getche();
}

Output
5. Scan line polygon filling Algorithm.
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void main()
{
intn,i,j,k,gd,gm,dy,dx;
intx,y,temp;
int a[20][2],xi[20];
float slope[20];
clrscr();
printf("\n\n\tEnter the no. of edges of polygon : ");
scanf("%d",&n);
printf("\n\n\tEnter the cordinates of polygon :\n\n\n ");
for(i=0;i<n;i++)
{
printf("\tX%dY%d : ",i,i);
scanf("%d %d",&a[i][0],&a[i][1]);
}
a[n][0]=a[0][0];
a[n][1]=a[0][1];
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
/*- draw polygon -*/
for(i=0;i<n;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
getch();
for(i=0;i<n;i++)
{
dy=a[i+1][1]-a[i][1];
dx=a[i+1][0]-a[i][0];
if(dy==0) slope[i]=1.0;
if(dx==0) slope[i]=0.0;
if((dy!=0)&&(dx!=0)) /*- calculate inverse slope -*/
{
slope[i]=(float) dx/dy;
}
}
for(y=0;y< 480;y++)
{
k=0;
for(i=0;i<n;i++)

{
if( ((a[i][1]<=y)&&(a[i+1][1]>y))||
((a[i][1]>y)&&(a[i+1][1]<=y)))
{
xi[k]=(int)(a[i][0]+slope[i]*(y-a[i][1]));
k++;
}
}
for(j=0;j<k-1;j++) /*- Arrange x-intersections in order -*/
for(i=0;i<k-1;i++)
{
if(xi[i]>xi[i+1])
{
temp=xi[i];
xi[i]=xi[i+1];
xi[i+1]=temp;
}
}
setcolor(3);
for(i=0;i<k;i+=2)
{
line(xi[i],y,xi[i+1]+1,y);
getch();
}
}
}

Output:
6. Boundary Filled Algorithm
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include <graphics.h>

// Function for 4 connected Pixels


void boundaryFill4(int x, int y, intfill_color,intboundary_color)
{
if(getpixel(x, y) != boundary_color&&
getpixel(x, y) != fill_color)
{
putpixel(x, y, fill_color);
boundaryFill4(x + 1, y, fill_color, boundary_color);
boundaryFill4(x, y + 1, fill_color, boundary_color);
boundaryFill4(x - 1, y, fill_color, boundary_color);
boundaryFill4(x, y - 1, fill_color, boundary_color);
}
}

//driver code
int main()
{
// gm is Graphics mode which is
// a computer display mode that
// generates image using pixels.
// DETECT is a macro defined in
// "graphics.h" header file
intgd = DETECT, gm;

// initgraph initializes the


// graphics system by loading a
// graphics driver from disk
initgraph(&gd, &gm, "");

int x = 250, y = 200, radius = 50;

// circle function
circle(x, y, radius);

// Function calling
boundaryFill4(x, y, 6, 15);

delay(100000);

getch();

// closegraph function closes the


// graphics mode and deallocates
// all memory allocated by
// graphics system .
closegraph();

return 0;
}

Output
7.Cohen-Sutherland Line Clipping Algorithm.
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
intrcode_begin[4]={0,0,0,0},rcode_end[4]={0,0,0,0},region_code[4];
intW_xmax,W_ymax,W_xmin,W_ymin,flag=0;
float slope;
int x,y,x1,y1,i, xc,yc;
int gr=DETECT,gm;
initgraph(&gr,&gm,"C:\\TURBOC3\\BGI");
printf("\n****** Cohen Sutherlsnd Line Clipping algorithm ***********");
printf("\n Now, enter XMin, YMin =");

scanf("%d %d",&W_xmin,&W_ymin);
printf("\n First enter XMax, YMax =");
scanf("%d %d",&W_xmax,&W_ymax);
printf("\n Please enter intial point x and y= ");
scanf("%d %d",&x,&y);
printf("\n Now, enter final point x1 and y1= ");
scanf("%d %d",&x1,&y1);
cleardevice();
rectangle(W_xmin,W_ymin,W_xmax,W_ymax);
line(x,y,x1,y1);
line(0,0,600,0);
line(0,0,0,600);
if(y>W_ymax) {
rcode_begin[0]=1; // Top
flag=1 ;
}
if(y<W_ymin) {
rcode_begin[1]=1; // Bottom
flag=1;
}
if(x>W_xmax) {
rcode_begin[2]=1; // Right
flag=1;
}
if(x<W_xmin) {
rcode_begin[3]=1; //Left
flag=1;
}

//end point of Line


if(y1>W_ymax){
rcode_end[0]=1; // Top
flag=1;
}
if(y1<W_ymin) {
rcode_end[1]=1; // Bottom
flag=1;
}
if(x1>W_xmax){
rcode_end[2]=1; // Right
flag=1;
}
if(x1<W_xmin){
rcode_end[3]=1; //Left
flag=1;
}
if(flag==0)
{
printf("No need of clipping as it is already in window");
}
flag=1;
for(i=0;i<4;i++){
region_code[i]=rcode_begin[i] &&rcode_end[i] ;
if(region_code[i]==1)
flag=0;
}
if(flag==0)
{
printf("\n Line is completely outside the window");
}
else{
slope=(float)(y1-y)/(x1-x);
if(rcode_begin[2]==0 &&rcode_begin[3]==1) //left
{
y=y+(float) (W_xmin-x)*slope ;
x=W_xmin;

}
if(rcode_begin[2]==1 &&rcode_begin[3]==0) // right
{
y=y+(float) (W_xmax-x)*slope ;
x=W_xmax;

}
if(rcode_begin[0]==1 &&rcode_begin[1]==0) // top
{
x=x+(float) (W_ymax-y)/slope ;
y=W_ymax;

}
if(rcode_begin[0]==0 &&rcode_begin[1]==1) // bottom
{
x=x+(float) (W_ymin-y)/slope ;
y=W_ymin;

}
// end points
if(rcode_end[2]==0 &&rcode_end[3]==1) //left
{
y1=y1+(float) (W_xmin-x1)*slope ;
x1=W_xmin;

}
if(rcode_end[2]==1 &&rcode_end[3]==0) // right
{
y1=y1+(float) (W_xmax-x1)*slope ;
x1=W_xmax;

}
if(rcode_end[0]==1 &&rcode_end[1]==0) // top
{
x1=x1+(float) (W_ymax-y1)/slope ;
y1=W_ymax;

}
if(rcode_end[0]==0 &&rcode_end[1]==1) // bottom
{
x1=x1+(float) (W_ymin-y1)/slope ;
y1=W_ymin;

}
}
delay(1000);
clearviewport();
rectangle(W_xmin,W_ymin,W_xmax,W_ymax);
line(0,0,600,0);
line(0,0,0,600);
setcolor(RED);
line(x,y,x1,y1);
getch();
closegraph();
}
Output:

8. Cyrus back line clipping algorithm


#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
 
void main()
{
inti,gd=DETECT,gm;
int x1,y1,x2,y2,xmin,xmax,ymin,ymax,xx1,xx2,yy1,yy2,dx,dy;
float t1,t2,p[4],q[4],temp;

x1=120;
y1=120;
x2=300;
y2=300;

xmin=100;
ymin=100;
xmax=250;
ymax=250;

initgraph(&gd,&gm,"c:\\turboc3\\bgi");
rectangle(xmin,ymin,xmax,ymax);
dx=x2-x1;
dy=y2-y1;

p[0]=-dx;
p[1]=dx;
p[2]=-dy;
p[3]=dy;

q[0]=x1-xmin;
q[1]=xmax-x1;
q[2]=y1-ymin;
q[3]=ymax-y1;

for(i=0;i<4;i++)
{
if(p[i]==0)
{
printf("line is parallel to one of the clipping boundary");
if(q[i]>=0)
{
if(i<2)
{
if(y1<ymin)
{
y1=ymin;
}

if(y2>ymax)
{
y2=ymax;
}

line(x1,y1,x2,y2);
}

if(i>1)
{
if(x1<xmin)
{
x1=xmin;
}

if(x2>xmax)
{
x2=xmax;
}

line(x1,y1,x2,y2);
}
}
}
}

t1=0;
t2=1;

for(i=0;i<4;i++)
{
temp=q[i]/p[i];

if(p[i]<0)
{
if(t1<=temp)
t1=temp;
}
else
{
if(t2>temp)
t2=temp;
}
}

if(t1<t2)
{
xx1 = x1 + t1 * p[1];
xx2 = x1 + t2 * p[1];
yy1 = y1 + t1 * p[3];
yy2 = y1 + t2 * p[3];
line(xx1,yy1,xx2,yy2);
}

delay(5000);
closegraph();
}

Output
9. Sutherland-Hodgeman Polygon Clipping Algorithm.
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
intgd,gm,n,*x,i,k=0;
//window coordinates int
wx1=220,wy1=140,wx2=420,wy2=140,wx3=420,wy3=340,wx4=220,
wy4=340;
int w[]={220,140,420,140,420,340,220,340,220,140};//array for
drawing window
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\turboc3\\bgi"); //initializing graphics
printf("Window:-");
setcolor(RED); //red colored window
drawpoly(5,w); //window drawn
printf("Enter the no. of vertices of polygon: ");
scanf("%d",&n);
x = malloc(n*2+1);
printf("Enter the coordinates of points:\n");
k=0;
for(i=0;i<n*2;i+=2) //reading vertices of polygon
{
printf("(x%d,y%d): ",k,k);
scanf("%d,%d",&x[i],&x[i+1]);
k++;
}
x[n*2]=x[0]; //assigning the coordinates of first vertex to last
additional vertex for drawpoly method.
x[n*2+1]=x[1];
setcolor(WHITE);
drawpoly(n+1,x);
printf("\nPress a button to clip a polygon..");
getch();
setcolor(RED);
drawpoly(5,w);
setfillstyle(SOLID_FILL,BLACK);
floodfill(2,2,RED);
gotoxy(1,1); //bringing cursor at starting position
printf("\nThis is the clipped polygon..");
getch();

cleardevice();
closegraph();
return 0;
}
Output:

9. Menu-based program for implementing 2D Transformations.


#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
void main()
{
intgm;
intgd=DETECT;
int x1,x2,x3,y1,y2,y3,nx1,nx2,nx3,ny1,ny2,ny3,c;
intsx,sy,xt,yt,r;
float t;
initgraph(&gd,&gm,"c:\tc\bg:");
printf("\t Program for basic transactions");
printf("\n\t Enter the points of triangle");
setcolor(1);
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
printf("\n 1.Transaction\n 2.Rotation\n 3.Scalling\n 4.exit");
printf("Enter your choice:");
scanf("%d",&c);
switch(c)
{
case 1:
printf("\n Enter the translation factor");
scanf("%d%d",&xt,&yt);
nx1=x1+xt;
ny1=y1+yt;
nx2=x2+xt;
ny2=y2+yt;
nx3=x3+xt;
ny3=y3+yt;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
case 2:
printf("\n Enter the angle of rotation");
scanf("%d",&r);
t=3.14*r/180;
nx1=abs(x1*cos(t)-y1*sin(t));
ny1=abs(x1*sin(t)+y1*cos(t));
nx2=abs(x2*cos(t)-y2*sin(t));
ny2=abs(x2*sin(t)+y2*cos(t));
nx3=abs(x3*cos(t)-y3*sin(t));
ny3=abs(x3*sin(t)+y3*cos(t));
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();

case 3:
printf("\n Enter the scalling factor");
scanf("%d%d",&sx,&sy);
nx1=x1*sx;
ny1=y2*sy;
nx2=x2*sx;
ny2=y2*sy;
nx3=x3*sx;
ny3=y3*sy;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();

case 4:
break;
default:
printf("Enter the correct choice");
}
closegraph();
}

Output:
10. Implementing Composite transformations(2D)
#include <graphics.h> /* include the necessary header files*/
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
int xa,xb,xc,ya,yb,yc,y1a,y1b,y1c,x1a,x1b,x1c,x2a,x2b,x2c,y2a,y2b,y2c;
int x3a,x3b,x3c,y3a,y3b,y3c,x4a,x4b,x4c,y4a,y4b,y4c,x5a,x5b,x5c,y5a,y5b,y5c;
inttx,shx,t,ch,shy;
floatang,theta,sx,sy;
int main(void)
{
intgdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode,"C:\\TC\\BGI"); /* request for auto detection*/
printf("\n\t\t\t 2D Composite Transformations");
printf("\n\n Enter all coordinates values :");
scanf("%d %d %d %d %d %d",&xa,&ya,&xb,&yb,&xc,&yc);
printf("\n\n The original Image"); /* get the coordinates for the original image*/
line(xa,ya,xb,yb); /* draw the original image*/
line(xb,yb,xc,yc);
line(xc,yc,xa,ya);
printf("\n\n Enter the value tranlsation factor :"); /* get the translation factor*/
scanf("%d",&tx);
printf("\n\n After Translation ");
x1a=xa+tx;
x1b=xb+tx;
x1c=xc+tx;
y1a=ya;
y1b=yb;
y1c=yc;
line(x1a,y1a,x1b,y1b); /* image after translation*/
line(x1b,y1b,x1c,y1c);
line(x1c,y1c,x1a,y1a);
delay(1);
printf("\n\n Next Operation is Rotation");
printf("\n\n Enter the rotation angle :"); /* get the angle of rotation*/
scanf("%f",&ang);
theta=((ang*3.14)/180); /* convert the angle*/
x2a=x1a*cos(theta)-y1a*sin(theta);
y2a=x1a*sin(theta)+y1a*cos(theta);
x2b=x1b*cos(theta)-y1b*sin(theta);
y2b=x1b*sin(theta)+y1b*cos(theta);
x2c=x1c*cos(theta)-y1c*sin(theta);
y2c=x1c*sin(theta)+y1c*cos(theta);
printf("\n\n After Rotation "); /* the rotated object*/
line(x2a,y2a,x2b,y2b);
line(x2b,y2b,x2c,y2c);
line(x2c,y2c,x2a,y2a);
delay(1);
printf("\n\n Next Operation is Scaling"); /* get the scale factor*/
printf("\n\n Enter the Scale factor :");
scanf("%f %f",&sx,&sy);
x3a=x2a+sx; /* modify the objects coordinates based on the scale factor*/
y3a=y2a+sy;
x3b=x2b+sx;
y3b=y2b+sy;
x3c=x2c+sx;
y3c=y2c+sy;
printf("\n\n After Scaling ");
line(x3a,y3a,x3b,y3b);
line(x3b,y3b,x3c,y3c);
line(x3c,y3c,x3a,y3a);
delay(1);
printf("\n\n Next Operation is Shearing");
printf("\n\n Enter 1 for x-axis \n 2 for y-axis: "); /* get the choice of shearing in
the x or y axis*/
scanf("%d",&ch);
if(ch==1) /* get the shear value*/
{
printf("\n\n Enter the x-SHEAR (^.^) Value: ");
scanf("%d",&shx);
}
else
{
printf("\n\n Enter the y-SHEAR (^.^) Value: ");
scanf("%d",&shy);
}
if(ch==1)
{
x3a=x3a+shx*y3a;
y4a=y3a;
x3b=x3a+shx*y3a;
y4b=y3b;
x3c=x3a+shx*y3a;
y4c=y3c;
}
else
{
x4a=x3a;
y3a=y3a+shy*x3a;
x4b=x3b;
y3b=y3b+shy*x3b;
x4c=x3c;
y3c=y3c+shy*x3c;
}
printf("\n\n After Shearing "); /* draw the final object after shearing*/
line(x3a,y3a,x3b,y3b);
line(x3b,y3b,x3c,y3c);
line(x3c,y3c,x3a,y3a);
delay(1);
printf("\n\n Next Operation is Reflection");
t=abs(y3a-y3c); /* calculate the value for reflection*/
x5a=x3a;
x5b=x3b;
x5c=x3c;
y5a=y3a+10+(2*t);
y5b=y3b+10;
y5c=y3c+10;
printf("\n\n After Reflection "); /* the final object after all the transformations*/
line(x5a,y5a,x5b,y5b);
line(x5b,y5b,x5c,y5c);
line(x5c,y5c,x5a,y5a);
getch();
closegraph();
return 0;
}

Output:

You might also like