Vous êtes sur la page 1sur 34

1

COMPUTER GRAPHICS
sp
ot
.c
om

CS2405

.e

ee

ex

cl

us
iv

e.
bl

og

LAB MANUAL

INDEX

SN

TITLE

PAGE

Line Drawing Using Bresenham Algorithm


1A

1C
2
3

Circle Drawing Using Bresenham Algorithm


Ellipse Drawing Using Bresenham Algorithm

. Implementation of Line, Circle and ellipse Attributes

Two Dimensional Transformations

e.
bl

Cohen Sutherland 2D line clipping and Windowing

us
iv

og

cl

ex

. Three dimensional transformations

ee

w
w

10

.e

8
9

sp
ot
.c
om

1B

SYLLABUS
COMPUTER GRAPHICS LABORATORY
CS2405

1. Implementation of Bresenhams Algorithm Line, Circle, Ellipse.


2. Implementation of Line, Circle and ellipse Attributes
Shear.
4. Composite 2D Transformations
5. Cohen Sutherland 2D line clipping and Windowing
6. Sutherland Hodgeman Polygon clipping Algorithm

sp
ot
.c
om

3. Two Dimensional transformations - Translation, Rotation, Scaling, Reflection,

og

7. Three dimensional transformations - Translation, Rotation, Scaling

e.
bl

8. Composite 3D transformations

9. Drawing three dimensional objects and Scenes

TOTAL: 45 PERIODS

ex

cl

us
iv

10. Generating Fractal images

LIST OF EQUIPMENTS:

ee

1) Turbo C

.e

2) Visual C++ with OPENGL

3) Any 3D animation software like 3DSMAX, Maya, Blender

LINE DRAWING USING BRESENHAM ALGORITHM


EX.NO: 1A

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.

sp
ot
.c
om

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.

og

Step 7 : Using do-while loop, put e,x,y,I values.

.e

ee

ex

cl

us
iv

e.
bl

Step 8 : Stop the program.

PROGRAM:
#include<stdio.h>
#include<conio.h>

#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);
scanf("%f",&y1);

sp
ot
.c
om

printf("\n ENTER THE VALUES OF Y1:");

printf("\n ENTER THE VALUES OF X2 AND Y2:");


scanf("%f%f",&x2,&y2);

og

initgraph(&gd,&gm," ");

e.
bl

dx=abs(x2-x1);
dy=abs(y2-y1);

us
iv

x=x1;
y-y1;

ex
ee

i=1;

.e

do
{

cl

e=2*(dy-dx);

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();

getch();
}

OUTPUT:

ENTER THE VALUE OF Y1 : 400

.e

ee

ex

cl

us
iv

e.
bl

og

ENTER THE VALUE OF X2 AND Y2 : 300 400

sp
ot
.c
om

ENTER THE VALUE OF X1 : 600

CIRCLE DRAWING USING BRESENHAM ALGORITHM


Ex. No : 1B
Date
AIM:

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.

sp
ot
.c
om

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.

e.
bl

og

Step 10 : Stop the Program.

us
iv

PROGRAM:
#include<stdio.h>

ex

cl

#include<conio.h>

ee

#include<graphics.h>

void circlefun(int xcenter, int ycenter,int x,int y);

.e

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;

while(x<y)
{
if(p<0)
x=x+1;
else
{
x=x+1;
y=y-1;
}
p=p+2*x+1;
else
p=p+2*(x-y)+1;
circlefun(xcenter,ycenter,x,y);

og

sp
ot
.c
om

if(p<0)

e.
bl

getch();
}

us
iv

void circlefun(int xcenter,int ycenter,int x,int y)


{

ex

cl

putpixel(xcenter+x,ycenter+y,1);

ee

putpixel(xcenter-x,ycenter+y,1);
putpixel(xcenter+x,ycenter-y,1);

.e

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

sp
ot
.c
om

ENTER THE RADIUS : 70

RESULT:

og

Thus the above program CIRCLE DRAWING USING BREASENHAM is executed

.e

ee

ex

cl

us
iv

e.
bl

successfully.

ELLIPSE DRAWING USING BRESENHAM ALGORITHM

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).
Step 6: Enter the coordinates a,b of the ellipse .
Step 7: Display the output.

sp
ot
.c
om

Step 8: Stop the program

