graphics lab manual 4

26
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING GRAPHICS AND MULTIMEDIA LABORATORY (CS651) MELVIN MATHEW 1159101 A Laboratory Record submitted in partial fulfillment of the requirement of Computer Programming Laboratory of VIth B.tech(CSE) March 2014

Upload: dhana210

Post on 23-Oct-2015

59 views

Category:

Documents


6 download

DESCRIPTION

Computer graphics lab manual

TRANSCRIPT

Page 1: Graphics lab manual 4

DEPARTMENT

OF

COMPUTER SCIENCE AND ENGINEERING

GRAPHICS AND MULTIMEDIA LABORATORY

(CS651)

MELVIN MATHEW

1159101

A Laboratory Record submitted in partial fulfillment of the requirement of

Computer Programming Laboratory of VIth B.tech(CSE)

March 2014

Page 2: Graphics lab manual 4

Certificate

This is to certify that the record titled Graphics and multimedia(CS651) is a

bonafide record of work done by Melvin Mathew in partial fulfillment of

requirement of VIth Integrated B.tech + M.tech CSE during the year 2013.

HEAD OF THE DEPARTMENT FACULTY-IN-CHARGE

EXAMINER 1:

ASSOCIATE DEAN EXAMINER 2:

Name : ___________________

Register Number : ___________________

Examination Center : ___________________

Date of Examination : ___________________

Page 3: Graphics lab manual 4

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.

Page 4: Graphics lab manual 4

Table of Contents

PRGM

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. 3

2

Write a program to perform 2D transformations such as

i) Rotation ii) Translation iii) Scaling iv) Reflection v) Shearing

7

3 a.

Write a program to implement window to viewport mapping.

14

3 b. Write a program to implement point clipping. 16

4 Write a program to implement Cohen Sutherland line clipping algorithm. 18

Page 5: Graphics lab manual 4

Graphics and Multimedia Laboratory (CS 651)

Department of Computer Science and Engineering, Christ University 1

1. a) Write a program to implement Bresenham’s line algorithm.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

#include<process.h>

void main()

{

int gdriver=DETECT,gmode;

initgraph(&gdriver,&gmode,"C:\\Turbo\\TC\\BGI");

int x1,y1,x2,y2,p,y,x,i,n,nn;

double m;

printf("ENTER COORDINATES OF LINE :: ");

scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

x=fabs(x1-x2);

y=fabs(y1-y2);

m=y/x*1.0;

if(m<1)

{

printf("\n\nERROR\n\n");

exit(0);

}

cleardevice();

p=2*y-x;

if(x2>x1)

{

i=x1;

n=x2;

nn=y1;

}

else

Page 6: Graphics lab manual 4

Graphics and Multimedia Laboratory (CS 651)

Department of Computer Science and Engineering, Christ University 2

{

i=x2;

n=x1;

nn=y2;

}

while(i<=n)

{

if(p<1)

{

putpixel(i,nn,100);

i=i+1;

p=p+2*y;

}

else

{

putpixel(i,nn,100);

i=i+1;

nn=nn+1;

p=p+2*y-2*x;

}

}

getch();

}

Page 7: Graphics lab manual 4

Graphics and Multimedia Laboratory (CS 651)

Department of Computer Science and Engineering, Christ University 3

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)

{

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

Page 8: Graphics lab manual 4

Graphics and Multimedia Laboratory (CS 651)

Department of Computer Science and Engineering, Christ University 4

{

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;

}

plot(xc,yc,x,y);

}

getch();

}

Page 9: Graphics lab manual 4

Graphics and Multimedia Laboratory (CS 651)

Department of Computer Science and Engineering, Christ University 5

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

void main()

