1
om
COMPUTER GRAPHICS
.c
CS2405
w
w
w
.re
jin pa
ul
LAB MANUAL
INDEX
2
SN
TITLE
PAGE
Line Drawing Using Bresenham Algorithm 1A
Ellipse Drawing Using Bresenham Algorithm
1C 2
ul
Two Dimensional Transformations
jin pa
4
Cohen Sutherland 2D line clipping and Windowing
6
. Three dimensional transformations
.re
7
w
w
10
w
8 9
.c
. Implementation of Line, Circle and ellipse Attributes
3
5
om
Circle Drawing Using Bresenham Algorithm
1B
3
SYLLABUS COMPUTER GRAPHICS LABORATORY
om
CS2405
1. Implementation of Bresenhams Algorithm – Line, Circle, Ellipse. 2. Implementation of Line, Circle and ellipse Attributes Shear. 4. Composite 2D Transformations
ul
5. Cohen Sutherland 2D line clipping and Windowing
.c
3. Two Dimensional transformations - Translation, Rotation, Scaling, Reflection,
6. Sutherland – Hodgeman Polygon clipping Algorithm
jin pa
7. Three dimensional transformations - Translation, Rotation, Scaling 8. Composite 3D transformations
9. Drawing three dimensional objects and Scenes 10. Generating Fractal images
1) Turbo C
.re
LIST OF EQUIPMENTS:
TOTAL: 45 PERIODS
2) Visual C++ with OPENGL
w
w
w
3) Any 3D animation software like 3DSMAX, Maya, Blender
LINE DRAWING USING BRESENHAM ALGORITHM EX.NO: 1A
4
DATE:
AIM: To write a C program for Line Drawing Using Bresenham Algorithm.
Step 1 : Start the program. Step 2 : Declare the necessary variables. Step 3 : Initialize the graph using dx, dy, gd, gm. Step 5 : Similarly, absolute values to dx, dy.
ul
Step 6 : put pixel and set 15 to pixel position.
.c
Step 4 : Assign the values of x1, y1 to x,y respectively.
Step 7 : Using do-while loop, put e,x,y,I values.
w
w
w
.re
jin pa
Step 8 : Stop the program.
PROGRAM: #include #include
om
ALGORITHM:
5
#include #include #include void main() { float x,y,x1,y1,x2,y2,dx,dy,e; int i,gd=DETECT,gm; printf("\n ENTER THE VALUE OF X1:"); scanf("%f",&x1); scanf("%f",&y1);
.c
printf("\n ENTER THE VALUES OF Y1:");
om
clrscr();
scanf("%f%f",&x2,&y2); initgraph(&gd,&gm," ");
jin pa
dx=abs(x2-x1); dy=abs(y2-y1); x=x1; y-y1;
e=2*(dy-dx);
.re
i=1; do {
w
putpixel(x,y,15);
w
while(e>=0)
w
ul
printf("\n ENTER THE VALUES OF X2 AND Y2:");
{ y=y+1; e=e-(2*dx); } x=x+1; e=e+(2*dy); i=i+1; delay(100);
}while(i<=dx); closegraph();
6
getch(); }
om
OUTPUT: ENTER THE VALUE OF X1 : 600
w
w
w
.re
jin pa
ul
ENTER THE VALUE OF X2 AND Y2 : 300 400
.c
ENTER THE VALUE OF Y1 : 400
Ex. No : 1B Date AIM:
:
CIRCLE DRAWING USING BRESENHAM ALGORITHM
7
To write a C program for Circle Drawing Using Bresenham Algorithm. ALGORITHM: Step 1 : Start the program. Step 2 : Declare the necessary variables. Step 3 : Create the function for Circle. Step 5 : Initialize the graph with gd, gm and assign y<-radius. Step 6 : Start the circle function and p<- 1- radius. Step 7 : Check the while loop until the condition is satisfied.
om
Step 4 : Enter the radius and center values.
.c
Step 8 : Check the if –else condition until the condition is satisfied.
PROGRAM: #include #include
.re
#include
jin pa
Step 10 : Stop the Program.
ul
Step 9 : Assign all operation for circle function and the values.
void circlefun(int xcenter, int ycenter,int x,int y); {
w
void main()
int x=0,radius,xcenter,ycenter;
w
int y,p,gd=DETECT,gm; clrscr();
w
initgraph(&gd,&gm," "); printf("\n ENTER THE XCENTER &YCENTER:"); scanf("%d%d",&xcenter,&ycenter); printf("\n ENTER THE RADIUS:"); scanf("%d",&radius); y=radius; circlefun(xcenter,ycenter,x,y); p=1-radius;
8
while(x
om
y=y-1; } if(p<0) else p=p+2*(x-y)+1;
ul
circlefun(xcenter,ycenter,x,y);
.c
p=p+2*x+1;
}
jin pa
getch(); }
void circlefun(int xcenter,int ycenter,int x,int y) {
putpixel(xcenter+x,ycenter+y,1);
.re
putpixel(xcenter-x,ycenter+y,1); putpixel(xcenter+x,ycenter-y,1); putpixel(xcenter-x,ycenter-y,1);
w
putpixel(xcenter+y,ycenter+x,1); putpixel(xcenter-y,ycenter+x,1);
w
putpixel(xcenter+y,ycenter-x,1); putpixel(xcenter-y,ycenter-x,1);
w
}
OUTPUT: ENTER THE X CENTER AND Y CENTER : 500 250
9
.c
om
ENTER THE RADIUS : 70
ul
RESULT:
Thus the above program “CIRCLE DRAWING USING BREASENHAM” is executed
w
.re
jin pa
successfully.
w
ELLIPSE DRAWING USING BRESENHAM ALGORITHM
w
Ex. No : 1C Date
:
AIM: To write a C program for Circle Drawing Using Bresenham Algorithm.
10
ALGORITHM: Step 1: Start the program. Step 2: Initialize the variables. Step 3: Call the initgraph() function. Step 4: Get the initialize points P1,P2. Step 5: Get the values of Co-Ordinates of the ellipse (x,y).
om
Step 6: Enter the coordinates a,b of the ellipse . Step 7: Display the output.
.c
Step 8: Stop the program
#include #include
jin pa
#include
ul
PROGRAM
#include void disp(); float x,y; int xc,yc; {
.re
void main()
int gd=DETECT,gm;
w
int a,b;
float p1,p2;
w
clrscr();
w
initgraph(&gd,&gm,""); scanf("%d%d",&xc,&yc); scanf("%d%d",&a,&b); x=0;y=b; disp(); p1=(b*b)-(a*a*b)+(a*a)/4; while((2.0*b*b*x)<=(2.0*a*a*y)) { x++;
11
if(p1<=0) p1=p1+(2.0*b*b*x)+(b*b); else { y--; p1=p1+(2.0*b*b*x)+(b*b)-(2.0*a*a*y); }
om
disp(); x=-x; disp(); x=-x;
.c
} y=0; disp();
jin pa
p2=(a*a)+2.0*(b*b*a)+(b*b)/4;
ul
x=a;
while((2.0*b*b*x)>(2.0*a*a*y)) { y++; if(p2>0) else {
.re
p2=p2+(a*a)-(2.0*a*a*y);
w
x--;
p2=p2+(2.0*b*b*x)-(2.0*a*a*y)+(a*a);
w
}
disp();
w
y=-y;
disp(); y=-y; } getch(); closegraph(); } void disp()
12
{ putpixel(xc+x,yc+y,10); putpixel(xc-x,yc+y,10); putpixel(xc+x,yc-y,10); putpixel(xc+x,yc-y,10); }
om
INPUT Ellipse Drawing Algorithm Enter the co-ordinates
.c
Xc = 200 Yc = 200
ul
A = 100
jin pa
B = 70
w
w
.re
OUTPUT
w
Result:
13
Thus using C++ program implementation of Bresenham’s algorithm for line, circle and
w
w
w
.re
jin pa
ul
.c
om
ellipse drawing is done.
TWO DIMENSIONAL TRANSFORMATIONS
14 Ex. No. :: 3 Date
om
AIM:
ALGORITHM: Step 1: Start the program.
ul
.c
To write a C program for Two Dimensional Transformations.
jin pa
Step 2: Declare the necessary variables and initialize the graph, gd, gm. Step 3: Use do-while loop and declare the function clear device. Step 4: Create four cases translation, scaling , rotation and exit. Step 5: In case 1 enter the translation values and print the translation object.
.re
Step 6: In case 2 enter the scaling values and print the scaling object. Step 7: In case 3 enter the rotaion values and print rotation object. Step 8: Clockwise rotation and counter clockwise rotation use the same
w
equation.
w
Step 9: Stop the program.
w
PROGRAM:
#include #include #include #include #include void main() {
15
int x,y,c; float a; int gd=DETECT,gm; initgraph(&gd,&gm," "); do { cleardevice();
om
printf("1.TRANSLATION 2.SCALING 3.ROTATION 4.EXIT"); printf("\n ENTER YR CHOICE:"); scanf("%d",&c);
.c
switch(c) {
ul
case 1:
rectangle(100,200,300,300);
jin pa
printf("\nENTER THE TX AND TY VALUE:"); scanf("%d%d",&x,&y);
outtextxy(130,180," ORIGINAL OBJECT"); rectangle(100+x,200+y,300+x,300+y);
.re
outtextxy(130+x,180," TRANSLATION OBJECT");
break;
w
w
case 2:
rectangle(50,100,150,150); printf("\n ENTER THE SX AND SY VALUE:"); scanf("%d%d",&x,&y);
w
outtextxy(38,85,"ORIGINAL OBJECT"); rectangle(50*x,100*y,150*x,150*y); outtextxy(250,250,"SCALING OBJECT"); break; case 3: printf("\n ENTER THE X AND Y VALUE:"); scanf("%d%d",&x,&y);
16
printf("\n ENTER THE ANGLE;"); scanf("%d",&c); a=(3.14*c)/180; cleardevice(); line(100,100,200,100); outtextxy(210,100,"ORIGINAL OBJECT"); line(100*cos(a)-100*sin(a)+x*(1-cos(a))
om
+y*sin(a),100*sin(a)+100*cos(a)+y*(1-cos(a)) -x*sin(a),200*cos(a)-100*sin(a)+x*(1-cos(a))
+y*sin(a),200*sin(a)+100*cos(a)+y*(1-cos(a)) -x*sin(a));
.c
outtextxy(110,75,"COUNTER CLOCKWISE ROTATION");
ul
line(100*cos(a)+100*sin(a)+x*(1-cos(a)) -y*sin(a),-100*sin(a)+100*cos(a)+y*(1-cos(a))
jin pa
+x*sin(a),200*cos(a)+100*sin(a)+x*(1-cos(a))
-y*sin(a),-200*sin(a)+100*cos(a)+y*(1-cos(a)) +x*sin(a));
outtextxy(110,150,"CLOCKWISE ROTATION"); getch();
.re
break;
case 4:
exit(0);
w
}
getch();
w
}while(c!=4);
w
}
17
OUTPUT: 1.TRANSLATION 2. SCALING 3. ROTATION 4. EXIT ENTER YOUR OPTION : 1 ENTER THE TX AND TY VALUE : 150 150 TRANSLATION OBJECT
.c
om
ORIGINAL OBJECT
ENTER YOUR OPTION : 2
.re
jin pa
ENTER YOUR SX AND SY VALUE : 2 2
ul
1. TRANSLATION 2.SCALING 3. ROTATION 4. EXIT
1. TRANSLATION 2.SCALING 3. ROTATION 4. EXIT
w
ENTER YOUR OPTION : 3
ENTER THE X AND Y VALUE :100 100
w
w
ENTER THE ANGLE :90
ORIGINAL OBJECT
COUNTER CLOCKWISE ROTATION
CLOCKWISE ROTATION
18
RESULT: Thus the above program”TWO DIMENSIONAL
w
w
w
.re
jin pa
ul
.c
om
TRANSFORMATIONS” Is executed successfully.
COHEN SUTHERLAND 2D LINE CLIPPING AND WINDOWING Ex. No : 5
19
Date
:
AIM:
To implement cohen-sutherland 2d clipping and window-view port
om
mapping
COHEN-SUTHERLAND 2D LINE CLIPPING
.c
#include #include
jin pa
#include
ul
#include
float cxl,cxr,cyt,cyb; code(float ,float);
void clip(float ,float,float,float);
void rect(float ,float,float,float); {
.re
main()
float x1,y1,x2,y2; int g=0,d;
w
initgraph(&g,&d,"c:\\tc\\bin");
w
settextstyle(1,0,1); outtextxy(40,15,"BEFORE CLIPPING");
w
printf("\n Please Enter Left,Bottom,Right,Top Of Clip Window"); scanf("%f%f%f%f",&cxl,&cyb,&cxr,&cyt); rect(cxl,cyb,cxr,cyt); getch(); printf("\n Enter The Line Coordinate"); scanf("%f%f%f%f",&x1,&y1,&x2,&y2); line(x1,y1,x2,y2); getch(); cleardevice();
20
settextstyle(1,0,1); outtextxy(40,15,"AFTER CLIPPING"); clip(x1,y1,x2,y2); getch(); closegraph(); }
om
void clip(float x1,float y1,float x2,float y2) { int c,c1,c2; float x,y;
.c
c1=code(x1,y1); c2=code(x2,y2);
jin pa
ul
getch();
while((c1!=0)||(c2!=0)) {
if((c1&c2)!=0)
.re
goto out; c=c1;
w
if(c==0)
c=c2;
w
w
if((c&1)==1) {
y=y1+(y2-y1)*(cxl-x1); x=cxl; } else if((c&2)==2) { y=y1+(y2-y1)*(cxl-x1)/(x2-x1); x=cxr; }
21
else if((c&8)==8) { x=x1+(x2-x1)*(cyb-y1)/(y2-y1); y=cyb; } else { x=x1+(x2-x1)*(cyt-y1)/(y2-y1); y=cyt;
.c
} if(c==c1)
ul
{ x1=x;
jin pa
y1=y;
c1=code(x,y);
} else {
.re
x2=x; y2=y;
c2=code(x,y);
w
}
w
}
w
out:
rect(cxl,cyb,cxr,cyt); line(x1,y1,x2,y2);
} code(float x ,float y) { int c=0; if(x
om
if((c&4)==4)
c=1;
22
else if(x>cxr)
c=2;
if(y
c=c|8;
if(y>cyt)
c=c|4;
else else return c;
om
}
void rect(float xl,float yb,float xr,float yt)
.c
{ line(xr,yb,xr,yt); line(xr,yt,xl,yt);
jin pa
line(xl,yt,xl,yb);
ul
line(xl,yb,xr,yb);
}
.re
OUTPUT
w
SAMPLE INPUT:
w
Please Enter Left , Bottom , Right , Top Of The Clip window
w
200 200 400 400
Please Enter The Line Coordinates (X1, Y1, X2, Y2) 150 300 400
23
450
SAMPLE OUTPUT:
.re
jin pa
ul
.c
om
BEFORE CLIPPING
w
w
w
AFTER CLIPPING:
Windowing To Viewport Mapping
24
#include #include #include #include main() {
om
float sx,sy; int w1,w2,w3,w4,x1,x2,x3,x4,y1,y2,y3,y4,v1,v2,v3,v4; int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi");
.c
printf("Enter The Coordinate x1,y1,x2,y2,x3,y3\n"); cleardevice(); w2=5; w3=635; w4=465;
jin pa
w1=5;
ul
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
rectangle(w1,w2,w3,w4); line(x1,y1,x2,y2);
.re
line(x2,y2,x3,y3); line(x3,y3,x1,y1); getch();
w
v1=425; v2=75;
w
v3=550;
w
v4=250; sx=(float)(v3-v1)/(w3-w1); sy=(float)(v4-v2)/(w4-w2); rectangle(v1,v2,v3,v4); x1=v1+floor(((float)(x1-w1)*sx)+.5); x2=v1+floor(((float)(x2-w1)*sx)+.5); x3=v1+floor(((float)(x3-w1)*sx)+.5); y1=v2+floor(((float)(y1-w2)*sy)+.5); y2=v2+floor(((float)(y2-w2)*sy)+.5);
25
y3=v2+floor(((float)(y3-w2)*sy)+.5); line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); getch(); return 0;
om
}
Enter The Coordinate x1,y1,x2,y2,x3,y3
ul
100 300 400 500 350
w
w
.re
SAMPLE OUTPUT:
jin pa
200
w
.c
SAMPLE INPUT:
w
w Result :
om
.c
ul
jin pa
.re
w
26
27
Ex. No. :: 07 Date
om
TRANSLATION AND SCALING USING 3D
.c
AIM:
ALGORITHM:
jin pa
ul
To write a C program for Translation and Scaling Using 3D Method.
Step 1: Start the program.
Step 2: Declare the necessary variables, with member functions. Step 3: Create the main function, in that function to initialize graph using
.re
do-while statement.
Step 4: Using Switch statement for translation, scaling, exit. Step 5: Using get function for getting the values.
w
Step 6: Similarly, get the draw function for draw the lines.
w
w
Step 7: Stop the program.
28
PROGRAM:
om
#include #include #include #include
.c
int n,dep; float x[10],y[10],nx[10],ny[10];
ul
void translation(); void rotation();
jin pa
void scaling(); void draw(float x[],float y[]); void get(); void main() {
.re
int gd=DETECT,gm,i,ch; initgraph(&gd,&gm," "); get();
w
do
w
w
{
cleardevice(); draw(x,y); printf("\n 1.TRANSLATION 2.SCALING 3.EXIT"); printf("\n ENTER UR CHOICE:"); scanf("%d",&ch); switch(ch) { case 1: translation(); break;
29
case 2: scaling(); break; case 3: closegraph(); exit(0); }
om
getch(); }while(ch!=3); }
.c
void get() {
ul
int i;
printf("\n ENTER THE NO.OF SIDES& DEPTH OF THE
jin pa
POLYGON:");
scanf("%d%d",&n,&dep);
printf("n ENTER THE CO ORDINATES\n"); for(i=0;i
.re
printf("\n ENTER THE X%d&Y%d VALUE:"); scanf("%f%f",&x[i],&y[i]);
}
w
}
void draw(float x[],float y[])
w
{
w
int i; for(i=0;i
30
} void translation() { int i,ch; float tx,ty; draw(x,y); printf("ENTER THE TRANSLATIONFACTOR TX &TY
om
VALUE:"); scanf("%f%f",&tx,&ty); for(i=0; i
ny[i]=y[i]+ty; } } void scaling() { int i,ch; float sx,sy;
.re
draw(x,y);
jin pa
draw(nx,ny);
ul
nx[i]=x[i]+tx;
.c
{
printf("ENTER THE SCALING FACTOR SX &SY
w
scanf("%f%f",&sx,&sy); for(i=0;i
w
w
{
nx[i]=x[i]*sx; ny[i]=y[i]*sy;
} draw(nx,ny);
}
VALUE:");
31
OUTPUT: ENTER THE NO OF SIDES & DEPTH OF THE POLYGON : 4 10 ENTER THE CO-ORDINATES : ENTER THE X1 AND Y1 VALUES : 200 10 ENTER THE X2 AND Y2 VALUES : 250 10
om
ENTER THE X3 AND Y3 VALUES : 250 60
jin pa
1. TRANSLATION 2. SCALING 3. EXIT
ul
.c
ENTER THE X4 AND Y4 VALUES : 200 60
ENTER YOUR CHOICE : 1
w
w
.re
ENTER THE TRANSLATION FACTORS TX AND TY VALUES : 100 0
w
1. TRANSLATION 2. SCALING 3. EXIT ENTER YOUR CHOICE : 2 ENTER THE SCALING FACTORS SX AND SY VALUES : 2 2
om
32
RESULT:
w
w
w
.re
jin pa
ul
.c
Thus the above program TRANSLATION AND SCALING USING 3D” Is executed successfully.
w
w
om
.c
ul
jin pa
.re
w
33
w
w
om
.c
ul
jin pa
.re
w
34