Computer Graphics LAB: Submitted To Submitted by Mrs. Savita Khatri Mohit Kumar CSE/14/113 14002001032

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 35

COMPUTER

GRAPHICS
LAB
SUBMITTED TO
SUBMITTED BY
MRS. SAVITA KHATRI
KUMAR

MOHIT
CSE/14/113
14002001032

Table of Contents
PROGRAM 1

DRAW A LINE USING DDA ALGORITHM..................................4

SOURCE.........................................................................................................4
OUTPUT.........................................................................................................5
PROGRAM 2

DRAW A LINE USING BRESENHAMS ALGORITHM..................6

SOURCE.........................................................................................................6
OUTPUT.........................................................................................................8
PROGRAM 3

DRAW A CIRCLE USING MID - POINT ALGORITHM..................9

SOURCE.........................................................................................................9
OUTPUT.......................................................................................................11
PROGRAM 4

DRAW A CIRCLE USING BRESENHAMS CIRCLE ALGORITHM12

SOURCE.......................................................................................................12
OUTPUT.......................................................................................................14
PROGRAM 5

FILL A POLYGON USING BOUNDARY FILL ALGORITHM.....15

SOURCE.......................................................................................................15
OUTPUT.......................................................................................................17
PROGRAM 6

FILL A POLYGON USING FLOOD FILL ALGORITHM............18

SOURCE.......................................................................................................18
OUTPUT.......................................................................................................20
PROGRAM 7
TO PERFORM 2D TRANSLATION, SCALING AND
ROTATION.......................................................................................................21
SOURCE.......................................................................................................21
OUTPUT.......................................................................................................24
PROGRAM 8 WINDOW TO VIEW PORT MAPPING............................................27
SOURCE.......................................................................................................27
OUTPUT.......................................................................................................29
PROGRAM 9 IMPLEMENTATION OF COHEN SUTHERLAND ALGORITHM..........30
SOURCE.......................................................................................................30
3

OUTPUT.......................................................................................................33

ROGRAM 1

DRAW A LINE USING DDA ALGORITHM

SOURCE
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
void main() {
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
int x,y,x1,y1,x2,y2,dx,dy,steps,xin,yin,i;
cout<<"\n Enter co-ordinates 1st : ";
cin>>x1>>y1;
cout<<"\n Enter co-ordinates 2nd : ";
cin>>x2>>y2;
dx=x2-x1;
dy=y2-y1;
if(dx>=dy) {
steps=dx;
}
else {
steps=dy;
}
xin=dx/steps;
yin=dy/steps;
for(i=0;i<steps;i++) {
putpixel(x1,y1,4);
x1=x1+xin;
y1=y1+yin;
}
getch();
}

OUTPUT