{

int gdriver=DETECT,gmode;

initgraph(&gdriver,&gmode,"C:\TurboC++\Disk\turboC3\BGI");

float cx,cy,rx,ry,x,y1,y2,x1,x2,y;

printf("enter cx,cy,rx,ry\n");

scanf("%f",&cx);

scanf("%f",&cy);

scanf("%f",&rx);

scanf("%f",&ry);

if(rx<ry)

{

for(x=cx-rx;x<=(cx+rx);x+=0.005)

{

Page 10: Graphics lab manual 4

Graphics and Multimedia Laboratory (CS 651)

Department of Computer Science and Engineering, Christ University 6

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

}

Page 11: Graphics lab manual 4

Graphics and Multimedia Laboratory (CS 651)

Department of Computer Science and Engineering, Christ University 7

2. Write a program to perform 2D transformations such as

i. Rotation

ii. Translation

iii. Scaling

iv. Reflection

v. Shearing

PROGRAM:

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

float rx,ry,angle;

initgraph(&gdriver,&gmode,"C:\\Turbo\\TC\\BGI");

while(1){

printf("\n\nENTER COORDINATES x1 , y1 , x2 , y2 :: ");

scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

printf("\n\n1:TRANSFORM 2:ROTATION 3: SHEARING 4: SCALING

5:REFLECTION \n ENTER CHOICE ::");

scanf("%d",&c);

cleardevice();

switch(c)

{

case 1:

printf("\n\nENTER TRANSFORM MATRIX ::");

Page 12: Graphics lab manual 4

Graphics and Multimedia Laboratory (CS 651)

Department of Computer Science and Engineering, Christ University 8

scanf("%d%d",&tx,&ty);

cleardevice();

line(x1,y1,x2,y2);

setcolor(RED);

line(x1+tx,y1+ty,x2+tx,y2+ty);

break;

case 2:

printf("\n\nENTER ANGLE OF ROTATION ::");

scanf("%d",&r);

angle=(r*3.14)/180;

line(x1,y1,x2,y2);

setcolor(BLUE);

rx=x1+(x2-x1)*cos(angle)-(y2-y1)*sin(angle);

ry=y1+(x2-x1)*sin(angle)+(y2-y1)*cos(angle);

line(x1,y1,rx,ry);

break;

case 3:

printf("\n\nENTER SHEARING FACTOR :: ");

scanf("%d",&sf);

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

Page 13: Graphics lab manual 4

Graphics and Multimedia Laboratory (CS 651)

Department of Computer Science and Engineering, Christ University 9

line(x2*sf,y2*sf,x2,y1);

break;

case 4:

printf("\n\nENTER SCALING FACTOR ::");

scanf("%d",&s);

line(x1,y1,x2,y2);

setcolor(70);

line(x1*s,s*y1,s*x2,s*y2);

break;

case 5:

line(x1,y1,x2,y2);

setcolor(50);

rx=x1+(x2-x1)*cos(3.14)-(y2-y1)*sin(3.14);

ry=y1+(x2-x1)*sin(3.14)+(y2-y1)*cos(3.14);

line(x1,y1,rx,ry);

break;

case 6:exit(0);

break;

}

getch();

cleardevice();

}

}

Page 14: Graphics lab manual 4

Graphics and Multimedia Laboratory (CS 651)

Department of Computer Science and Engineering, Christ University 10

Translation

Page 15: Graphics lab manual 4

Graphics and Multimedia Laboratory (CS 651)

Department of Computer Science and Engineering, Christ University 11

Rotation

Page 16: Graphics lab manual 4

Graphics and Multimedia Laboratory (CS 651)

Department of Computer Science and Engineering, Christ University 12

Shearing

Scaling

Page 17: Graphics lab manual 4

Graphics and Multimedia Laboratory (CS 651)

Department of Computer Science and Engineering, Christ University 13

Reflection

Page 18: Graphics lab manual 4

Graphics and Multimedia Laboratory (CS 651)

Department of Computer Science and Engineering, Christ University 14

3 a) Write a program to implement window to viewport mapping.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

#include<process.h>

void main()

{

int gdriver=DETECT,gmode;

int xvmin, xvmax, xwmin, xwmax, ywmin, ywmax, yvmin, yvmax, xw1, xw2, yw1,

yw2, xv1,xv2,yv1,yv2;

float sx,sy;

initgraph(&gdriver,&gmode,"C:\\Turbo\\TC\\BGI");

printf("\n\n ENTER xwmin , ywmin , xwmax ,ywmax :: ");

scanf("%d%d%d%d",&xwmin,&ywmin,&xwmax,&ywmax);

printf("\n\nENTER COORDINATES IN WINDOW PORT x1 , y1 , x2 , y2 :: ");

scanf("%d%d%d%d",&xw1,&yw1,&xw2,&yw2);

printf("\n\n ENTER xvmin , yvmin , xvmax ,yvmax :: ");

scanf("%d%d%d%d",&xvmin,&yvmin,&xvmax,&yvmax);

cleardevice();

rectangle(xwmin,ywmin,xwmax,ywmax);

line(xw1,yw1,xw2,yw2);

setcolor(RED);

rectangle(xvmin,yvmin,xvmax,yvmax);

sx=(xvmax-xvmin)/(xwmax-xwmin);

sy=(yvmax-yvmin)/(ywmax-ywmin);

Page 19: Graphics lab manual 4

Graphics and Multimedia Laboratory (CS 651)

Department of Computer Science and Engineering, Christ University 15

xv1=xvmin+(xw1-xwmin)*sx;

yv1=yvmin+(yw1-ywmin)*sy;

xv2=xvmin+(xw2-xwmin)*sx;

yv2=yvmin+(yw2-ywmin)*sy;

line(xv1,yv1,xv2,yv2);

getch();

}

Page 20: Graphics lab manual 4

Graphics and Multimedia Laboratory (CS 651)

Department of Computer Science and Engineering, Christ University 16

3 b) Write a program to implement point clipping.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

