0% found this document useful (0 votes)
94 views

CG File

This document contains 12 computer graphics experiments involving drawing various shapes and implementing graphics algorithms. The experiments include drawing lines, circles, and rectangles using different algorithms, as well as implementing transformations and polygon clipping. Code snippets and output are provided for each experiment, which was submitted as part of a computer graphics course.

Uploaded by

Ravi Ranjan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

CG File

This document contains 12 computer graphics experiments involving drawing various shapes and implementing graphics algorithms. The experiments include drawing lines, circles, and rectangles using different algorithms, as well as implementing transformations and polygon clipping. Code snippets and output are provided for each experiment, which was submitted as part of a computer graphics course.

Uploaded by

Ravi Ranjan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 41

COMPUTER GRAPHICS FILE

Submitted to: Submitted by:

MRS. PARUL TUSHIR Chaitanya sharma

CSE department CSE 4TH SEMESTER

Roll no. 14015001004


INDEX

S.NO EXPERIMENT DATE SIGNATURE

1 WAP to draw a line using 29-August-


DDA algorithm. 2016

2 WAP to draw a line using 29-August-


Bresenhams line drawing 2016

algorithm.
3 WAP to draw a circle using 12-September-
Bresenhams circle 2016

drawing algorithm.
4 WAP to draw a circle using 19-September-
Mid-point algorithm. 2016

5 WAP to draw a rectangle 26-September-


using line function. 2016

6 WAP to draw a triangle 26-September-


using line function. 2016

7 WAP to Implement 3-October-


translation. 2016

8 WAP to Implement 10-October-


boundry filled algorithm. 2016
9 WAP for Cohen-Sutherland 17-October-
Polygon Clipping 2016
algorithm.
10 WAP for Cohen 24-October-
Sutherland Line Clipping 2016
algorithm.
11 WAP for displaying 3D 24-October-
objects as 2D display using 2016
perspective
Transformation.
EXPERIMENT 1
AIM: WAP to draw a rectangle using line function.

CODING:
#include<iostream.h>

#include<conio.h>

#include<graphics.h>

void main()

intgd=DETECT,gmode;

initgraph(&gd,&gmode,"c:\\tc\\bgi");

int x1,x2,x3,x4,x5,x6,x7,y1,y2,y3,y5,y4,y6,y7;

cout<<"enter the coordinates";

cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;

line(x1,y1,x2,y2);

line(x2,y2,x3,y3);

line(x3,y3,x4,y4);

line(x4,y4,x1,y1);

cout<<"rectangle drawn";

getch();

}
OUTPUT:
EXPERIMENT 2
AIM: WAP to draw a triangle using line function.

CODING:
#include<iostream.h>

#include<conio.h>

#include<graphics.h>

void main()

intgd=DETECT,gmode;

initgraph(&gd,&gmode,"c:\\tc\\bgi");

int x1,y1,x2,y2,x3,y3;

cout<<"enter the coordinates";

cin>>x1>>y1>>x2>>y2>>x3>>y3;

line(x1,y1,x2,y2);

line(x2,y2,x3,y3);

line(x3,y3,x1,y1);

cout<<"triangle drawn";

getch();

}
OUTPUT:
EXPERIMENT 3
AIM: WAP to draw a line using DDA algorithm.

CODING:
#include<iostream.h>

#include<conio.h>

#include<graphics.h>

void main()

clrscr();

intgd=DETECT,gmode;

initgraph(&gd,&gmode,"c:\\tc\\bgi");

int x1,y1,x2,y2,step,dx,dy,xi,yi;

cout<<"enter first coordinate";

cin>>x1>>y1;

cout<<"enter second coordinate";

cin>>x2>>y2;

dx=x2-x1;

dy=y2-y1;

if(dx>=dy)

step=dx;

else

{
step=dy;

xi=dx/step;

yi=dx/step;

for(inti=0;i<step;i++)

putpixel(x1,y1,WHITE);

x1=x1+xi;

y1=y1+yi;

getch();

}
OUTPUT:
EXPERIMENT 4
AIM: WAP to draw a line using Bresenhams line drawing algorithm.

CODING:
#include<iostream.h>

#include<conio.h>

#include<graphics.h>

void main()

clrscr();

intgd=DETECT,gmode;

initgraph(&gd,&gmode,"c:\\tc\\bgi");

int x,y,x1,y1,x2,y2,p,dx,dy;

cout<<"enter coordinates of initial point:"<<endl;

cin>>x1>>y1;

cout<<"enter coordinate of final point:"<<endl;

cin>>x2>>y2;

dx=x2-x1;

dy=y2-y1;

p=2*dy-dx;

x=x1;

y=y1;

while (x<=x2)