PROGRAM
#include<stdio.h>

og

#include<conio.h>

e.
bl

#include<graphics.h>
#include<math.h>

us
iv

void disp();
float x,y;

ex

cl

int xc,yc;

ee

void main()
{

.e

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++;

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();

sp
ot
.c
om

x=-x;
}
x=a;
y=0;

og

disp();
while((2.0*b*b*x)>(2.0*a*a*y))

us
iv

e.
bl

p2=(a*a)+2.0*(b*b*a)+(b*b)/4;

y++;

ex

cl

if(p2>0)

ee

p2=p2+(a*a)-(2.0*a*a*y);
else

x--;

.e

p2=p2+(2.0*b*b*x)-(2.0*a*a*y)+(a*a);

}
disp();
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);
}
INPUT
Ellipse Drawing Algorithm

sp
ot
.c
om

Enter the co-ordinates


Xc = 200
Yc = 200
A = 100

e.
bl

og

B = 70

.e

ee

ex

cl

us
iv

OUTPUT

Result:

13

Thus using C++ program implementation of Bresenhams algorithm for line, circle and

.e

ee

ex

cl

us
iv

e.
bl

og

sp
ot
.c
om

ellipse drawing is done.

TWO DIMENSIONAL TRANSFORMATIONS

14
Ex. No. :: 3
Date

AIM:

sp
ot
.c
om

To write a C program for Two Dimensional Transformations.


ALGORITHM:

og

Step 1: Start the program.

Step 2: Declare the necessary variables and initialize the graph, gd, gm.

e.
bl

Step 3: Use do-while loop and declare the function clear device.

us
iv

Step 4: Create four cases translation, scaling , rotation and exit.


Step 5: In case 1 enter the translation values and print the translation

cl

object.

ex

Step 6: In case 2 enter the scaling values and print the scaling object.

ee

Step 7: In case 3 enter the rotaion values and print rotation object.

.e

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()
{

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:");

sp
ot
.c
om

scanf("%d",&c);
switch(c)
{
case 1:

og

rectangle(100,200,300,300);

e.
bl

printf("\nENTER THE TX AND TY VALUE:");


scanf("%d%d",&x,&y);

us
iv

outtextxy(130,180," ORIGINAL OBJECT");


rectangle(100+x,200+y,300+x,300+y);
OBJECT");

ee

ex

cl

outtextxy(130+x,180," TRANSLATION
break;

.e

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);

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))

sp
ot
.c
om

-x*sin(a));

outtextxy(110,75,"COUNTER CLOCKWISE
ROTATION");

line(100*cos(a)+100*sin(a)+x*(1-cos(a))

og

-y*sin(a),-100*sin(a)+100*cos(a)+y*(1-cos(a))

e.
bl

+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))

us
iv

+x*sin(a));

outtextxy(110,150,"CLOCKWISE ROTATION");

ex

cl

getch();

ee

break;
case 4:

.e

exit(0);

getch();

}while(c!=4);
}

17

OUTPUT:
1.TRANSLATION 2. SCALING 3. ROTATION 4. EXIT
ENTER YOUR OPTION : 1
ENTER THE TX AND TY VALUE : 150 150
TRANSLATION OBJECT

sp
ot
.c
om

ORIGINAL OBJECT

1. TRANSLATION 2.SCALING 3. ROTATION 4. EXIT


ENTER YOUR OPTION : 2

ee

ex

cl

us
iv

e.
bl

og

ENTER YOUR SX AND SY VALUE : 2 2

.e

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

18

RESULT:
Thus the above programTWO DIMENSIONAL

.e

ee

ex

cl

us
iv

e.
bl

og

sp
ot
.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


mapping

sp
ot
.c
om

COHEN-SUTHERLAND 2D LINE CLIPPING

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
float cxl,cxr,cyt,cyb;
code(float ,float);

us
iv

void clip(float ,float,float,float);

e.
bl

og

#include<math.h>

cl

void rect(float ,float,float,float);

ex

main()

ee

.e

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();

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;

sp
ot
.c
om

float x,y;
c1=code(x1,y1);
c2=code(x2,y2);

us
iv

while((c1!=0)||(c2!=0))

e.
bl

og

getch();

ex

cl

if((c1&c2)!=0)

ee

goto out;
c=c1;