PROGRAM 2
DRAW A LINE
USING
BRESENHAMS
ALGORITHM
SOURCE
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main() {
int gd = DETECT , gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cleardevice();
int x1,y1,x2,y2;
cout<<"Enter Point 1 : ";
cin>>x1>>y1;
cout<<"Enter Point 2 : ";
cin>>x2>>y2;
int dx,dy;
dx = x2-x1;
dy = y2-y1;
7

int dx2 = dx*2;


int dy2 = dy*2;
int p = dy2-dx;
int x,y;
x = x1;
y = y1;
putpixel(x,y,4);
while(x<=x2) {
if(p<0) {
++x;
putpixel(x,y,4);
p+=dy2;
}
else {
++x;
++y;
putpixel(x,y,4);
p+=(dy2-dx2);
}
//cout<<p<<" "<<x<<" "<<y<<"\n";
}

OUTPUT

PROGRAM 3
DRAW A CIRCLE
USING MID - POINT
ALGORITHM
SOURCE
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void draw(int x,int y,int c) {
int a = 320;
int b = 240;
putpixel(x+a,y+b,c);
putpixel(x+a,-y+b,c);
putpixel(-x+a,y+b,c);
putpixel(-x+a,-y+b,c);
putpixel(y+a,x+b,c);
putpixel(-y+a,x+b,c);
putpixel(y+a,-x+b,c);
putpixel(-y+a,-x+b,c);
}
void main() {
int gd=DETECT,gm;
initgraph(&gd,&gm,"..//BGI");
int r;
cout<<"Enter Radius of the circle to be drawn : ";
cin>>r;
int x,y,h;
10

x = 0 , y = r;
h = 1-r;
while(y>x) {
if(h<0)
h+=(2*x)+3;
else {
h+=(2*x)-(2*y)+5;
--y;
}
++x;
draw(x,y,15);
}
getch();
closegraph();
}

11

OUTPUT

12

PROGRAM 4
DRAW A CIRCLE
USING
BRESENHAMS
CIRCLE
ALGORITHM
SOURCE
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void draw(int x,int y,int c) {
int a = 320;
int b = 240;
putpixel(x+a,y+b,c);
putpixel(x+a,-y+b,c);
putpixel(-x+a,y+b,c);
putpixel(-x+a,-y+b,c);
putpixel(y+a,x+b,c);
putpixel(y+a,-x+b,c);
13

putpixel(-y+a,x+b,c);
putpixel(-y+a,-x+b,c);
}
void main() {
int gd = DETECT,gm;
initgraph(&gd,&gm,"..//BGI");
int r;
cout<<"Enter Radius of the circle to be drawn : ";
cin>>r;
int x,y,d;
x = 0, y = r;
d = 3 - (2 * r);
while(x<y) {
if(d<=0)
d+=(4*x)+6;
else {
d+=4*(x-y)+10;
--y;
}
++x;
draw(x,y,15);
}
getch();
closegraph();
}

14

OUTPUT

15

PROGRAM 5
FILL A POLYGON
USING BOUNDARY
FILL ALGORITHM
SOURCE
#include<graphics.h>
#include<conio.h>
#include<iostream.h>
voidrightFill(intx,inty,intb_col,intf_col){
if(getpixel(x,y)!=b_col&&getpixel(x,y)!=f_col){
putpixel(x,y,f_col);
rightFill(x+1,y,b_col,f_col);
rightFill(x,y+1,b_col,f_col);
rightFill(x,y-1,b_col,f_col);
}
}
voidleftFill(intx,inty,intb_col,intf_col){
if(getpixel(x,y)!=b_col&&getpixel(x,y)!=f_col){
putpixel(x,y,f_col);
leftFill(x-1,y,b_col,f_col);
leftFill(x,y-1,b_col,f_col);
leftFill(x,y+1,b_col,f_col);
}
}
void main(){
intgd = DETECT,gm;
initgraph(&gd,&gm,"..//BGI");
16

intbcol=15,fcol=1;
setcolor(bcol);
line(100,100,300,100);
line(300,100,300,300);
line(100,100,100,300);
line(100,300,300,300);
int x = 200,y = 200;
rightFill(x,y,bcol,fcol);
leftFill(x-1,y,bcol,fcol);
getch();
closegraph();
}

17

OUTPUT

18

PROGRAM 6
FILL A POLYGON
USING FLOOD FILL
ALGORITHM
SOURCE
#include<conio.h>
#include<graphics.h>
void left(int x,int y,int back_col,int f_col) {
if(getpixel(x,y)==back_col) {
putpixel(x,y,f_col);
left(x-1,y,back_col,f_col);
left(x,y+1,back_col,f_col);
left(x,y-1,back_col,f_col);
}
}
void right(int x,int y,int back_col,int f_col) {
if(getpixel(x,y)==back_col) {
putpixel(x,y,f_col);
right(x+1,y,back_col,f_col);
right(x,y+1,back_col,f_col);
right(x,y-1,back_col,f_col);
}
}
void main() {
int gd = DETECT,gm;
initgraph(&gd,&gm,"..//BGI");
int back_col=0,f_col=4;
19

int x=320,y=320;
setcolor(15);
line(100,340,540,340);
line(100,140,100,340);
line(100,140,540,140);
line(540,140,540,340);
right(x,y,back_col,f_col);
left(x-1,y,back_col,f_col);
getch();
closegraph();
}

20

OUTPUT

21

PROGRAM 7
TO PERFORM 2D
TRANSLATION,
SCALING AND
ROTATION
SOURCE
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void translate(int x1,int y1,int x2, int y2,int color) {
int dx=0,dy=0;
cout<<"Enter dx and dy : ";
cin>>dx>>dy;
setcolor(color);
outtextxy(x1+10,y1-10,"Old");
rectangle(x1,y1,x2,y2);
setcolor(WHITE);
outtextxy(x1+dx+10,y1+dy-10,"Translated");
rectangle(x1+dx,y1+dy,x2+dx,y2+dx);
}
void rotate(int x1,int y1,int x2, int y2,int color) {
double angle;
22

cout<<"Enter angle of rotation : ";


cin>>angle;
angle = (angle*3.14)/180;
setcolor(color);
outtextxy(x1+10,y1-10,"Old");
rectangle(x1,y1,x2,y2);
setcolor(WHITE);
int xx1 = (x1*cos(angle))-(y1*sin(angle));
int xx2 = (x1*cos(angle))-(y2*sin(angle));
int xx3 = (x2*cos(angle))-(y2*sin(angle));
int xx4 = (x2*cos(angle))-(y1*sin(angle));
int yy1 = (x1*sin(angle))+(y1*cos(angle));
int yy2 = (x1*sin(angle))+(y2*cos(angle));
int yy3 = (x2*sin(angle))+(y2*cos(angle));
int yy4 = (x2*sin(angle))+(y1*cos(angle));
line(xx1,yy1,xx2,yy2);
line(xx2,yy2,xx3,yy3);
line(xx3,yy3,xx4,yy4);
line(xx4,yy4,xx1,yy1);
}
void scale(int x1,int y1,int x2, int y2,int color) {
float sx=1,sy=1;
cout<<"Enter sx and sy : ";
cin>>sx>>sy;
setcolor(color);
outtextxy(x1+10,y1-10,"Old");
rectangle(x1,y1,x2,y2);
setcolor(WHITE);
outtextxy(x1*sx+10,y1*sy-10,"Scaled");
rectangle(x1*sx,y1*sy,x2*sx,y2*sy);
}
void main() {
int gd=DETE CT,gm;
initgraph(&gd,&gm,"..//BGI");
int x1=220,y1=200,x2=420,y2=300;
int ch;
rectangle(x1,y1,x2,y2);
cout<<"Enter your choice : ";
cout<<"\n\n1. Translate\n2. Scale\n3. Rotate\n";
cin>>ch;
switch(ch){
case 1: translate(x1,y1,x2,y2,RED); break;
case 2: scale(x1,y1,x2,y2,RED); break;
case 3: rotate(x1,y1,x2,y2,RED); break;
default : cout<<"Wrong choice ..!!";
}
23

getch();
closegraph();
}

24

OUTPUT

25

26

27

PROGRAM 8
WINDOW TO VIEW
PORT MAPPING
SOURCE
#include"stdio.h"
#include"conio.h"
#include"graphics.h"
#include"stdlib.h"
void main(){
float xwmin,xwmax,ywmax,ywmin;
float xvmin,xvmax,yvmax,yvmin;
float x[10],y[10],yv,xv,sx,sy;
int gd=DETECT,gm,i;
clrscr();
printf("\n enter window port coordinates:\n(xwmin,ywmin,xwmax,ywmax):
");
scanf("%f%f%f%f",&xwmin,&ywmin,&xwmax,&ywmax);
printf("\n enter view port coordinates:\n(xvmin,yvmin,xvmax,yvmax): ");
scanf("%f%f%f%f",&xvmin,&yvmin,&xvmax,&yvmax);
printf("\n enter vertices for triangle: ");
for(i=0;i < 3;i++) {
printf("\n enter(x%d,y%d):",i,i);
scanf("%f%f",&x[i],&y[i]);
28

}
sx=((xvmax-xvmin)/(xwmax-xwmin));
sy=((yvmax-yvmin)/(ywmax-xwmin));
initgraph(&gd,&gm," ");
outtextxy(80,30,"window port");
rectangle(xwmin,ywmin,xwmax,ywmax);
for(i=0;i <2;i++){
line(x[i],y[i],x[i+1],y[i+1]);
}
line(x[2],y[2],x[0],y[0]);
getch();
cleardevice();
for(i=0;i <3;i++){
x[i]=xvmin+((x[i]-xwmin)*sx);
y[i]=yvmin+((y[i]-ywmin)*sy);
}
outtextxy(150,10,"view port");
rectangle(xvmin,yvmin,xvmax,yvmax);
for(i=0;i <2;i++){
line(x[i],y[i],x[i+1],y[i+1]);
}
line(x[2],y[2],x[0],y[0]);
getch();
}

29

OUTPUT

30

PROGRAM 9
IMPLEMENTATION
OF COHEN
SUTHERLAND
ALGORITHM
SOURCE
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#define TRUE 1
#define FALSE 0
typedef unsigned int outcode;
outcode CompOutCode(float x,float y);
enum { TOP = 0x1,BOTTOM = 0x2,RIGHT = 0x4,LEFT = 0x8};
float xmin,xmax,ymin,ymax;
void clip(float x0,float y0,float x1,float y1){
outcode outcode0,outcode1,outcodeOut;
int accept = FALSE,done = FALSE;
outcode0 = CompOutCode(x0,y0);
outcode1 = CompOutCode(x1,y1);
31

do{
if(!(outcode0|outcode1))
{
accept = TRUE;
done = TRUE;
}
else if(outcode0 & outcode1)
done = TRUE;
else{
float x,y;
outcodeOut = outcode0?outcode0:outcode1;
if(outcodeOut & TOP)
{x = x0+(x1-x0)*(ymax-y0)/(y1-y0);
y = ymax;}
else if(outcodeOut & BOTTOM)
{x = x0+(x1-x0)*(ymin-y0)/(y1-y0);
y = ymin;}
else if(outcodeOut & RIGHT)
{y = y0+(y1-y0)*(xmax-x0)/(x1-x0);
x = xmax;}
else
{y = y0+(y1-y0)*(xmin-x0)/(x1-x0);
x = xmin;}
if(outcodeOut==outcode0)
{x0 = x;
y0 = y;
outcode0 = CompOutCode(x0,y0);}
else{
x1 = x;
y1 = y;
outcode1 = CompOutCode(x1,y1);}
}}
while(done==FALSE);
if(accept)
line(x0,y0,x1,y1);
outtextxy(200,20,"LINE AFTER CLIPPING");
rectangle(xmin,ymin,xmax,ymax);}
outcode CompOutCode(float x,float y)
{outcode code = 0;
32

if(y>ymax)
code|=TOP;
else if(y<ymin)
code|=BOTTOM;
if(x>xmax)
code|=RIGHT;
else if(x<xmin)
code|=LEFT;
return code;}
void main( )
{
float x1,y1,x2,y2;
int gdriver = DETECT, gmode ;
printf("\nEnter the endpoints of line\n");
scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
printf("Enter the rectangular coordinates of clipping window\n");
scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax);
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
outtextxy(200,20,"LINE BEFORE CLIPPING");
line(x1,y1,x2,y2);
rectangle(xmin,ymin,xmax,ymax);
getch( );
cleardevice( );
clip(x1,y1,x2,y2);
getch( );
restorecrtmode( );
}

33

OUTPUT

34

***

35

You might also like