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

Compter Graphics File - 1

The document is a lab file from a computer graphics course. It contains 10 experiments involving drawing lines, circles, triangles and other shapes using various computer graphics algorithms like DDA, Bresenham, midpoint circle drawing etc. For each experiment the objective, software used, code, output and conclusions are documented. The file provides a record of the practical work done as part of the course.

Uploaded by

Akshay Madan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views

Compter Graphics File - 1

The document is a lab file from a computer graphics course. It contains 10 experiments involving drawing lines, circles, triangles and other shapes using various computer graphics algorithms like DDA, Bresenham, midpoint circle drawing etc. For each experiment the objective, software used, code, output and conclusions are documented. The file provides a record of the practical work done as part of the course.

Uploaded by

Akshay Madan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

COMPUTER GRAPHICS

LAB FILE

AMITY SCHOOL OF ENGINEERING AND


TECHNOLOGY
AMITY UNIVERSITY
UTTAR PRADESH

SUBMITTED BY: SUBMITTED TO:


Index
S.N Category of Co Name of Experiment Date of Date of Max. Marks Sign.
o Assignment de Allotment Evaluatio Mark obtaine of
of n s d Facult
experimen y
t
1. Mandatory LR WAP to draw a line using 20/12/16 1
Experiment (10) DDA algorithms

2. 3/01/17 1

WAP to draw a line using


Bresenham's algorithms
3. WAP to draw a circle 10/01/17 1
using Bresenham's
algorithms
4. WAP to draw a circle 17/01/17 1
using Mid-point
algorithms
5. WAP to draw a ellipse 31/01/17 1
using Mid-point
algorithms
6. 07/02/17 1
Cohen Sutherland clipping
algorithm
7. 14/02/17 1
WAP to translate and scale
a triangle
8. a 28/02/17 1
d
s
f WAP to rotate a triangle
9. 07/03/17 1

WAP to reflect a triangle

OPEN PR 10
ENDED (10)
EXPERIME
NT Wap to draw a hyperbola
EXPERIMENT 1

DATE:

OBJECTIVE: Introduction graphics programming

SOFTWARE USED: TurboC3

SOURCE CODE
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main(){
int gd = DETECT,gm;
int x ,y ,radius=80;
initgraph(&gd, &gm, "C:\\TC\\BGI");
x = getmaxx()/2;
y = getmaxy()/2;
outtextxy(x-100, 50, "Circle and Line Using Inbuilt Functions");
circle(x, y, radius);
setcolor(RED);
line(200,300,400,500);
getch(); }
OUTPUT:

CONCLUSIONS: The line and circle is plotted on the screen using inbuilt functions

SIGNATURE:
EXPERIMENT 2
DATE:

OBJECTIVE: To draw a line using DDA algorithm

SOFTWARE USED: Turbo C3

Source Code :

include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<stdlib.h>

void drawline(float,float,float,float);

void main(){clrscr();

int gdriver = DETECT, gmode;


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

drawline(100,200,100,300);
drawline(100,200,150,250);
drawline(150,250,100,300);
getch();}

void drawline(float x1,float y1,float x2,float y2){


float dx,dy,x,y,length;
int i;
dx=x2-x1;
dy=y2-y1;
if(dx>dy) {length=dx;}
else {length=dy;}

dx=(x2-x1)/length;
dy=(y2-y1)/length;
//cout<<"dx and dy are "<<dx<<" and "<<dy;
//getch();

x=x1;y=y1;

for(i=1;i<=length;i++){
putpixel(x,y,MAGENTA);
x=x+dx;
y=y+dy;
//delay(50); }}

OUTPUT:

CONCLUSIONS: The line is plotted on the screen using DDA Algorithm.

SIGNATURE:
EXPERIMENT 3
DATE:

OBJECTIVE: To draw a line using Bresenham’s line drawing algorithm

SOFTWARE USED: Turbo C3

PRACTICAL:

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int gd=DETECT,gm;
int x1,y1,x2,y2,m,Pk,dx,dy;
initgraph(&gd,&gm,"C:\\TC\\BGI");
cout<<" Enter initial coordinates:\n \n”;
cin>>x1>>y1;
cout<<" Enter final coordinates:\n \n”;
cin>>x2>>y2;
dx=x2-x1;
dy=y2-y1;
m=dy/dx;
if(m<1)
{
Pk=2*dy-dx;
while(x1!=x2)
{
if(Pk<0)
{
x1+=1;
putpixel(x1,y1,WHITE);
Pk=Pk+2*dy;
}
else
{
x1+=1;
y1+=1;
putpixel(x1,y1,WHITE);
Pk=Pk+2*dy-2*dx; } } }
else
{
Pk=2*dx-dy;
while(y1!=y2)
{
if(Pk<0)
{
y1+=1;
putpixel(x1,y1,WHITE);
Pk=Pk+2*dx;
}
else
{
x1+=1;
y1+=1;
putpixel(x1,y1,WHITE);
Pk=Pk+2*dx-2*dy; } }
}
getch();
closegraph();
}

RESULT:

CONCLUSIONS: Line is plotted on the screen using Bresenham’s Algorithm.

SIGNATURE:
EXPERIMENT 4
DATE:

OBJECTIVE: To draw a circle using Bresenham’s Circle drawing algorithm

SOFTWARE USED: Turbo C3

PRACTICAL:

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
float Pk,Xc,Yc,r,x,y;

cout<<"Enter radius of circle\n”;


cin>>r;
cout<<"Enter centre coordinates of circle\n”;
cin>>Xc>>Yc;
x=0,y=r;
Pk=3-2*r;
while(x<=y)
{
if(Pk<0)
{
x++;
Pk=Pk+4*x+6;
}
else
{
x++;
y--;
Pk=Pk+4*x-4*y+10;

}
putpixel(x+Xc,y+Yc,WHITE);
putpixel(-x+Xc,y+Yc,WHITE);
putpixel(y+Xc,x+Yc,WHITE);
putpixel(-y+Xc,x+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);
}
getch();
closegraph();
}