.e

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;
}

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);

sp
ot
.c
om

y=cyt;
}
if(c==c1)
{

og

x1=x;
c1=code(x,y);

us
iv

e.
bl

y1=y;

else

ex

cl

ee

x2=x;
y2=y;

.e

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;

22

else
if(x>cxr)

c=2;

if(y<cyb)

c=c|8;

if(y>cyt)

c=c|4;

else
else
return c;
}

sp
ot
.c
om

void rect(float xl,float yb,float xr,float yt)


{
line(xl,yb,xr,yb);
line(xr,yb,xr,yt);

og

line(xr,yt,xl,yt);

e.
bl

line(xl,yt,xl,yb);

us
iv

.e

SAMPLE INPUT:

ee

ex

cl

OUTPUT

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

23

450

SAMPLE OUTPUT:

AFTER CLIPPING:

.e

ee

ex

cl

us
iv

e.
bl

og

sp
ot
.c
om

BEFORE CLIPPING

Windowing To Viewport Mapping

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;

sp
ot
.c
om

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();

og

w1=5;

e.
bl

w2=5;
w3=635;

us
iv

w4=465;

ex

line(x1,y1,x2,y2);

cl

rectangle(w1,w2,w3,w4);

ee

line(x2,y2,x3,y3);
line(x3,y3,x1,y1);

.e

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);

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;
}

Enter The Coordinate x1,y1,x2,y2,x3,y3


100

og

200

sp
ot
.c
om

SAMPLE INPUT:

e.
bl

300
400

us
iv

500

ex

cl

350

.e

ee

SAMPLE OUTPUT:

w
.e

e.
bl

Result :

us
iv

cl

ex

ee

og

sp
ot
.c
om

26

27

Ex. No. :: 07
Date

TRANSLATION AND SCALING USING 3D

sp
ot
.c
om

AIM:

og

To write a C program for Translation and Scaling Using 3D Method.

e.
bl

ALGORITHM:

us
iv

Step 1: Start the program.

cl

Step 2: Declare the necessary variables, with member functions.

ex

Step 3: Create the main function, in that function to initialize graph using

ee

do-while statement.

.e

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.

28

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>

sp
ot
.c
om

#include<math.h>
int n,dep;
float x[10],y[10],nx[10],ny[10];
void translation();

og

void scaling();

e.
bl

void rotation();
void draw(float x[],float y[]);

us
iv

void get();
void main()

ex

cl

ee

int gd=DETECT,gm,i,ch;

do

get();

.e

initgraph(&gd,&gm," ");

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);
}
getch();
}while(ch!=3);

sp
ot
.c
om

}
void get()
{
int i;

og

printf("\n ENTER THE NO.OF SIDES& DEPTH OF THE


scanf("%d%d",&n,&dep);

e.
bl

POLYGON:");

us
iv

printf("n ENTER THE CO ORDINATES\n");


for(i=0;i<n;i++)

ex

cl

ee

printf("\n ENTER THE X%d&Y%d VALUE:");


scanf("%f%f",&x[i],&y[i]);

w
w

.e

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);

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++)

sp
ot
.c
om

{
nx[i]=x[i]+tx;
ny[i]=y[i]+ty;
}

og

draw(nx,ny);

e.
bl

}
void scaling()

us
iv

{
int i,ch;

ex

cl

float sx,sy;

ee

draw(x,y);

.e

printf("ENTER THE SCALING FACTOR SX &SY

scanf("%f%f",&sx,&sy);

for(i=0;i<n;i++)
{
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
ENTER THE X3 AND Y3 VALUES : 250 60

e.
bl

1. TRANSLATION 2. SCALING 3. EXIT

og

sp
ot
.c
om

ENTER THE X4 AND Y4 VALUES : 200 60

us
iv

ENTER YOUR CHOICE : 1

.e

ee

ex

cl

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

32

sp
ot
.c
om

RESULT:

.e

ee

ex

cl

us
iv

e.
bl

og

Thus the above program TRANSLATION AND


SCALING USING 3D Is executed successfully.

w
.e

e.
bl

us
iv

cl

ex

ee

og

sp
ot
.c
om

33

w
.e

e.
bl

us
iv

cl

ex

ee

og

sp
ot
.c
om

34

Vous aimerez peut-être aussi