Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
COMPUTER GRAPHICS
sp
ot
.c
om
CS2405
.e
ee
ex
cl
us
iv
e.
bl
og
LAB MANUAL
INDEX
SN
TITLE
PAGE
1C
2
3
e.
bl
us
iv
og
cl
ex
ee
w
w
10
.e
8
9
sp
ot
.c
om
1B
SYLLABUS
COMPUTER GRAPHICS LABORATORY
CS2405
sp
ot
.c
om
og
e.
bl
8. Composite 3D transformations
TOTAL: 45 PERIODS
ex
cl
us
iv
LIST OF EQUIPMENTS:
ee
1) Turbo C
.e
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
og
.e
ee
ex
cl
us
iv
e.
bl
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
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:
.e
ee
ex
cl
us
iv
e.
bl
og
sp
ot
.c
om
sp
ot
.c
om
e.
bl
og
us
iv
PROGRAM:
#include<stdio.h>
ex
cl
#include<conio.h>
ee
#include<graphics.h>
.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
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
RESULT:
og
.e
ee
ex
cl
us
iv
e.
bl
successfully.
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
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
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
14
Ex. No. :: 3
Date
AIM:
sp
ot
.c
om
og
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
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.
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
us
iv
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
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
ee
ex
cl
us
iv
e.
bl
og
.e
ORIGINAL OBJECT
CLOCKWISE ROTATION
18
RESULT:
Thus the above programTWO DIMENSIONAL
.e
ee
ex
cl
us
iv
e.
bl
og
sp
ot
.c
om
19
Date
AIM:
sp
ot
.c
om
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
float cxl,cxr,cyt,cyb;
code(float ,float);
us
iv
e.
bl
og
#include<math.h>
cl
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
og
line(xr,yt,xl,yt);
e.
bl
line(xl,yt,xl,yb);
us
iv
.e
SAMPLE INPUT:
ee
ex
cl
OUTPUT
23
450
SAMPLE OUTPUT:
AFTER CLIPPING:
.e
ee
ex
cl
us
iv
e.
bl
og
sp
ot
.c
om
BEFORE CLIPPING
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");
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;
}
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
sp
ot
.c
om
AIM:
og
e.
bl
ALGORITHM:
us
iv
cl
ex
Step 3: Create the main function, in that function to initialize graph using
ee
do-while statement.
.e
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
e.
bl
POLYGON:");
us
iv
ex
cl
ee
w
w
.e
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
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
og
sp
ot
.c
om
us
iv
.e
ee
ex
cl
32
sp
ot
.c
om
RESULT:
.e
ee
ex
cl
us
iv
e.
bl
og
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