RESULT:

CONCLUSIONS: A circle is plotted on the screen using Bresenham’s Algorithm.

SIGNATURE:
EXPERIMENT 5

DATE:

OBJECTIVE: To draw a circle using Midpoint Circle drawing algorithm

SOFTWARE USED: Turbo C++

PRACTICAL:

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
float Pk,Xc,Yc,r,x,y;

cout<<"Enter radius of the circle\n”;


cin>>r;
cout<<"Enter centre coordinates of the circle\n”;
cin>>Xc>>Yc;
x=0,y=r;
Pk=(5/4)-r;
while(x<=y)
{
if(Pk<0)
{
x++;
Pk=Pk+2*x+3;
}
else
{
x++;
y--;
Pk=Pk+2*x-2*y+5;

}
putpixel(x+Xc,y+Yc,WHITE);
putpixel(-x+Xc,y+Yc,WHITE);
putpixel(y+Xc,x+Yc,WHITE);
putpixel(-y+Xc,x+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);
}
getch();
closegraph();
}

RESULT:

CONCLUSIONS –A circle is plotted on the screen using Midpoint Algorithm.

SIGNATURE:
EXPERIMENT 6
DATE:

OBJECTIVE: To draw an Ellipse using midpoint algorithm

SOFTWARE USED: Turbo C++

SOURCE CODE:

#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
float x,y,rx,ry,Xc,Yc;
float pk;

cout<<"Enter major axis\n”;


cin>>rx;
cout<<"Enter minor axis\n”;
cin>>ry;
cout<<"Enter centre\n”;
cin>>Xc>>Yc;
x=0,y=ry;
float Rx=pow(rx,2),Ry=pow(ry,2);
pk=Ry-(Rx*ry)+(Rx/4);
while((2*Ry*x)<=(2*Rx*y))
{
if(pk<0)
{
x=x+1;
pk=pk+(2*Ry*x)+Ry;
}
else
{
x=x+1;
y=y-1;
pk=pk+(2*Ry*x)-(2*Rx*y)+Ry;
}
putpixel(x+Xc,y+Yc,WHITE);
putpixel(-x+Xc,y+Yc,WHITE);
putpixel(x+Xc,-y+Yc,WHITE);
putpixel(-x+Xc,-y+Yc,WHITE);
}
pk=(Ry*pow((x+0.5),2))+(Rx*pow((y-1),2))-(Rx*Ry);
while((2*Rx*y)!=0)
{
if(pk<0)
{
x=x+1;
y=y-1;
pk=pk-(2*Rx*y)+(2*Ry*x)+Rx;
}
else
{
y=y-1;
pk=pk-(2*Rx*y)+Rx;
}
putpixel(x+Xc,y+Yc,WHITE);
putpixel(-x+Xc,y+Yc,WHITE);
putpixel(x+Xc,-y+Yc,WHITE);
putpixel(-x+Xc,-y+Yc,WHITE);

}
getch();
closegraph();
}
RESULT:

CONCLUSIONS: An ellipse is plotted on the screen using midpoint Algorithm.

