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