#include<process.h>

void main()

{

int gdriver=DETECT,gmode;

initgraph(&gdriver,&gmode,"C:\\Turbo\\TC\\BGI");

int x[10],y[10],xmin,xmax,ymin,ymax;

double m;

printf("\n\nENTER xmin ::");

scanf("%d",&xmin);

printf("\n\nENTER ymin ::");

scanf("%d",&ymin);

printf("\n\nENTER xmax ::");

scanf("%d",&xmax);

printf("\n\nENTER ymax ::");

scanf("%d",&ymax);

printf("ENTER 10 POINTS \n\n");

for(int i=0;i<5;i++)

Page 21: Graphics lab manual 4

Graphics and Multimedia Laboratory (CS 651)

Department of Computer Science and Engineering, Christ University 17

{

printf("\n\nENTER COORDINATE FOR %d POINT :: ",i+1);

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

}

cleardevice();

rectangle(xmin,ymin,xmax,ymax);

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

{

if(x[i]<xmin || x[i]>xmax || y[i]<ymin || y[i]>ymax) {}

else

putpixel(x[i],y[i],GREEN);

}

getch();

}

Page 22: Graphics lab manual 4

Graphics and Multimedia Laboratory (CS 651)

Department of Computer Science and Engineering, Christ University 18

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;

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,"C:\\Turbo\\TC\\BGI");

int xmin,xmax,ymin,ymax,i,rc1,rc2;

float m,x1,x2,y1,y2;

Page 23: Graphics lab manual 4

Graphics and Multimedia Laboratory (CS 651)

Department of Computer Science and Engineering, Christ University 19

printf("\n\nENTER xmin,ymin,xmax,ymax :: ");

scanf("%d%d%d%d",&xmin,&ymin,&xmax,&ymax);

printf("\n\nENTER COORDINATES OF LINE :: ");

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

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;

}

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

Page 24: Graphics lab manual 4

Graphics and Multimedia Laboratory (CS 651)

Department of Computer Science and Engineering, Christ University 20

x2=x2+(ymax-y2)/m;

y2=ymax;

}

line(x1,y1,x2,y2);

}

getch();

}

Page 25: Graphics lab manual 4

Graphics and Multimedia Laboratory (CS 651)

Department of Computer Science and Engineering, Christ University 21

Page 26: Graphics lab manual 4

Graphics and Multimedia Laboratory (CS 651)

Department of Computer Science and Engineering, Christ University 22