SIGNATURE:
EXPERIMENT 7
DATE:
OBJECTIVE: WAP to translate and scale a polygon
SOFTWARE USED: TURBO C3
SOURCE CODE:
#INCLUDE <GRAPHICS.H>
#INCLUDE <STDLIB.H>
#INCLUDE <STDIO.H>
#INCLUDE <CONIO.H>
#INCLUDE<MATH.H>
INT X1,Y1,X[20],Y[20],N;
FLOAT THETA;

VOID DRAW_POLY()
{
INT I;
FOR(I=0;I<N-1;I++)
{
LINE(X[I],Y[I],X[I+1],Y[I+1]);
}
LINE(X[0],Y[0],X[N-1],Y[N-1]);
}

VOID TRANSLATE(INT X1,INT Y1)


{
INT A;
FOR(A=0;A<N;A++)
{
X[A]=X[A]+X1;
Y[A]=Y[A]+Y1;

}
}

VOID SCALE(INT X1,INT Y1)


{
INT A,B;
A=X[0];
B=Y[0];
TRANSLATE(-A,-B);
INT I;
FOR(I=0;I<N;I++)
{
X[I]=X[I]*X1;
Y[I]=Y[I]*Y1;
}
TRANSLATE(A,B);

VOID MAIN()
{

INT GDRIVER = DETECT, GMODE;

INITGRAPH(&GDRIVER, &GMODE, “”);

INT I;
CLRSCR();
COUT<<"\NENTER NO. OF SIDES:: ";
CIN>>N;
COUT<<"\NENTER THE CO-ORDINATE OF VERTICES::";
FOR(I=0;I<N;I++)
{
COUT<<"\N"<<I+1<<"::";
CIN>>X[I]>>Y[I];

}
DRAW_POLY();
GETCH();
CLEARDEVICE();

CLRSCR();
INT K;
DO
{
CLRSCR();
CLEARDEVICE();
COUT<<"\N1)TRANSLATE\N2)SCALE\N3)EXIT.";
COUT<<"\NENTER UR CHOICE::";

CIN>>K;
SWITCH(K)
{
CASE 1:
COUT<<"\NFOR X-AXIS::";
CIN>>X1;
COUT<<"\NFOR Y-AXIS::";
CIN>>Y1;
TRANSLATE(X1,Y1);
DRAW_POLY();
GETCH();
CLEARDEVICE();
BREAK;

CASE 2:

COUT<<"\NFOR X-AXIS::";
CIN>>X1;
COUT<<"\NFOR Y-AXIS::";
CIN>>Y1;
SCALE(X1,Y1);
DRAW_POLY();
GETCH();
CLEARDEVICE();
BREAK;

}
} WHILE(K!=3);
RETURN 0;
}

TRANSLATE:
SCALING:

CONCLUSIONS: Scaling and translation of a polygon is plotted on screen using matrix


multiplication.

SIGNATURE:
EXPERIMENT 8

DATE:
OBJECTIVE: WAP to rotate a polygon
SOFTWARE USED: TURBO C3
SOURCE CODE:
#INCLUDE<STDIO.H>
#INCLUDE<GRAPHICS.H>
#INCLUDE<MATH.H>

INT GRADRIVER=DETECT,GRAMODE;
INT N,XS[100],YS[100],I,XP,YP,DEGREE;
FLOAT RADIAN;

VOID ROTATION();
VOID DRAWFN();

VOID DEGTORAD()
{
RADIAN=(FLOAT)DEGREE*3.14F/180;
}

VOID MAIN()
{
PRINTF("ENTER NUMBER OF SIDES: ");
SCANF("%D",&N);
PRINTF("ENTER CO-RDINATES: X,Y FOR EACH POINT ");
FOR(I=0;I<N;I++)
SCANF("%D%D",&XS[I],&YS[I]);
PRINTF("\NENTER PIVOT POINT CO-ORDINATE");
SCANF("%D%D",&XP,&YP);
PRINTF("\NENTER ROTATION ANGLE");
SCANF("%D",&DEGREE);
DEGTORAD();
INITGRAPH(&GRADRIVER,&GRAMODE,"");
CLEARDEVICE();
//DRAWING ORIGINAL IN RED COLOR
SETCOLOR(RED);
DRAWFN();
//DOING ROTATION
ROTATION();
//DRAWING ROTATED POLYGON IN BLUE COLOR
SETCOLOR(BLUE);
DRAWFN();
GETCH();
}

VOID DRAWFN()
{
FOR(I=0;I<N;I++)
LINE(XS[I],YS[I],XS[(I+1)%N],YS[(I+1)%N]);
}

VOID ROTATION()
{
FLOAT T,V;
FOR(I=0;I<N;I++)
{
T=XS[I]-XP;
V=YS[I]-YP;
XS[I]=XP+FLOOR(T*COS(RADIAN)-V*SIN(RADIAN));
YS[I]=YP+FLOOR(V*COS(RADIAN)+T*SIN(RADIAN));
}
}

OUTPUT:

CONCLUSIONS: Rotation of a polygon is plotted on screen using matrix multiplication.

SIGNATURE:
EXPERIMENT 9

DATE:
OBJECTIVE: WAP to reflect a triangle
SOFTWARE USED: TURBO C3
SOURCE CODE:

#INCLUDE<STDIO.H>
#INCLUDE<CONIO.H>
#INCLUDE<GRAPHICS.H>
VOID MAIN()
{
INT A,A1,B,B1,C,C1,XT,CH;
INT GD=DETECT,GM;
INITGRAPH(&GD,&GM,"");
A=GETMAXX();
A1=GETMAXY();
B=A/2;
B1=A1/2;

LINE(B,0,B,A1);
LINE(0,B1,A,B1);
LINE(400,200,600,200);
LINE(400,200,400,100);
LINE(400,100,600,200);

PRINTF("1.X-AXIS\N");
PRINTF("2.Y-AXIS\N");
PRINTF("3.EXIT\N");

DO
{
PRINTF("ENTER YOUR CHOICE\N");
SCANF("%D",&CH);
SWITCH(CH)
{
CASE 1:
C=400-B;C1=200-B1;
LINE(B+C,B1-C1,B+C+200,B1-C1);
LINE(B+C,B1-C1,B+C,B1-C1+100);
LINE(B+C,B1-C1+100,B+C+200,B1-C1);
BREAK;

CASE 2:
C=400-B;C1=200-B1;
LINE(B-C,B1+C1,B-C-200,B1+C1);
LINE(B-C,B1+C1,B-C,B1+C1-100);
LINE(B-C,B1+C1-100,B-C-200,B1+C1);
BREAK;
}
}WHILE(CH<3);
GETCH();
CLOSEGRAPH();
}

OUTPUT:

CONCLUSIONS: Reflection about x-axis and y-axis is plotted on screen using matrix
multiplication.

SIGNATURE:
EXPERIMENT 10
DATE:

OBJECTIVE: To implement Cohen Sutherland line clipping algorithm

SOFTWARE USED: Turbo C3

SOURCE CODE:

#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#define TRUE 1
#define FALSE 0
typedef unsigned intoutcode;
outcodeCompOutCode(float x,float y);
enum
{
TOP = 0x1,
BOTTOM = 0x2,
RIGHT = 0x4,
LEFT = 0x8
};
floatxmin,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);
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);
}
}while(done==FALSE);
if(accept)
line(x0,y0,x1,y1);
outtextxy(200,20,"LINE AFTER CLIPPING");
rectangle(xmin,ymin,xmax,ymax);
}

