Upload
dhana210
View
26
Download
6
Embed Size (px)
DESCRIPTION
Lab manual Graphics
Citation preview
DEPARTMENTOF
COMPUTER SCIENCE AND ENGINEERING
GRAPHICS AND MULTIMEDIA LABORATORY(CS651)
AKOIJAM RANJAN SINGH1117137
A Laboratory Record submitted in partial fulfillment of the requirement of Computer Programming Laboratory
of VI th B.Tech(CSE).
March 2014
CertificateThis is to certify that the record titled Graphics and multimedia (CS651) is a
bonafide record of work done by AKOIJAM RANJAN SINGH in partial
fulfillment of requirement of VIth B.Tech(CSE) during the year 2013-14.
HEAD OF THE DEPARTMENT FACULTY-IN-CHARGE
EXAMINER 1:
ASSOCIATE DEAN EXAMINER 2:
Name : ___________________
Register Number : ___________________
Examination Center : ___________________
Date of Examination : ___________________
Acknowledgement
We take pleasure in acknowledging the assistance and contribution of a number of
individuals to this effort. We are deeply indebted to each faculty for their support and
encouragement.
We thank Christ University, the institution that has been working towards its noble mission
“Christ University is the nurturing ground for an individual’s holistic development to
make effective contribution to the society in a dynamic environment”
We are grateful to our Rev. Fr. Dr. Thomas C Mathew, Vice Chancellor,Rev. Fr. Dr. V. M
Abraham, Pro Vice Chancellor and Rev. Fr. Benny Thomas, Director of CUFE for their
moral support and providing all facilities for the development of this Laboratory.
We express our sincere thanks to Dr. Iven Jose, Associate Dean and Prof. K.
Balachandran, Head of Department for providing all facilities for carrying out this
assignment.
We are grateful to ___________________, Faculty in-charge in guiding us for the
completion of the laboratory experiments.
We also extend our sincere thanks to all the Laboratory Assistants for maintaining and
supporting the laboratory to complete the experiments.
Our acknowledgment would be incomplete if we did not thank our parents who encouraged
us in all we did and were an infinite source of support. Also, we thank our friends who
inspired us with ideas and helped us our in tight spots.
Table of ContentsPRGM
NO.PROGRAM NAME
PAGE NO.
1 a. Write a program to implement Bresenham’s line algorithm. 1
1 b. Write a program to implement mid-point circle and ellipse drawing algorithm. 2-5
2 Write a program to perform 2D transformations such asi) Rotation ii) Translation iii) Scaling iv) Reflection v) Shearing
6-11
3 a. Write a program to implement window to viewport mapping. 12
3 b. Write a program to implement point clipping. 13
4 Write a program to implement Cohen Sutherland line clipping algorithm. 14
5 Write a program to visualize projection of 3D image 17
Graphics and Multimedia Laboratory (CS651)
1 . a) Write a program to implement Bresenham’s line algorithm.
Department of Computer Science and Engineering, Christ University 1
Graphics and Multimedia Laboratory (CS651)
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\Turbo\\TC\\BGI");
int x1=100,y1=50,x2=150,y2=180;
int dx=abs(x2-x1);
int dy=abs(y2-y1);
int p=2*dy-dx;
int Tdy=2*dy;
int Tdydx=(2*(dy-dx));
int x=x1;
int y=y1;
initgraph(&gdriver,&gmode,"..\\BGI");
while(x<=x2)
{
Department of Computer Science and Engineering, Christ University 2
Graphics and Multimedia Laboratory (CS651)
if(p<0)
p=p+Tdy;
else
{
y++;
p=p+Tdydx;
}
x++;
putpixel(x,y,GREEN);
}
getch();
closegraph();
}
1 b) Write a program to implement mid-point circle and ellipse drawing algorithm.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<process.h>
void plot(int xc,int yc,int x, int y)
{
Department of Computer Science and Engineering, Christ University 3
Graphics and Multimedia Laboratory (CS651)
putpixel(xc-x,yc-y,100-10*(x-y));
putpixel(xc+x,yc+y,100-10*(x-y));
putpixel(xc-x,yc+y,100-10*(x-y));
putpixel(xc+x,yc-y,100-10*(x-y));
putpixel(xc-y,yc-x,100-10*(x-y));
putpixel(xc-y,yc+x,100-10*(x-y));
putpixel(xc+y,yc+x,100-10*(x-y));
putpixel(xc+y,yc-x,100-10*(x-y));
}
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\Turbo\\TC\\BGI");
int xc,yc,x,y,r,p;
printf("\n\nENTER xc , yc , r :: ");
scanf("%d%d%d",&xc,&yc,&r);
x=0;
y=r;
p=(5/4)-r;
plot(xc,yc,x,y);
while(x<y)
{
x++;
if(p<0)
{
p=p+2*x+1;
}
else
{ y--;
p=p+2*x-2*y+1;
Department of Computer Science and Engineering, Christ University 4
Graphics and Multimedia Laboratory (CS651)
}
plot(xc,yc,x,y);
}
getch();
}
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"..bgi");
float cx,cy,rx,ry,x,y1,y2,x1,x2,y;
printf("enter cx,cy,rx,ry\n");
scanf("%f",&cx);
Department of Computer Science and Engineering, Christ University 5
Graphics and Multimedia Laboratory (CS651)
scanf("%f",&cy);
scanf("%f",&rx);
scanf("%f",&ry);
if(rx<ry)
{
for(x=cx-rx;x<=(cx+rx);x+=0.005)
{
y1=cy+((ry/rx)*(sqrt(pow(rx,2)-((x-cx)*(x-cx)))));
y2=cy-((ry/rx)*(sqrt(pow(rx,2)-((x-cx)*(x-cx)))));
putpixel(x,y1,100);
putpixel(x,y2,100);
}
}
getch();
closegraph();
}
Department of Computer Science and Engineering, Christ University 6
Graphics and Multimedia Laboratory (CS651)
2. Write a program to perform 2D transformations such as
i. Rotation
ii. Translation
iii. Scaling
iv. Reflection
v. Shearing
PROGRAM:
i) Rotation:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
float ang,angle,xr,yr;
int x1=150,x2=100,y1=200,y2=200;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"...bgi");
line(x1,y1,x2,y2);
printf("\n Enter angle::");
scanf("%f",&ang);
angle=ang*(M_PI/180);
xr=x1+((x2-x1)*cos(angle))-((y2-y1)*sin(angle));
yr=x1+((x2-x1)*cos(angle))+((y2-y1)*sin(angle));
setcolor(GREEN);
line(x1,y1,(int)xr,(int)yr);
getch();
Department of Computer Science and Engineering, Christ University 7
Graphics and Multimedia Laboratory (CS651)
closegraph();
}
ii) Translation
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"...bgi");
int tx,ty,r=50;
int x=100,y=200;
printf("\n Enter tx and ty::");
scanf("%d %d",&tx,&ty);
setcolor(RED);
circle(x,y,r);
setcolor(GREEN);
circle(x+tx,y+ty,r);
getch();
Department of Computer Science and Engineering, Christ University 8
Graphics and Multimedia Laboratory (CS651)
closegraph();
}
iii) Scaling
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
float s;
int x=100,y=200,r=50;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"...bgi");
setcolor(RED);
circle(x,y,r);
printf("\n Enter scaling factor::");
scanf("%f",&s);
Department of Computer Science and Engineering, Christ University 9
Graphics and Multimedia Laboratory (CS651)
setcolor(GREEN);
circle(x,y,r*s);
getch();
closegraph();
}
iv) Reflection:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
float ang,angle,xr,yr;
int x1=150,x2=100,y1=200,y2=200;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"...bgi");
Department of Computer Science and Engineering, Christ University 10
Graphics and Multimedia Laboratory (CS651)
line(x1,y1,x2,y2);
printf("\n Enter angle::");
scanf("%f",&ang);
angle=ang*(M_PI/180);
xr=x1+((x2-x1)*cos(angle))-((y2-y1)*sin(angle));
yr=x1+((x2-x1)*cos(angle))+((y2-y1)*sin(angle));
setcolor(GREEN);
line(x1,y1,(int)xr,(int)yr);
getch();
closegraph();
}
v) SHEARING
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<process.h>
void main()
{
int gdriver=DETECT,gmode;
int c,tx,ty,r,s,x1,x2,y1,y2,sf;
initgraph(&gdriver,&gmode,"..bgi");
Department of Computer Science and Engineering, Christ University 11
Graphics and Multimedia Laboratory (CS651)
printf("\n\nENTER COORDINATES x1 , y1 , x2 , y2 :: ");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
printf("\n\nENTER SHEARING FACTOR :: ");
scanf("%d",&sf);
cleardevice();
line(x1,y1,x1,y2);
line(x1,y1,x2,y1);
line(x2,y2,x2,y1);
line(x2,y2,x1,y2);
setcolor(GREEN);
line(x1,y1,x1,y2);
line(x1,y1,x2,y1);
line(x2*sf,y2*sf,x1,y2);
line(x2*sf,y2*sf,x2,y1);
getch();
closegraph();
}
Department of Computer Science and Engineering, Christ University 12
Graphics and Multimedia Laboratory (CS651)
3 a) Write a program to implement window to viewport mapping .
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "C:\\TC\\BGI");
int xwmin=100,ywmin=150, xwmax=300, ywmax=350,xw=150,yw=200,xvmin=50, yvmin=100,xvmax=80,yvmax=120;
float sx,sy,xv,yv;
sx = (float)(xvmax-xvmin)/(xwmax-xwmin);
sy = (float)(yvmax-yvmin)/(ywmax-ywmin);
xv = ((xw-xwmin)*sx)+xvmin;
yv = ((yw-ywmin)*sy)+yvmin;
rectangle(xwmin, ywmin, xwmax, ywmax);
line(xwmin, ywmin, xw, yw);
rectangle(xvmin, yvmin, xvmax, yvmax);
line(xvmin, yvmin, xv, yv);
getch();
closegraph();
}
Department of Computer Science and Engineering, Christ University 13
Graphics and Multimedia Laboratory (CS651)
3 b) Write a program to implement point clipping.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <graphics.h>
void main()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "C:\\TC\\BGI");
int xwmin=100,ywmin=100, xwmax=150, ywmax=150,x,y,no,i;
int slope;
rectangle(xwmin,ywmin,xwmax,ywmax);
printf("Enter the number of points: ");
scanf("%d",&no);
Department of Computer Science and Engineering, Christ University 14
Graphics and Multimedia Laboratory (CS651)
for(i=0;i<no;i++) {
x = rand() % 250;
y = rand() % 250;
if(x>xwmin && x<xwmax && y>ywmin && y<ywmax)
putpixel(x,y,YELLOW);
else
putpixel(x,y,RED);
}
getch();
closegraph();
}
4. Write a program to implement Cohen Sutherland line clipping algorithm.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<process.h>
int regioncode(int x,int y,int xmin,int ymin, int xmax, int ymax)
{
int rc = 0;
if(x-xmin<0)rc=rc|1;
if(xmax-x<0)rc=rc|2;
Department of Computer Science and Engineering, Christ University 15
Graphics and Multimedia Laboratory (CS651)
if(y-ymin<0)rc=rc|4;
if(ymax-y<0)rc=rc|8;
return rc;
}
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"...bgi");
int xmin=100,xmax=300,ymin=110,ymax=300,i,rc1,rc2;
float m,x1=60,x2=320,y1=100,y2=400;
rc1=regioncode(x1,y1,xmin,ymin,xmax,ymax);
rc2=regioncode(x2,y2,xmin,ymin,xmax,ymax);
m=(y2-y1)/(x2-x1)*1.0;
cleardevice();
rectangle(xmin,ymin,xmax,ymax);
if(rc1==0 && rc2==0){
line(x1,y1,x2,y2);
}
else if((rc1 & rc2) !=0){}
else
{
if((rc1&1)==1)
{
y1=y1+m*(xmin-x1);
x1=xmin;
}
if((rc2&1)==1){
y2=y2+m*(xmin-x2);
x2=xmin;
}
Department of Computer Science and Engineering, Christ University 16
Graphics and Multimedia Laboratory (CS651)
if((rc1&2)==2){
y1=y1+m*(xmax-x1);
x1=xmax;
}
if((rc2&2)==2){
y2=y2+m*(xmax-x2);
x2=xmax;
}
if((rc1&4)==4){
x1=x1+(ymin-y1)/m;
y1=ymin;
}
if((rc2&4)==4)
{
x2=x2+(ymin-y2)/m;
y2=ymin;
}
if((rc1&8)==8)
{
x1=x1+(ymax-y1)/m;
y1=ymax;
}
if((rc2&8)==8){
x2=x2+(ymax-y2)/m;
y2=ymax;
}
line(x1,y1,x2,y2);
}
getch();
closegraph();
Department of Computer Science and Engineering, Christ University 17
Graphics and Multimedia Laboratory (CS651)
}
5. Write a program to visualize the projection of 3D images.
Program:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
typedef struct point {
int x, y, z;
} point;
void main() {
int gdriver=DETECT, gmode;
initgraph(&gdriver, &gmode, "C:\\TC\\BGI");
point a, b, c, d, e, f, g, h, i, j, k, l;
a.x=100; a.y=110; a.z=100;
b.x=200; b.y=110; b.z=100;
c.x=100; c.y=110; c.z=200;
d.x=200; d.y=110; d.z=200;
Department of Computer Science and Engineering, Christ University 18
Graphics and Multimedia Laboratory (CS651)
e.x=100; e.y=60; e.z=200;
f.x=200; f.y=60; f.z=200;
g.x=100; g.y=60; g.z=300;
h.x=100; h.y=10; h.z=100;
i.x=200; i.y=60; i.z=300;
j.x=200; j.y=10; j.z=100;
k.x=200; k.y=10; k.z=300;
l.x=100; l.y=10; l.z=300;
// front
printf("\n FRONT VIEW \n");
rectangle(c.x, c.y, f.x, f.y);
rectangle(g.x, g.y, k.x, k.y);
//side
getch();
cleardevice();
printf("\n SIDE VIEW \n");
line(k.y, k.z, j.y, j.z);
line(j.y, j.z, b.y, b.z);
line(b.y, b.z, d.y, d.z);
line(d.y, d.z, f.y, f.z);
line(f.y, f.z, i.y, i.z);
line(i.y, i.z, k.y, k.z);
// top view
getch();
cleardevice();
printf("\n TOP VIEW \n");
Department of Computer Science and Engineering, Christ University 19
Graphics and Multimedia Laboratory (CS651)
rectangle(a.x, a.z, d.x, d.z);
rectangle(e.x, e.z, i.x, i.z);
getch();
closegraph();
}
Department of Computer Science and Engineering, Christ University 20