Professional Documents
Culture Documents
om
COMPUTER GRAPHICS
.c
CS2405
.re
jin
pa
ul
LAB MANUAL
INDEX
SN
TITLE
PAGE
1C
2
ul
jin
pa
.re
10
8
9
.c
om
1B
SYLLABUS
COMPUTER GRAPHICS LABORATORY
om
CS2405
ul
.c
jin
pa
1) Turbo C
.re
LIST OF EQUIPMENTS:
TOTAL: 45 PERIODS
DATE:
AIM:
To write a C program for Line Drawing Using Bresenham Algorithm.
ul
.c
.re
jin
pa
PROGRAM:
#include<stdio.h>
#include<conio.h>
om
ALGORITHM:
#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;
printf("\n ENTER THE VALUE OF X1:");
scanf("%f",&x1);
scanf("%f",&y1);
.c
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
{
putpixel(x,y,15);
while(e>=0)
ul
{
y=y+1;
e=e-(2*dx);
}
x=x+1;
e=e+(2*dy);
i=i+1;
delay(100);
}while(i<=dx);
closegraph();
getch();
}
om
OUTPUT:
ENTER THE VALUE OF X1 : 600
.re
jin
pa
ul
.c
Ex. No : 1B
Date
AIM:
om
.c
PROGRAM:
#include<stdio.h>
#include<conio.h>
.re
#include<graphics.h>
jin
pa
ul
Step 9 : Assign all operation for circle function and the values.
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;
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();
}
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);
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
.c
om
ul
RESULT:
.re
jin
pa
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).
om
.c
#include<stdio.h>
#include<conio.h>
jin
pa
#include<graphics.h>
ul
PROGRAM
#include<math.h>
void disp();
float x,y;
int xc,yc;
{
.re
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++;
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);
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()
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
.re
OUTPUT
Result:
13
Thus using C++ program implementation of Bresenhams algorithm for line, circle and
.re
jin
pa
ul
.c
om
14
Ex. No. :: 3
Date
om
AIM:
ALGORITHM:
Step 1: Start the program.
ul
.c
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
equation.
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();
om
.c
switch(c)
{
ul
case 1:
rectangle(100,200,300,300);
jin
pa
.re
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);
16
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);
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
.c
om
ORIGINAL OBJECT
.re
jin
pa
ul
ORIGINAL OBJECT
COUNTER CLOCKWISE
ROTATION
CLOCKWISE ROTATION
18
RESULT:
Thus the above programTWO DIMENSIONAL
.re
jin
pa
ul
.c
om
19
Date
AIM:
om
mapping
.c
#include<stdio.h>
#include<graphics.h>
jin
pa
#include<math.h>
ul
#include<conio.h>
float cxl,cxr,cyt,cyb;
code(float ,float);
.re
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");
20
settextstyle(1,0,1);
outtextxy(40,15,"AFTER CLIPPING");
clip(x1,y1,x2,y2);
getch();
closegraph();
}
om
.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;
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
{
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);
out:
rect(cxl,cyb,cxr,cyt);
line(x1,y1,x2,y2);
}
code(float x ,float y)
{
int c=0;
if(x<cxl)
om
if((c&4)==4)
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;
om
.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
SAMPLE INPUT:
200
200
400
400
23
450
SAMPLE OUTPUT:
.re
jin
pa
ul
.c
om
BEFORE CLIPPING
AFTER CLIPPING:
24
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
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
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();
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;
om
ul
100
300
400
500
350
.re
SAMPLE OUTPUT:
jin
pa
200
.c
SAMPLE INPUT:
w
Result :
om
.c
ul
jin
pa
.re
26
27
Ex. No. :: 07
Date
om
.c
AIM:
ALGORITHM:
jin
pa
ul
.re
do-while statement.
Step 6: Similarly, get the draw function for draw the lines.
28
PROGRAM:
om
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
.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();
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;
29
case 2:
scaling();
break;
case 3:
closegraph();
exit(0);
}
om
getch();
}while(ch!=3);
}
.c
void get()
{
ul
int i;
jin
pa
POLYGON:");
scanf("%d%d",&n,&dep);
.re
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
om
VALUE:");
scanf("%f%f",&tx,&ty);
for(i=0; i<n;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
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
om
jin
pa
ul
.c
.re
om
32
RESULT:
.re
jin
pa
ul
.c
om
.c
ul
jin
pa
.re
33
om
.c
ul
jin
pa
.re
34