{
x++;

if(p<=0)

p=p+2*dy;

else

p=p+2*dy-2*dx;

y++;

putpixel(x,y,WHITE);

getch();

}
OUTPUT:
EXPERIMENT 5
AIM: WAP to draw a circle using Bresenhams circle drawing
algorithm.

CODING:
#include<iostream.h>

#include<conio.h>

#include<graphics.h>

voidcirclepoint(intxc,intyc,intx,int y)

putpixel(x+xc,y+yc,WHITE);

putpixel(y+xc,x+yc,WHITE);

putpixel(-y+xc,x+yc,WHITE);

putpixel(-x+xc,y+yc,WHITE);

putpixel(-x+xc,-y+yc,WHITE);

putpixel(-y+xc,-x+yc,WHITE);

putpixel(x+xc,-y+yc,WHITE);

putpixel(y+xc,-x+yc,WHITE);

void main()

clrscr();

intgd=DETECT,gmode;

initgraph(&gd,&gmode,"c:\\tc\\bgi");

intx,y,xc,yc,d,r;
cout<<"enter center coordinates";

cin>>xc>>yc;

cout<<"enter radius";

cin>>r;

x=0;

y=r;

d=3-2*r;

while (x<=y)

circlepoint(xc,yc,x,y);

if(d<=0)

d=d+4*x+6;

else

d=d+4*(x-y)+10;

y--;

x++;

getch();

}
OUTPUT:
EXPERIMENT 6
AIM: WAP to draw a circle using Mid-point algorithm.

CODING:
#include<iostream.h>

#include<conio.h>

#include<graphics.h>

voidcirclepoint(intxc,intyc,intx,int y)

putpixel(x+xc,y+yc,WHITE);

putpixel(y+xc,x+yc,WHITE);

putpixel(-y+yc,x+xc,WHITE);

putpixel(-x+xc,y+yc,WHITE);

putpixel(-x+xc,-y+yc,WHITE);

putpixel(-y+yc,-x+xc,WHITE);

putpixel(y+yc,-x+xc,WHITE);

putpixel(x+xc,-y+yc,WHITE);

void main()

clrscr();

intgd=DETECT,gmode;

initgraph(&gd,&gmode,"c:\\tc\\bgi");

intx,y,xc,yc,d,r;
cout<<"enter center coordinates";

cin>>xc>>yc;

cout<<"enter radius";

cin>>r;

x=0;

y=r;

d=1-r;

while (x<=y)

circlepoint(xc,yc,x,y);

if(d<=0)

d=d+3+2*x;

else

d=d+5+2*(x-y);

y--;

x++;

getch();

}
OUTPUT:
EXPERIMENT 7
AIM: WAP to Implement translation.

CODING:
#include<iostream.h>

#include<conio.h>

#include<graphics.h>

void main()

clrscr();

intgd=DETECT,gmode;

initgraph(&gd,&gmode,"c:\\tc\\bgi");

int tx,ty,x1,x2,y1,y2;

cout<<"enter the coordinates";

cin>>x1>>x2>>y1>>y2;

line(x1,y1,x2,y2);

cout<<"enter the translation factor";

cin>>tx>>ty;

x1=x1+tx;

x2=x2+tx;

y1=y1+ty;

y2=y2+ty;

line(x1,y1,x2,y2);

getch();
}

OUTPUT:
EXPERIMENT 8
AIM: WAP to Implement Boundry Filled Algorithm.

CODING:
#include<stdio.h>

#include<iostream.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

#include<process.h>

voidright_fill(intx,int y)

if((getpixel(x,y)!=WHITE)&&(getpixel(x,y)!=YELLOW))

putpixel(x,y,YELLOW);

right_fill(++x,y);

x--;

right_fill(x,y-1);

right_fill(x,y+1);

delay(0.75);

voidleft_fill(intx,int y)

if((getpixel(x,y)!=WHITE)&&(getpixel(x,y)!=BLUE))
{

putpixel(x,y,BLUE);

left_fill(--x,y);

x++;

left_fill(x,y-1);

left_fill(x,y+1);

delay(0.75);

void main()

clrscr();

intgd=DETECT,gmode;

intxmax,ymax;

initgraph(&gd,&gmode,"c:\\tc\\bgi");

intx,y;

rectangle(200,200,100,100);

x=150;

y=150;

right_fill(x,y);

left_fill(x-1,y);

getch();

}
OUTPUT:
EXPERIMENT 9
AIM: WAP FOR COHEN-SUTHERLAND POLYGON CLIPPING
ALGORITHM
#include<iostream.h>

#include<conio.h>

#include<graphics.h>

#define round(a) ((int)(a+0.5))

int k;

float xmin,ymin,xmax,ymax,arr[20],m;

void clipl(float x1,float y1,float x2,float y2)

if(x2-x1)

