EX. NO. 1 Bresenhams Algorithms For Line, Circle, Ellipse: k+1 K k+1 K
EX. NO. 1 Bresenhams Algorithms For Line, Circle, Ellipse: k+1 K k+1 K
EX. NO. 1 Bresenhams Algorithms For Line, Circle, Ellipse: k+1 K k+1 K
1
Bresenhams algorithms for line, circle, ellipse
AIM
To write and execute a c program to draw a line using BRESENHAMS line algorithm.
ALGORITHM
1. Initialize the graphics driver and mode.
2. Read the coordinates for starting point (x1,y1) and ending point (x2,y2).
3. Find dx=x2-x1 and dy=y2-y1 with its absolute value.
4. If x1>x2 then set the increment step as -1, if x1<x2 then set the increment point as
1, else set the increment step as 0. Similarly for the y values set the increment step
points as -1, 1, 0.
5. If dy > dx then exchange the dx, dy values.
6. The initial decision value d=2*dy-dx;
7. At each Xk (ie) k=0; k=1 etc perform the following test, If(d<0) the next point is
(Xk+1,Yk) and dk+1= dk+2dy-2dx else the next point is (Xk+1,Yk+1) and d k+1=
dk-2dx
8. Repeat step 7 by dx times.
PROGRAM
#include<stdio.h>
#include<graphics.h>
main()
{
int g_driver=DETECT,g_mode,x1,y1,x2,y2,dx,dy,step,i,x,y,d,exdy=0;
int xinc,yinc,temp;
initgraph(&g_driver,&g_mode,"c:\\tc\\bgi");
// Getting the coordinate values
printf("Enter the x1,y1 coordinates \n");
scanf("%d",&x1);
scanf("%d",&y1);
printf("Enter the x2,y2 coordinates \n");
scanf("%d",&x2);
1
scanf("%d",&y2);
//Find the distance of x and y
dx=abs(x2-x1);
dy=abs(y2-y1);
if((x2-x1)>0)
xinc=1; // Drawing teh line from left to right
else if((x2-x1)<0)
xinc=-1;// right to left
else
xinc=0; // x value constant
if((y2-y1)>0)
yinc=1; // top to bottom
else if((y2-y1)<0)
yinc=-1; //bottom to top
else
yinc=0; // y is contant
if(dy>dx)
{
temp=dy;
dy=dx;
dx=temp;
exdy=1;
}
cleardevice();
setbkcolor(WHITE);
setcolor(BLUE);
outtextxy(50,50,"BRESENHSAMS LINE");
outtextxy(50,52,".................");
// set the initial points
x=x1;
y=y1;
2
d=2*dy-dx;
for(i=1;i<=dx;i++)
{
putpixel(x,y,RED);
while(d>=0)
{
if(exdy==1)
x=x+xinc;
else
y=y+yinc;
d=d-2*dx;
}
if(exdy==1)
y=y+yinc;
else
x=x+xinc;
d=d+2*dy;
delay(100);
}
getch();
closegraph();
return(0);
}
SAMPLE OUTPUT
BRESENHSAMS LINE
……………………….
Enter the x1,y1 coordinates
100 100
Enter the x1,y1 coordinates
200 200
3
Circle Drawing - Using BRESENHAMS algorithm
AIM
To write and execute a c program to draw a circle using BRESENHAMS circle
algorithm.
ALGORITHM
1. Initialize the graphics driver and mode.
2. Read the center point (xc,yc) and radius r.
3. Calculate the pivot value p using suitable formula to find the pixel values.
4. Using the values found out in step 3,put the pixels which forms the circle.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void draw_circle(int,int,int);
void plot(int,int,int,int);
void main()
{
int xc,yc,r;
int gd=DETECT,gm;
initgraph(&gd,&gm,”c:\\tc\\bgi”);
cleardevice();
printf(“Enter the x axis\n”);
scanf(“%d”,&xc);
4
printf(“Enter the y axis\n”);
scanf(“%d”,&yc);
printf(“\n Enter the radius\n”);
scanf(“%d”,&r);
circle(xc,yc,r);
getch();
closegraph();
}
void draw_circle(int xc,int yc,int r)
{
int p,x,y;
x=0;
y=r;
while(x<=y)
{
if(p<=y)
p=p+(4*x+6) ;
else
{
p=p+10+4*(x-y);
y=y-1;
}
plot(xc,yc,x,y);
x=x+1 ;
}
}
void plot(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,15) ;
putpixel(xc+x,yc-y,15) ;
putpixel(xc-x,yc+y,15) ;
5
putpixel(xc-x,yc-y,15) ;
putpixel(xc+y,yc+x,15) ;
putpixel(xc+y,yc-x,15) ;
putpixel(xc-y,yc+x,15) ;
putpixel(xc-y,yc-x,15) ;
}
SAMPLE OUTPUT
ENTER THE CENTER AND THE RADIUS: 100 100 50.
6
8. Repeat the process steps 9-12 until px value less than that of py
9. Calculate x = x+1 and px=px+twory2
10. If p>=0 then y=y-1 and py = py – twory2
11. If p<0 then next p value is calculated by p=p+ry2+px else P=p+ry2+px-py
12. Plot the point using four diff pixel point process
13. For region 2 initial p value is calculated by
P =ry2(x+5)(x+5)+rx2*(y-1)*(y-1)-rx2*ry2
14. Repeat the process steps 15-18 until y>0
15. Calculate y=y-1 and py=py-tworx2
16. If p<0 then x=x+1 and px =px+twory2
17. If P>0 then next p value is calculated by p=p+rx2-py else
p-p+rx2-py+px
18. Plot points using four pixel point process
19. End the process. The plot point procedure consists of the following steps
Putpixel (xc+x,yc+y)
Putpixel (xc-x,yc+y)
Putpixel (xc+x,yc-y)
Putpixel (xc-x,yc-y)
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int xcenter,ycenter,x=0,y=0;
void main()
{
int p,px,py;
int ry,rx,ry2,rx2,tworx2,twory2;
int gd=DETECT,gm;
void plotpoints(int,int,int,int);
initgraph(&gd,&gm,“c:\\tc\\bgi”);
7
clrscr();
printf(“Enter the center coordinates”);
scanf(“%d%d”,&xcenter,&ycenter);
printf(“enter the major and minor axes”);
scanf(“%d%d”,&rx,ry);
ry2=ry*ry;
rx2=rx*rx;
twory2=2*ry2;
tworx2=2*rx2;
x=0;
y=ry;
plotpoints(xcenter,ycenter,x,y);
p=ceil(ry2-rx2*ry+(0.25*rx2));
px=0;
py=tworx2*y;
while(px<py)
{
x=x+1;
px-px+twory2;
if(p>=0)
{
y=y-1;
py=py-tworx2;
}
if(p<0)
p=p+ry2+px;
else
p=p+ry2+px-py;
plotpoints(xcenter,ycenter,x,y);
}
p=ceil(ry2*(x+0.5)*(x+0.5)+rx2*(y-1)*(y-1)-rx2*ry2);
8
while(y>0)
{
y=y-1;
py=py-tworx2;
if(p<=0)
{
x=x+1;
px=px+twory2;
}
if(p>0)
p=p+rx2-py;
else
p=p+rx2-py+px;
plotpoints(xcenter,ycenter,x,y);
}
getch();
}
void plotpoints(int xcenter,int ycenter,int x,int y)
{
setcolor(BLUE);
putpixe1(xcenter+x,ycenter+y,1);
putpixe1(xcenter-x,ycenter+y,1);
putpixe1(xcenter+x,ycenter-y,1);
putpixe1(xcenter-x,ycenter-y,1);
/*putpixe1(xcenter+y,ycenter+x,1);
putpixe1(xcenter-y,ycenter+x,1);
putpixe1(xcenter+y,ycenter-x,1);
putpixe1(xcenter-y,ycenter-x,1);*/}
SAMPLE OUTPUT
9
Enter the coordinates
50 50
EX. NO. 2
2D Transformation such as translation, rotation, scaling, reflection and
shearing
AIM
To perform the transformation of Translation on 2D objects.
ALGORITHM
1. Initialize the graphics driver and mode.
2. Read the coordinates for 2D object which is transformed.
3. For Translation, read the translation vector and add it to the original coordinates of
the 2D object.
4. Stop the program.
PROGRAM
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
void main()
{
int gdrive=0,gmode,ch;
float tx,ty,r,a,b,c,d;
char cho;
10
printf("\n\n\t\t 2D Translation \n\n");
printf("Enter the coordinates of the rectangle \n" );
scanf("%f%f%f%f",&a,&b,&c,&d);
initgraph(&gdrive,&gmode,"c:\\tc\\bgi\\");
do{
printf("1.Translation in X direction 2.Translation in Y direction
3.Translation in X & Y direction\n\n");
scanf("%d",&ch);
switch(ch){
case 1:
cleardevice();
rectangle(a,b,c,d);
printf("Enter the translation factor for X direction");
scanf("%f",&tx);
rectangle(a,b,c,d);
rectangle(a+tx,b,c+tx,d);
break;
case 2:
cleardevice();
rectangle(a,b,c,d);
printf("Enter the translation factor for Y direction");
scanf("%f",&ty);
rectangle(a,b,c,d);
rectangle(a,b+ty,c,d+ty);
break;
case 3:
cleardevice();
rectangle(a,b,c,d);
printf("Enter the translation factor for X and Y direction");
scanf("%f %f",&tx,&ty);
rectangle(a,b,c,d);
11
rectangle(a+tx,b+ty,c+tx,d+ty);
break;
}
getch();
printf("continue y/n");
scanf("%c",&cho);
cleardevice();
}
while(cho=='y');
closegraph();
}
SAMPLE OUTPUT
2D Translation
Enter the coordinates of the rectangle
100 100 200 200
12
Rotation
AIM
To perform the transformation of Rotation on 2D objects.
ALGORITHM
1. Initialize the graphics driver and mode.
2. Read the coordinates for 2D object which is transformed.
3. For Rotation read the rotation angle and multiply it to the original coordinates of the
2D objectusing the formula x=x*cos(ang)-y*sin(ang),y=x*sin(ang)+y*cos(ang)
4. Stop the program.
PROGRAM
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
void main()
{
int gdrive=0,gmode,ch;
float a,b,c,d,c1,d1;
char cho;
signed int ang;
printf("Enter the coordinates of line");
scanf("%f%f%f%f",&a,&b,&c,&d);
initgraph(&gdrive,&gmode,"c:\\tc\\bgi\\");
line(a,b,c,d);
printf("Enter the rotation angle");
scanf("%d",&ang);
c1=c*cos(ang)-d*sin(ang);
d1=c*sin(ang)+d*cos(ang);
line(a,b,c1,d1);
getch();
closegraph();
13
}
SAMPLE OUTPUT
Enter the coordinates of line
100 100 200 100
Scaling
AIM
To perform the transformation of Scaling on 2D objects.
ALGORITHM
1. Initialize the graphics driver and mode.
2. Read the coordinates for 2D object which is transformed.
3. For Scaling read the scaling vector and multiply it to the original coordinates of the 2D
object.
4. Stop the program
PROGRAM
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
void main()
{
int gdrive=0,gmode,ch;
float sx,sy,r,a,b,c,d;
char cho;
14
printf("\n\n\t\t 2D Scaling \n\n");
printf("Enter the coordinates of the rectangle \n" );
scanf("%f%f%f%f",&a,&b,&c,&d);
initgraph(&gdrive,&gmode,"c:\\tc\\bgi\\");
do
{
printf("1.Scaling in X direction 2.Scaling in Y direction 3.Scaling in X & Y
direction\n\n");
scanf("%d",&ch);
switch(ch){
case 1:
cleardevice();
rectangle(a,b,c,d);
printf("Enter the scaling factor for X direction");
scanf("%f",&sx);
rectangle(a,b,c,d);
rectangle(a*sx,b,c*sx,d);
break;
case 2:
cleardevice();
rectangle(a,b,c,d);
printf("Enter the scaling factor for Y direction");
scanf("%f",&sy);
rectangle(a,b,c,d);
rectangle(a,b*sy,c,d*sy);
break;
case 3:
cleardevice();
rectangle(a,b,c,d);
printf("Enter the scaling factor for X and Y direction");
scanf("%f %f",&sx,&sy);
15
rectangle(a,b,c,d);
rectangle(a*sx,b*sy,c*sx,d*sy);
break;
}
getch();
printf("continue y/n");
scanf("%c",&cho);
cleardevice();
}
while(cho=='y');
closegraph();
}
SAMPLE OUTPUT
2D Scaling
Enter the coordinates of the rectangle
100 100 200 200
1
Enter the scaling factor for X direction
2
16
Shearing and Reflection
AIM
To perform the transformation of Shearing and Reflection on 2D objects.
ALGORITHM
1. Initialize the graphics driver and mode.
2. Read the coordinates for 2D object which is transformed.
3. For Shearing read the shearing factor and apply the shearing formula .
Shearing with respect to x , x’=x + shx*y, y’=y
Shearing with respect to y , y’=y + shy*x, x’=x
4. Stop the program
PROGRAM
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<string.h>
void main()
{
int gd=DETECT,gm,midx,midy,w;
int xs1,xs2,xs3,ys1,ys2,ch,x1,y1,x2,y2,x3,y3,a,b,c,n,z,sh;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\nEnter the first coordinates");
scanf("%d%d",&x1,&y1);
printf("\nEnter the second coordinates");
scanf("%d%d",&x2,&y2);
midx=abs((x2-x1)/2);
midy=abs((y2-y1)/2);
cleardevice();
do
{
printf("1.Shearing\n2.Reflection\n3.Quit\n");
printf("Enter your choice");
17
scanf("%d",&n);
switch(n)
{
case 1:
cleardevice();
line(x1,y1,x2,y2);
moveto(x2,y2);
lineto(x1+midx,y1+midy);
lineto(x1,y1);
printf(" Enter the shearing factor \n");
scanf("%d",&sh);
printf("Enter the choice 1- with respect to X 2. with respect to
Y\n");
scanf("%d",&ch);
printf("%d",ch);
if(ch==1)
{
xs1=x1+sh*y1;
xs2=x2+sh*y2;
line(x1,y1,xs2,y2);
moveto(xs2,y2);
lineto(xs1+abs((xs2-xs1)/2),y1+abs((y2-y1)/2));
lineto(x1,y1);
}
else
{
ys1=y1+sh*x1;
ys2=y2+sh*x2;
line(x1,y1,x2,ys2);
moveto(x2,ys2);
lineto(x1+abs((x2-x1)/2),ys1+abs((ys2-ys1)/2));
18
lineto(x1,y1);
}
getch();
break;
case 2:
cleardevice();
line(x1,y1,x2,y2);
moveto(x2,y2);
lineto(x1+midx,y1+midy);
lineto(x1,y1);
if(y1>y2)
z=y1;
else
z=y2;
if(x1<x2)
w=x2;
else
w=x1;
line(0,z+10,w+50,z+10);
line(x1,z+20+(z-y1),x2,z+20+(z-y2));
moveto(x2,z+20+(z-y2));
lineto(x1+midx,z+20+(z-(y1+midy)));
lineto(x1,z+20+(z-y1));
getch();
break;
case 3:
exit(0);
break;
}
}
while(n<=3);
19
getch();
}
SAMPLE OUTPUT
Enter the first coordinates
100 100
Enter the second coordinates
200 100
1. Shearing
2. Reflection
3. Quit
1. Shearing
2. Reflection
3. Quit
20
Enter your choice
2
EX. NO. 3
Cohen-Sutherland 2D Clipping and window – view port mapping
2-D Clipping
AIM
To write a program in C for the line clipping algorithm(Cohen-Sutherland algorithm).
ALGORITHM
1. Change the normal mode to graphics mode by setting “initgraph” and “detect graph”.
2. Get the points of window minimum and window maximum so that to define the
window size.
3. Accept the points in the line end co ordinates
4. Find whether
a) The points lies within the window area
b) The points lies outside the window area
c) The line lies partially inside and outside
5. If the line is fully inside accept the line
6. If the line is fully outside reject the line
21
7. If the line is partially inside and outside clip the line. Midpoint subdivision procedure is
used to find the point lying on the window to clip the line.
8. The accepted lines are redraw within the window area.
9. Stop the process.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<stdlib.h>
#include<math.h>
typedef struct coordinate
{
int x,y;
char code[100];
}
pt;
void drawwindow();
void drawline(pt p1,pt p2,int c1);
pt setcode(pt p);
int visibility(pt p1,pt p2);
//pt resetendpt(pt p1,pt p2);
void main()
{
int gd=DETECT,gm,v;
pt p1,p2,ptemp;
initgraph(&gd,&gm,“c:\\tc\\bgi”);
cleardevice();
printf(“enter points:(x,y)”);
scanf(“%d%d”,&p1.x,&p2.y);
printf(“enter 2 point (x,y)”);
22
scanf(“%d%d”,&p2.x,&p2y);
cleardevice();
drawwindow();
getch();
drawline(p1,p2,15);
getch();
cleardevice();
drawwindow();
midsub(p1,p2);
getch();
closegraph();
}
mindsub(pt p1,pt p2)
{
pt mid;
int v;
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
switch(v)
{
case 0:drawline(p1,p2,15);
break;
case 1:
break;
case 2:
mid.x=p1.x+(p2.x-p1.x)/2;
mid.y=p1.x+(p2.y-p1.y)/2;
midsub(p1,mid);
mid.x=mid.x+1;
mid.y=mid.y+1;
23
midsub(mid,p2);
break;
}
return(0);
}
void drawwindow()
{
setcolor(4);
line(150,100,450,100);
line(450,100,450,400);
line(450,400,150,400);
line(150,400,150,100);
}
void drawline(pt p1,pt p2,int c1)
{
setcolor(c1);
line(p1.x,p1.y,p2.x,p2.y);
}
pt setcode(Pt p)
{
pt ptemp;
if(p.y<=100)
ptem.code[0]=‘1’;
else
ptemp.code[1]=‘0’;
if(p.x>=450)
ptemp.code[2]=‘1’;
else
ptemp.code[2]=‘0’;
if(p.x<=150)
ptemp.code[3]=‘1’;
24
else
ptemp.code[3]=‘0’;
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
}
int visibility(pt p1,pt p2)
{
int i,flag=0;
for(i=0;<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);
}
SAMPLE OUTPUT
25
Before Clipping
After Clipping
26
int ywmax=50,ywmin=9,xwmax=500,xwmin=400;
int yvmax=150,yvmin=50,xvmax=326,xvmin=300;
int xv,yv,sx,sy;
intigraph(&gd,&gm,”c:\\tc\\bgi”);
cleardevice();
putpixel(xw,yw,5);
rectangle(ywmin,ywmax,xwmax,xwmin);
gotoxy(10,28);
printf(“\WINDOW COORDINATE SYSTEM”);
getch();
cleardevice();
setcolor(3);
rectangle(yvmin,yvmax,xvmax,xvmin);
sx=((xvmax-xvmin)/(xwmax-xwmin)) ;
sy=((yvmax-vvmin)/(ywmax-ywmin)) ;
xv=xvmin+((xw-xwmin)*sx) ;
yv=yvmin+((yw-ywmin)*sy) ;
putpixel(xv,yv,6) ;
gotoxy(10,28) ;
printf(“\VIEW PORT COORDINATE SYSTEM”);
getch();
closegraph();
}
SAMPLE OUTPUT
27
EX. NO. 4
3D – Transformation such as translation, rotation and scaling
AIM
To perform the transformation operations such as Translation, Scaling and Rotation on
3D objects.
ALGORITHM
1. Initialize the graphics driver and mode.
2. Read the coordinates for 3D object which is transformed.
3. For Translation, read the translation vector and add it to the original coordinates of
the 3D object.
4. For Scaling, read the scaling vector and multiply it to the original coordinates of the
3D object.
28
5. For Rotation, read the angle of rotation, distance value and calculate the new
coordinates using the formula for rotation and draw the 3D object for the new
coordinates.
PROGRAM
#include<stdio.h>
#include<graphics.h>
#include <math.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
void cube(int,int,int);
int gd=DETECT,gm;
int tx,ty,tz,sx,sy,sz,x,y,z,m,o,ch,dx,dy,dz;
int xf,yf,zf,a,teta,x1,x2,y1,y2,m1,n;
initgraph(&gd,&gm,"c:\\tc\\bgi");
clrscr();
printf("\n 1-Translation \n 2-Scaling \n 3-Rotation \n ");
printf("Enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n Enter the value for cube:");
scanf("%d %d %d",&x,&y,&z);
cube(x,y,z);
printf("Enter the translation vertex");
scanf("%d %d %d",&tx,&ty,&tz);
dx=x+tx;
dy=y+ty;
dz=z+tz;
29
cube(dx,dy,dz);
getch();
case 2:
printf("\n Enter the value of cube:");
scanf("%d %d %d",&x,&y,&z);
cube(x,y,z);
printf("Enter the pivot element");
scanf("%d %d %d",&xf,&yf,&zf);
printf("enter the scaling vector");
scanf("%d %d %d",&sx,&sy,&sz);
dx=x*sx+(1-sx)*xf;
dy=y*sy+(1-sy)*yf;
dz=z*sz+(1-sz)*zf;
cube(dx,dy,dz);
getch();
case 3:
printf("enter ur choice");
printf("4. x axis 5.y axis 6.z axis");
scanf("%d",&n);
switch(n)
{
case 4:
printf("4.with repect x axis");
printf("enter the va;lue of the cube");
scanf("%d%d%d",&x,&y,&z);
cube(x,y,z);
printf("THE ROTATION IS ONLY RESPECT WITH X AXIS");
printf("\nenter the angle of rotation");
scanf("%d",&m);
dx=x;
dy=y*cos(m)-z*sin(m);
30
dz=y*sin(m)+z*cos(m);
cube(dx,dy,dz);
getch();
case 5:
printf("5.with repect y axis");
printf("enter the va;lue of the cube");
scanf("%d%d%d",&x,&y,&z);
cube(x,y,z);
printf("THE ROTATION IS ONLY RESPECT WITH Y AXIS");
printf("\nenter the angle of rotation");
scanf("%d",&m);
dx=z*sin(m)+x*cos(m);
dy=y;
dz=z*cos(m)-x*sin(a);
cube(dx,dy,dz);
getch();
case 6:
printf("6.with repect z axis");
printf("enter the va;lue of the cube");
scanf("%d%d%d",&x,&y,&z);
cube(x,y,z);
printf("THE ROTATION IS ONLY RESPECT WITH Z AXIS");
printf("\nenter the angle of rotation");
scanf("%d",&m);
dx=x*cos(m)-y*sin(m);
dy=y*sin(m)+z*cos(m);
dz=z;
cube(dx,dy,dz);
getch();
}
}
31
getch();
}
void cube(int x,int y,int z)
{
line(x,y,x+100,y);
line(x,y,x-50,y+50);
line(x-50,y+50,x+50,y+50);
line(x+50,y+50,x+100,y);
line(x-50,y+50,x-50,y-50);
line(x-50,y-50,x,y-100);
line(x-50,y-50,x+50,y-50);
line(x,y-100,x+100,y-100);
line(x+100,y-100,x+50,y-50);
line(x+50,y-50,x+50,y+50);
line(x+100,y-100,x+100,y);
line(x,y-100,x,y);
getch();
}
SAMPLE OUTPUT
THREE-D TRANSFORMATION
1-TRANSLATION
2-SCALING
3-ROTATION
32
ENTER THE CHOICE: 1
1. FIXED POINT
2. NOT FIXED POINT
33
ENTER THE SCALING VALUES: 1 2 1
1-TRANSLATION
2-SCALING
3-ROTATION
ENTER THE CHOICE: 3
1-ABOUT X-AXIS
2-ABOUT Y-AXIS
3-ABOUT Z-AXIS
34
EX. NO. 5
Projections of 3D Images
AIM
To perform a parallel projection on a three dimensional image.
ALGORITHM
1. Initialize the graphics driver and mode.
2. Read the coordinates for 3D object which is to be projected.
3. Get the menu choices for parallel projection.
4. Read the different coordinates for the four options
Top-view
Front-view
Side-view
Original view.
PROGRAM
#include<stdio.h>
#include<graphics.h>
void main()
{
int g_driver=DETECT,g_mode;
int ae,ef,bf,ab,bc,choice,choice1;
initgraph(&g_driver,&g_mode,"c:\\tc\\bgi");
printf(" \t values of 3D-BAR \n");
printf("Enter the value of left :"); //int left, int top, int right, int
bottom,int depth
scanf("%d",&ae); // ae -> ,ef-> ,bf-> , ab-> ,bc->
printf("Enter the value of top :");
scanf("%d",&ef);
printf("Enter the value of right :");
scanf("%d",&bf);
printf("Enter the value of bottom :");
scanf("%d",&ab);
35
printf("Enter the value of depth :");
scanf("%d",&bc);
cleardevice();
printf("\t Your 3D - BAR \n");
bar3d(ae,ef,bf,ab,bc,1);
//bar3d(midx-50, midy-50, midx+50,midy+50, 10, 1);
getch();
do
{
cleardevice();
printf("\t OPTION DETAILS\n ");
printf(" 1.To view 3D-BAR \n");
printf(" 2.To view Front view\n");
printf(" 3.To view Top view\n");
printf(" 4.To view Side view\n");
printf(" Enter your choice : ");
scanf("%d",&choice);
cleardevice();
switch(choice)
{
case 1:
bar3d(ae,ef,bf,ab,bc,1);
case 2:
// bar(int left, int top, int right, int bottom);
bar(ae,ef,bf,ab);
break;
case 3:
bar(ae,ae,bf,(ae+bc));
break;
case 4:
bar(ae,ef,(ae+bc),ab);
36
break;
default:
printf("\n\tWrong choice\n");
}
printf("Do you wish to be continue? [YES(1)/NO(other)]: ");
scanf("%d",&choice1);
}
while(choice1==1);
cleardevice();
SAMPLE OUTPUT:
values of 3D-BAR
Your 3D – BAR
OPTION DETAILS
1.To view 3D-BAR
2.To view Front view
3.To view Top view
4.To view Side view
Enter your choice : 2
37
Do you wish to be continue? [YES(1)/NO(other)]:1
OPTION DETAILS
1.To view 3D-BAR
2.To view Front view
3.To view Top view
4.To view Side view
38
EX. NO.6
Conversion between color models
Aim:
To change the colors of an object
ALGORITHM:
1. Click a layer name to make it the current layer, and select a frame in the layer
where you want the animation to start.
2. If the frame isn't already a key frame, choose Insert > Key frame to make it one.
3. Create the artwork for the first frame of the sequence.
4. You can use the drawing tools, paste graphics from the Clipboard
5. Place the Required Object in the scene
6. Place color buttons, text boxes and required tools
7. Write the action script
39
Sample Output
40
EX. NO. 7
Text Compression Algorithm
Run Length Encoding:
AIM:
To implement run length encoding in C
ALGORITHM:
1. Get the input string of character from the user and store it in an array.
2. Initially the string of characters is read one character by another and they are stored
in a structure.
3. The structure contains a character and an integer.
4. Each and every time a character is read from the input string its occurrence is also
found.
5. When a character is checked, it is stored in the sample character in the structure
6. Then the next character is also checked, if the ext character is matching the previous
character, then a counter is increased.
7. As soon as the matching is not there, store the character and the number of
occurrence, i.e. the counter value in the structure.
8. This process goes on until the input string comes to an end.
9. This results in the compressed text.
10. While decompression, the structure is read first.
11. For each character, it checks for the number of occurrence, then prints the character
that number of times.
12. This results in the decompressed text.
PROGRAM
#include<stdio.h>
#include<stdlib.h>
void main()
{
void decomp(char op[30]);
char ip[30],op[30];
char v[1],value1,value2;
41
int i=0,j=1,k=0,t,x;
int cmp,l,rpt=1,rpt1,rpt2;
clrscr();
printf("Enter the data : ");
gets(ip);
for(l=1;l<strlen(ip);l++)
{
cmp=strcmp(ip[i],ip[j]);
if(cmp==0)
{
rpt=++rpt;
for(t=j+1;strcmp(ip[i],ip[t])==0;t++)
{
rpt=++rpt;
}
if(rpt<=4)
{
--k;
for(x=1;x<=rpt;x++)
{
op[++k]=ip[i];
}
}
if(rpt>4)
{
op[k++]='#';
op[k]=ip[i];
rpt1=rpt/10;
rpt2=rpt%10;
value1=*itoa(rpt1,v,10);
value2=*itoa(rpt2,v,10);
42
op[++k]=(value1);
op[++k]=(value2);
}
rpt=1;
i=t,j=t+1,k++;
}
else
{
rpt=1;
op[k]=ip[i];
i++,j++,k++;
if(j>=strlen(ip))
{
op[k]=ip[i];
op[++k]='\0';
goto end;
}
}
}
end:
printf("Compressed Data : %s\n",op);
getch();
decomp(op);
}
void decomp(char ip1[30])
{
char op1[30];
char temp,*t1,*t2;
int i,i1,i2,j,k=0,l;
for(l=0;l<strlen(ip1);++l)
{
43
if (ip1[l]=='#')
{
temp=ip1[++l];
*t1=ip1[++l];
*t2=ip1[++l];
i1=atoi(t1);
i2=atoi(t2);
i=(i1*10)+i2;
for(j=0;j<i;j++)
{
op1[k++]=temp;
}
}
else if (ip1[l]=='\0')
{
goto end;
}
else
{
op1[k++]=ip1[l];
}
}
end:
op1[k]='\0';
printf("Decompresed Output : %s\n",op1);
getch();
getch();
}
44
SAMPLE INPUT AND OUTPUT
Enter the data : aaaassdddddddddfffggggggg
Compressed Data : aaaass#d09fff#g07
Decompresed Output : aaaassdddddddddfffggggggg
Huffman Coding:
AIM
To Implement Huffman Coding in C
ALGORITHM
1. First the source symbols are listed in the order of decreasing probability.
2. The two source symbols of the lowest probability are assigned a 0 and a 1.
3. This part of the step is referred to as a splitting stage.
4. These two source symbols are regarded as being combined into a new source symbol
with probability equal to the sum of the two original probabilities.
5. The probability of the new symbol is placed in the list in accordance with its value.
6. The procedure is repeated until we are left with a final list of source statistics of only
two for which a 0 and a 1 are assigned.
7. Then traversing backwards, find the desired digit or symbol.
8. The resulting sequence of 0’s and 1’s are the resulting Huffman code for that particular
symbol.
PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
static char ip[30],code[30];
static char table[30][30];
static int count[30];
static int related[30][30];
int r,rpt;
45
int i,j;
void show(char code[30],char table[30][30]);
void counting(char ip[30],char code[30],int count[30],int related[30][30]);
void sort(char code[30],int count[30]);
int calculate(int cou,int count[30],int related[30][30],char table[30][30]);
void sort2(int count[30],int related[30][30],int cnt);
void rectify(char table[30][30]);
void tableit(char ip[30],char code[30],char table[30][30]);
clrscr();
for(i=0;i<30;++i)
{
for(j=0;j<15;++j)
{
table[i][j]='x';
}
}
printf("Enter the data :");
scanf("%s",ip);
counting(ip,code,count,related);
sort(code,count);
r=calculate(strlen(code),count,related,table);
rpt=strlen(code)-2;
for(rpt=rpt;rpt>0;--rpt)
{
r=calculate(r,count,related,table);
}
rectify(table);
printf("\t T A B L E : \n");
printf("\t-------------\n");
show(code,table);
tableit(ip,code,table);
46
getch();
}
47
}
if(found==0)
{
code[k]=ip[i];
related[k][0]=k+1;
count[k++]=1;
}
if(found==1)
{
count[f]=count[f]+1;
}
}
}
48
count[i]=vari;
code[i]=temp;
}
}
}
}
49
for(x=0;x<30;++x)
{
related[i][x]=temp[0][x];
}
}
}
}
}
50
}
ct=ct+1;
}
escape:
for(i=0;i<ct;++i)
{
p=related[cnt-2][i]-1;
table[p][z]='1';
}
for(i=0;i<ct1;++i)
{
q=related[cnt-1][i]-1;
table[q][z]='0';
}
count[cnt-2]=count[cnt-1]+count[cnt-2];
count[cnt-1]=0;
for(l=ct,m=0;m<ct1;++l,++m)
{
related[cnt-2][l]=related[cnt-1][m];
related[cnt-1][m]=0;
}
cnt=cnt-1;
sort2(count,related,cnt);
++z;
return(cnt);
}
void rectify(char table[30][30])
{
int i,j,t;
char temp;
for(i=0;i<29;++i)
51
{
for(j=0;j<29;++j)
{
if(table[i][j]=='\0')
{
goto leave;
}
if(table[i][j]=='x')
{
for(t=j;t<29;++t)
{
table[i][t]=table[i][t+1];
}
}
}
leave:;
}
for(i=0;i<29;++i)
{
for(j=0;j<29;++j)
{
if(table[i][j]=='x')
{
rectify(table);
}}}}
52
for(i=0;i<29;++i)
{
if(ip[i]=='\0')
{
return;
}
for(j=0;j<29;++j)
{
if(ip[i]==code[j])
{
for(k=0;k<strlen(table[j]);++k)
{
temp[0][k]=table[j][k];
}
temp[0][k]='\0';
printf("%s ",strrev(temp[0]));
}
}
}
}
TABLE:
-------------
d 1
f 10
a 00
Output : 00 00 00 00 1 1 1 01 01 01 01 01 1 1 1 1 1 1 1
53
EX. NO. 8:
Image compression algorithm
AIM:
To perform image compression using JAVA
ALGORITHM:
1. Get the image to be compressed.
2. Get pixel values of the image using PixelGrabber
3. Store the pixel value in one dimensional array
4. Compress the pixel array using run length encoding
5. This will produced the compressed result
6. End the program
Program
public class image extends Applet{
Image im;
int pixel[];
int pixel1[];
int w;
int h;
public void init(){
try
{
im= getImage(getDocumentBase(),"yy.gif");
MediaTracker mt= new MediaTracker(this);
mt.addImage(im,0);
mt.waitForID(0);
w=im.getWidth(this);
h=im.getHeight(this);
pixel=new int[w*h];
pixel1=new int[w*h];
54
PixelGrabber pg=new PixelGrabber(im,0,0,w,h,pixel,w,h);
pixel1= (int[]) pg.getPixels();
compress cs=new compress();
cs.rle(pixel1);
System.out.println("the image has been compressed using RLE");
//MemoryImageSource mm=new MemoryImageSource(w,h,pixel,w,h);
}
catch(Exception e){}
}
public void paint(Graphics g){
g.drawImage(im,0,0,w,h,this);}
}
SAMPLE OUTPUT
C:\jdk1.3\bin>javac image.java
C:\jdk1.3\bin>appletviewer image.java
the image has been compressed using RLE
55
EX. NO.9:
Animation using any animation software
Frame By Frame
AIM
To perform Frame by Frame animation
ALGORITHM
1. Click a layer name to make it the current layer, and select a frame in the layer where
you want the animation to start.
2. If the frame isn't already a key frame, choose Insert > Key frame to make it one.
3. Create the artwork for the first frame of the sequence.
4. You can use the drawing tools, paste graphics from the Clipboard, or import a file.
5. Click the next frame to the right in the same row and choose Insert > Keyframe, or
right-click (Windows) or Control-click (Macintosh) and choose Insert Key frame from
the context menu.
6. This adds a new key frame whose contents are the same as those of the first key frame.
7. Alter the contents of this frame on the Stage to develop the next increment of the
animation.
8. To complete your frame-by-frame animation sequence, repeat steps 4 and 5 until you've
built the motion you want.
9. To test the animation sequence, choose Control > Play or click the Play button on the
Controller.
56
SAMPLE OUTPUT
57
Create an instance, group, or text block on the Stage.
Drag an instance of a symbol from the Library panel.
3. Create a second key frame where you want the animation to end, and then select the ending
frame (immediately to the left of the second key frame on the Timeline).
4. Do any of the following to modify the instance, group, or text block in the ending frame:
Move the item to a new position.
Modify the item's size, rotation, or skew.
Modify the item's color (instance or text block only).
To tween the color of elements other than instances or text blocks, use shape tweening.
See Tweening shapes.
5. If the Property inspector is not visible, choose Window > Properties.
6. Double-click the ending frame in the Timeline.
7. Select Motion from the Tween pop-up menu in the Property inspector.
8. If you modified the size of the item in step 4, select Scale to tween the size of the selected
item.
9. Drag the arrow next to the Easing value or enter a value to adjust the rate of change between
tweened frames:
To begin the motion tween slowly and accelerate the tween toward the end of the
animation, drag the slider up or enter a negative value between -1 and -100.
To begin the motion tween rapidly and decelerate the tween toward the end of the
animation, drag the slider down or enter a positive value between 1 and 100.
By default, the rate of change between tweened frames is constant. Easing creates a
more natural appearance of acceleration or deceleration by gradually adjusting the rate of
change.
10. To rotate the selected item while tweening, choose an option from the Rotate menu:
Choose none (the default setting) to prevent rotation.
Choose Auto to rotate the object once in the direction requiring the least motion.
Choose Clockwise (CW) or Counterclockwise (CCW) to rotate the object as indicated,
and then enter a number to specify the number of rotations.
Note: The rotation in step 9 is in addition to any rotation you applied to the ending frame in
step 4.
58
11. If you're using a motion path, select Orient to Path to orient the baseline of the tweened
element to the motion path.
12. Select the Sync checkbox in the Property inspector to synchronize the animation of
graphic symbol instances with the main Timeline.
Note: Modify > Frames > Synchronize Symbols and the Sync checkbox both recalculate the
number of frames in a tween to match the number of frames allotted to it in the Timeline.
13. If you're using a motion path, select snap to attach the tweened element to the motion path
by its registration point.
To create a motion tween using the Create Motion Tween command:
1. Select an empty keyframe and draw an object on the Stage, or drag an instance of a symbol
from the Library panel.
Note: In order to create a tween, you must have only one item on the layer.
2. Choose Insert > Create Motion Tween.
If you drew an object in step 1, Flash automatically converts the object to a symbol and
assigns it the name tween1.
3. Click inside the frame where you want the animation to end, and choose Insert > Frame.
4. Move the object, instance, or type block on the Stage to the desired position. Adjust the size
of the element if you want to tween its scale. Adjust the rotation of the element if you want to
tween its rotation. Deselect the object when you have completed adjustments.
A key frame is automatically added to the end of the frame range.
5. Drag the arrow next to the Easing value or enter a value to adjust the rate of change between
tweened frames:
To begin the motion tween slowly and accelerate the tween toward the end of the
animation, drag the slider up or enter a value between -1 and -100.
To begin the motion tween rapidly and decelerate the tween toward the end of the
animation, drag the slider down or enter a positive value between 1 and 100.
By default, the rate of change between tweened frames is constant. Easing creates a
more natural appearance of acceleration or deceleration by gradually adjusting the rate of
change.
6. To rotate the selected item while tweening, choose an option from the Rotate menu:
Choose Auto to rotate the object once in the direction requiring the least motion.
59
Choose Clockwise (CW) or Counterclockwise (CCW) to rotate the object as indicated,
and then enter a number to specify the number of rotations.
Note: The rotation in step 6 is in addition to any rotation you applied to the ending frame in
step 4.
7. If you're using a motion path, select Orient to Path to orient the baseline of the tweened
element to the motion path.
8. Select Synchronize to ensure that the instance loops properly in the main movie.
Use the Synchronize command if the number of frames in the animation sequence
inside the symbol is not an even multiple of the number of frames the graphic instance
occupies in the movie.
9. If you're using a motion path, select snap to attach the tweened element to the motion
path by its registration point.
SAMPLE OUTPUT
60
Shape Tweening in Flash MX
AIM
To perform using Shape tweening
ALGORITHM
By tweening shapes, you can create an effect similar to morphing, making one shape
appear to change into another shape over time. Flash can also tween the location, size, and
color of shapes. Tweening one shape at a time usually yields the best results. If you tween
multiple shapes at one time, all the shapes must be on the same layer.
To apply shape tweening to groups, instances, or bitmap images, you must first break these
elements apart. To apply shape tweening to text, you must break the text apart twice to convert
the text to objects. To control more complex or improbable shape changes, you use shape hints,
which control how parts of the original shape move into the new shape. See Using shape hints.
To tween a shape:
1. Click a layer name to make it the current layer, and create or select a keyframe where
you want the animation to start.
2. Create or place the artwork for the first frame of the sequence. For best results, the
frame should contain only one item (a graphic object or broken-apart group, bitmap,
instance, or text block).
3. Select the keyframe in the Timeline.
4. Choose Window > Properties.
5. In the Property inspector, select Shape from the Tween pop-up menu.
6. Drag the arrow next to the Easing value or enter a value to adjust the rate of change
between tweened frames:
7. To begin the shape tween gradually and accelerate the tween toward the end of the
animation, drag the slider down or enter a negative value between -1 and -100.
8. To begin the shape tween rapidly and decelerate the tween toward the end of the
animation, drag the slider up or enter a positive value between 1 and 100.
9. By default, the rate of change between tweened frames is constant. Easing creates a
more natural appearance of transformation by gradually adjusting the rate of change.
10. Choose an option for Blend:
61
11. Distributive creates an animation in which the intermediate shapes are smoother and
more irregular.
12. Angular creates an animation that preserves apparent corners and straight lines in the
intermediate shapes.
13. Note: Angular is appropriate only for blending shapes with sharp corners and straight
lines. If the shapes you choose do not have corners, Flash reverts to distributive shape
tweening.
14. Create a second keyframe the desired number of frames after the first keyframe.
15. With the second keyframe selected, select the artwork you placed in the first keyframe
and do one of the following:
16. Modify the shape, color, or position of the artwork.
17. Delete the artwork and place new artwork in the second keyframe.
Sample Output
62
Use of Guide Layer in Flash MX
AIM
To create animation using guide layer
ALGORITHM
Motion guide layers let you draw paths along which tweened instances, groups, or text
blocks can be animated. You can link multiple layers to a motion guide layer to have multiple
objects follow the same path. A normal layer that is linked to a motion guide layer becomes a
guided layer.
To create a motion path for a tweened animation:
1. Create a motion-tweened animation sequence as described in Tweening instances,
groups, and type.
2. If you select Orient to Path, the baseline of the tweened element will orient to the
motion path. If you select Snap, the registration point of the tweened element will snap
to the motion path.
3. Do one of the following:
4. Select the layer containing the animation and choose Insert > Motion Guide.
5. Right-click (Windows) or Control-click (Macintosh) the layer containing the animation
and choose Add Motion Guide from the context menu.
6. Flash creates a new layer above the selected layer with a motion guide icon to the left
of the layer name.
7. Use the Pen, Pencil, Line, Circle, Rectangle, or Brush tool to draw the desired path.
8. Snap the center to the beginning of the line in the first frame, and to the end of the line
in the last frame.
9. Note: For best snapping results, drag the symbol by its registration point.
10. To hide the motion guide layer and the line so that only the object's movement is visible
while you work, click in the Eye column on the motion guide layer.
11. The group or symbol follows the motion path when you play the animation.
To link layers to a motion guide layer, do one of the following:
1. Drag an existing layer below the motion guide layer. The layer is indented under the
motion guide layer. All objects on this layer automatically snap to the motion path.
63
2. Create a new layer under the motion guide layer. Objects you tween on this layer are
automatically tweened along the motion path.
3. Select a layer below a motion guide layer. Choose Modify > Layer and select Guided in
the Layer Properties dialog box.
To unlink layers from a motion guide layer:
1. Select the layer you want to unlink.
2. Do one of the following:
3. Drag the layer above the motion guide layer.
4. Choose Modify > Layer and select Normal as the layer type in the Layer Properties
dialog box.
64
Masking in Flash MX
AIM
To perform masking
ALGORITHM
The art that you created on the Shapes layer extends beyond the Stage, well into the
canvass area. Although the area on the canvass won't appear in your published movie, the art
beyond the Stage can be distracting in the authoring environment. While you can erase the part
of the shapes that extend into the canvass, a better solution is to apply a mask over the Stage so
that only the area under the mask—the entire Stage, in this case—remains visible. This way, if
you'd like to return to the shapes to modify them, they will be intact.
1. With the Shapes layer selected, add a new layer to the Timeline and name it Mask.
2. In the toolbox, select the Rectangle tool and draw a rectangle that extends from the
upper left corner of the Stage to the lower right corner.
3. This rectangle is the shape of your mask. Anything under the rectangle will be visible.
4. Right-click (Windows) or Control-click (Macintosh) the Mask layer name in the
Timeline and choose Mask from the context menu.
5. The layer is converted to a mask layer, indicated by a down arrow icon. The layer
immediately below it is linked to the mask layer, and its contents show throughout the
filled area on the mask. The masked layer name is indented, and its icon changes to a
right-pointing arrow. The art on the canvass is no longer visible on the Stage.
6. Mask layers must be locked for the Mask effect to show. To edit the shapes, you can
unlock the Mask and Background Shapes layers. When you finish editing the art, lock
the layers again to invoke masking.
7. Save your file.
SAMPLE OUTPUT
65
EX NO. 10:
Basic operations on image using any image editing software
AIM
To perform basic operations on a image using Photoshop
ALGORITHM
1. Open any image editing software say Photoshop
2. Create a new file
3. Load an image you want to edit
4. Use lasso tool and select the required part in the image
5. Open another file
6. Move the selected part from file1 to file2 using move tool
66
SAMPLE OUTPUT
67
68