outcodeCompOutCode(float x,float y)
{
outcode code = 0;
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( );

OUTPUT:

CONCLUSIONS: A line is clipped using the algorithm

SIGNATURE:
OPEN ENDED EXPERIMENT

DATE:

OBJECTIVE: WAP to draw hyperbola

SOFTWARE USED: Turbo C3

SOURCE CODE:
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<dos.h>
#include<stdlib.h>
void main()
{
int gd=DETECT, gm;
initgraph(&gd,&gm,””);
float x,y;
float xc,yc;
float d,fx,fy,b,a;
d=b*b*(a+0.5)*(a+0.5)-a*a-a*a*b*b;
printf(“enter centre (xc,yc)\n”);
scanf(“%f %f”,&xc,&yc);
printf(“enter a & b”);
scanf(“%f %f”,&a,&b);
x=a;
y=0;
fx=2*b*b*a;
fy=0;

while(abs(fy)<=fx)
{ if(d>=0)
{
d=d-a*a*(2*y+3);
}
else
{ d=d-a*a*(2*y+3)+b*b*(2*x+2);
x++;
fx=fx+2*b*b; }
y++;
fy=fy+2*a*a;
putpixel(x+320+xc,240-y-yc,GREEN);
putpixel(x+320+xc,240+y-yc,GREEN);
putpixel(-x+320+xc,240-y-yc,GREEN);
putpixel(-x+320+xc,240+y-yc,GREEN);}
getch();
}
OUTPUT:

CONCLUSIONS: A hyperbola is drawn on screen.

SIGNATURE:

You might also like