m=(y2-y1)/(x2-x1);

else

m=100000;

if(x1 >= xmin && x2 >= xmin)

arr[k]=x2;

arr[k+1]=y2;

k+=2;

if(x1 < xmin && x2 >= xmin)

arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);

arr[k+2]=x2;

arr[k+3]=y2;

k+=4;

if(x1 >= xmin && x2 < xmin)

arr[k]=xmin;

arr[k+1]=y1+m*(xmin-x1);

k+=2;

void clipt(float x1,float y1,float x2,float y2)

if(y2-y1)

m=(x2-x1)/(y2-y1);

else

m=100000;

if(y1 <= ymax && y2 <= ymax)

arr[k]=x2;

arr[k+1]=y2;

k+=2;

if(y1 > ymax && y2 <= ymax)


{

arr[k]=x1+m*(ymax-y1);

arr[k+1]=ymax;

arr[k+2]=x2;

arr[k+3]=y2;

k+=4;

if(y1 <= ymax && y2 > ymax)

arr[k]=x1+m*(ymax-y1);

arr[k+1]=ymax;

k+=2;

void clipr(float x1,float y1,float x2,float y2)

if(x2-x1)

m=(y2-y1)/(x2-x1);

else

m=100000;

if(x1 <= xmax && x2 <= xmax)

arr[k]=x2;

arr[k+1]=y2;

k+=2;
}

if(x1 > xmax && x2 <= xmax)

arr[k]=xmax;

arr[k+1]=y1+m*(xmax-x1);

arr[k+2]=x2;

arr[k+3]=y2;

k+=4;

if(x1 <= xmax && x2 > xmax)

arr[k]=xmax;

arr[k+1]=y1+m*(xmax-x1);

k+=2;

void clipb(float x1,float y1,float x2,float y2)

if(y2-y1)

m=(x2-x1)/(y2-y1);

else

m=100000;

if(y1 >= ymin && y2 >= ymin)

arr[k]=x2;
arr[k+1]=y2;

k+=2;

if(y1 < ymin && y2 >= ymin)

arr[k]=x1+m*(ymin-y1);

arr[k+1]=ymin;

arr[k+2]=x2;

arr[k+3]=y2;

k+=4;

if(y1 >= ymin && y2 < ymin)

arr[k]=x1+m*(ymin-y1);

arr[k+1]=ymin;

k+=2;

void main()

int gdriver=DETECT,gmode,n,poly[20];

float xi,yi,xf,yf,polyy[20];

clrscr();

cout<<"Coordinates of rectangular clip window :\nxmin,ymin :";

cin>>xmin>>ymin;
cout<<"xmax,ymax :";

cin>>xmax>>ymax;

cout<<"\n\nPolygon to be clipped :\nNumber of sides :";

cin>>n;

cout<<"Enter the coordinates :";

for(int i=0;i < 2*n;i++)

cin>>polyy[i];

polyy[i]=polyy[0];

polyy[i+1]=polyy[1];

for(i=0;i < 2*n+2;i++)

poly[i]=round(polyy[i]);

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

setcolor(RED);

rectangle(xmin,ymax,xmax,ymin);

cout<<"\t\tUNCLIPPED POLYGON";

setcolor(WHITE);

fillpoly(n,poly);

getch();

cleardevice();

k=0;

for(i=0;i < 2*n;i+=2)

clipl(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);

n=k/2;

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

polyy[i]=arr[i];

polyy[i]=polyy[0];
polyy[i+1]=polyy[1];

k=0;

for(i=0;i < 2*n;i+=2)

clipt(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);

n=k/2;

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

polyy[i]=arr[i];

polyy[i]=polyy[0];

polyy[i+1]=polyy[1];

k=0;

for(i=0;i < 2*n;i+=2)

clipr(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);

n=k/2;

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

polyy[i]=arr[i];

polyy[i]=polyy[0];

polyy[i+1]=polyy[1];

k=0;

for(i=0;i < 2*n;i+=2)

clipb(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);

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

poly[i]=round(arr[i]);

if(k)

fillpoly(k/2,poly);

setcolor(RED);

rectangle(xmin,ymax,xmax,ymin);
cout<<"\tCLIPPED POLYGON";

getch();

closegraph();}

OUTPUT:
EXPERIMENT 10
AIM:WAP FOR COHEN-SUTHERLAND LINE CLIPPING ALGORITHM
#include<conio.h>

#include<iostream.h>

#include<graphics.h>

static int LEFT=1,RIGHT=2,BOTTOM=4,TOP=8,xl,yl,xh,yh;

int getcode(int x,int y){

int code = 0;

//Perform Bitwise OR to get outcode

if(y > yh) code |=TOP;

if(y < yl) code |=BOTTOM;

if(x < xl) code |=LEFT;

if(x > xh) code |=RIGHT;

return code;

void main()

int gdriver = DETECT,gmode;

initgraph(&gdriver,&gmode,"C:\\TurboC3\\BGI");
setcolor(BLUE);

cout<<"Enter bottom left and top right co-ordinates of window: ";

cin>>xl>>yl>>xh>>yh;

rectangle(xl,yl,xh,yh);

int x1,y1,x2,y2;

cout<<"Enter the endpoints of the line: ";

cin>>x1>>y1>>x2>>y2;

line(x1,y1,x2,y2);

getch();

int outcode1=getcode(x1,y1), outcode2=getcode(x2,y2);

int accept = 0; //decides if line is to be drawn

while(1){

float m =(float)(y2-y1)/(x2-x1);

//Both points inside. Accept line

if(outcode1==0 && outcode2==0){

accept = 1;

break;

//AND of both codes != 0.Line is outside. Reject line

else if((outcode1 & outcode2)!=0){

break;

}else{

int x,y;

int temp;

//Decide if point1 is inside, if not, calculate intersection


if(outcode1==0)

temp = outcode2;

else

temp = outcode1;

//Line clips top edge

if(temp & TOP){

x = x1+ (yh-y1)/m;

y = yh;

else if(temp & BOTTOM){ //Line clips bottom edge

x = x1+ (yl-y1)/m;

y = yl;

}else if(temp & LEFT){ //Line clips left edge

x = xl;

y = y1+ m*(xl-x1);

}else if(temp & RIGHT){ //Line clips right edge

x = xh;

y = y1+ m*(xh-x1);

//Check which point we had selected earlier as temp, and replace its
co-ordinates

if(temp == outcode1){

x1 = x;

y1 = y;

outcode1 = getcode(x1,y1);

}else{
x2 = x;

y2 = y;

outcode2 = getcode(x2,y2);

setcolor(WHITE);

cout<<"After clipping:";

if(accept)

line(x1,y1,x2,y2);

getch();

closegraph();

}
OUTPUT:
EXPERIMENT 11
AIM:WAP FOR DISPLAYING 3D OBJECTS IN 2D DISPLAY USING
PERPECTIVE TRANSFORMATION
#include<stdio.h>

#include<math.h>

#include<graphics.h>

#include<conio.h>

main()

int x1,y1,x2,y2,gd,gm;

int ymax,a[4][8];

float par[4][4],b[4][8];

int i,j,k,m,n,p;

int xp, yp, zp, x, y, z;

a[0][0] = 100; a[1][0] = 100; a[2][0] = -100;

a[0][1] = 200; a[1][1] = 100; a[2][1] = -100;

a[0][2] = 200; a[1][2] = 200; a[2][2] = -100;

a[0][3] = 100; a[1][3] = 200; a[2][3] = -100;


a[0][4] = 100; a[1][4] = 100; a[2][4] = -200;

a[0][5] = 200; a[1][5] = 100; a[2][5] = -200;

a[0][6] = 200; a[1][6] = 200; a[2][6] = -200;

a[0][7] = 100; a[1][7] = 200; a[2][7] = -200;

detectgraph(&gd,&gm);

initgraph(&gd,&gm, "c:\\turboc3\\bgi");

ymax = getmaxy();

xp = 300; yp = 320; zp = 100;

for(j=0; j<8; j++)

x = a[0][j]; y = a[1][j]; z = a[2][j];

b[0][j] = xp - ( (float)( x - xp )/(z - zp)) * (zp);

b[1][j] = yp - ( (float)( y - yp )/(z - zp)) * (zp);

/*- front plane display -*/

for(j=0;j<3;j++)

{
x1=(int) b[0][j]; y1=(int) b[1][j];

x2=(int) b[0][j+1]; y2=(int) b[1][j+1];

line( x1,ymax-y1,x2,ymax-y2);

x1=(int) b[0][3]; y1=(int) b[1][3];

x2=(int) b[0][0]; y2=(int) b[1][0];

line( x1, ymax-y1, x2, ymax-y2);

/*- back plane display -*/

setcolor(11);

for(j=4;j<7;j++)

x1=(int) b[0][j]; y1=(int) b[1][j];

x2=(int) b[0][j+1]; y2=(int) b[1][j+1];

line( x1, ymax-y1, x2, ymax-y2);

x1=(int) b[0][7]; y1=(int) b[1][7];

x2=(int) b[0][4]; y2=(int) b[1][4];

line( x1, ymax-y1, x2, ymax-y2);

setcolor(7);

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

x1=(int) b[0][i]; y1=(int) b[1][i];


x2=(int) b[0][4+i]; y2=(int) b[1][4+i];

line( x1, ymax-y1, x2, ymax-y2);

getch(); getch();

OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy