0% found this document useful (0 votes)
2K views

Computer Graphics: Lab Manual

This document contains 9 practical summaries for computer graphics lab experiments involving drawing lines, circles, and triangles using various algorithms like DDA, Bresenham, midpoint circle, and performing transformations like translation, rotation, scaling, and reflection on triangles. Each practical provides the program code in C along with sample input and output to implement the given computer graphics concept or transformation.

Uploaded by

Jasmandeep brar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

Computer Graphics: Lab Manual

This document contains 9 practical summaries for computer graphics lab experiments involving drawing lines, circles, and triangles using various algorithms like DDA, Bresenham, midpoint circle, and performing transformations like translation, rotation, scaling, and reflection on triangles. Each practical provides the program code in C along with sample input and output to implement the given computer graphics concept or transformation.

Uploaded by

Jasmandeep brar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

COMPUTER

GRAPHICS
Lab Manual

ByRaj Ankur Singh

S.No. Practical

Date

PRACTICAL 1
Write a program in C to implement DDA Algorithm.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
main()
{
float x,y,x1,y1,x2,y2,dx,dy,length;
int gm,gd=DETECT,i;
printf("enter the value of x1:\t");
scanf("%f",&x1);
printf("enter the value of y1:\t");
scanf("%f",&y1);
printf("enter the value of x2:\t");
scanf("%f",&x2);
printf("enter the value of y2:\t");
scanf("%f",&y2);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
dx=ab s(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
{
length=dx;
}
else
{
length=dy;
}

dx=(x2-x1)/length;
dy=(y2-y1)/length;
x=x1+0.5;
y=y1+0.5;
i=1;
while(i<=length)
{
putpixel(x,y,15);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
getch();
closegraph();
}

Output

PRACTICAL 2
Write a program in C to implement Bresenhams Line Drawing Algorithm.
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
main()
{
clrscr();
float x1,y1,x2,y2,dx,dy,p;
int gm,gd=DETECT,i=1;
printf("Enter The Value of x1:");
scanf("%f",&x1);
printf("Enter The Value of y1: ");
scanf("%f",&y1);
printf("Enter The Value of x2: ");
scanf("%f",&x2);
printf("Enter The Value of y2: ");
scanf("%f",&y2);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
dx=abs(x2-x1);
dy=abs(y2-y1);
p=((2*dy)-dx);
while(i<=dx)
{
putpixel(x1,x2,15);
if(p<0)
{
++x1;

p=p+(2*dy);
}
else
{
++x1;
++y1;
p=p+(2*dy)-(2*dx);
}
i=i+1;
delay(100);
}
getch();
closegraph();
}

Output

PRACTICAL 3
Write a program in C to implement Mid-Point Circle Algorithm.
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
main()
{
float x,y,r,p;
int gm,gd=DETECT;
printf("Enter the radius: ");
scanf("%f",&r);
x=0;
y=r;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
p=1-r;
do
{
putpixel(200+x,200+y,15);
putpixel(200+y,200+x,15);
putpixel(200+x,200-y,15);
putpixel(200+y,200-x,15);
putpixel(200-x,200-y,15);
putpixel(200-y,200-x,15);
putpixel(200-x,200+y,15);
putpixel(200-y,200+x,15);
if(p<0)
{
++x;
p=p+(2*x)+1;

}
else
{
++x;
--y;
p=p+(2*x)-(2*y)+1;
}
delay(100);
}while(x<y);
getch();
closegraph();
}

Output

PRACTICAL 4
Write a program in C to implement Bresenhams Line Drawing Algorithm to
rasterize the line.
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
main()
{
clrscr();
float x1,y1,x2,y2,x,y,dx,dy,e;
int gm,gd=DETECT,i=1;
printf("Enter The Value of x1:");
scanf("%f",&x1);
printf("Enter The Value of y1: ");
scanf("%f",&y1);
printf("Enter The Value of x2: ");
scanf("%f",&x2);
printf("Enter The Value of y2: ");
scanf("%f",&y2);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
dx=abs(x2-x1);
dy=abs(y2-y1);
e=((2*dy)-dx);
x=x1;
y=y1;
while(i<=dx)

{
putpixel(x,y,15);
while(e>=0)
{
++y;
e=e-(2*dx);
}
++x;
e=e+(2*dy);
i=i+1;
}
getch();
closegraph();
}

Output

PRACTICAL 5
Write a program in C to implement Mid-Point Ellipse Algorithm.
#include<conio.h>
#include<stdio.h>
#include<dos.h>
#include<graphics.h>
void main()
{
int gm,gd=DETECT;
int Rx,Ry,P10,x,y,a,b,P20;
printf("Enter Value of Rx and Ry: ");
scanf("%d %d",&Rx,&Ry);
x=0;
y=Ry;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
P10=(Ry*Ry)-(Rx*Rx)-(Rx*Rx*Ry)+(0.25*Rx*Rx);
do
{
putpixel(200+x,200+y,15);
putpixel(200+x,200-y,15);
putpixel(200-x,200+y,15);
putpixel(200-x,200-y,15);
a=(2*Ry*Ry*x)+(2*Ry*Ry);
b=(2*Rx*Rx*y)+(2*Rx*Rx);
if(P10<0)
{
x++;
P10=P10+a+(Ry*Ry);
}
else
{

x++;
y--;
P10=P10+a+(Ry*Ry)-b;
}
delay(100);
}while(a>b);
P20=((Ry*Ry)*((x+0.5)*(x+0.5)))+((Rx*Rx)*((y-1)*(y-1)))-((Rx*Rx)*(Ry*Ry));
a=(2*Ry*Ry*x)+(2*Ry*Ry);
b=(2*Rx*Rx*y)+(2*Rx*Rx);
do
{
putpixel(200+x,200+y,15);
putpixel(200+x,200-y,15);
putpixel(200-x,200+y,15);
putpixel(200-x,200-y,15);
a=(2*Ry*Ry*x)+(2*Ry*Ry);
b=(2*Rx*Rx*y)+(2*Rx*Rx);
if(P20>0)
{
y--;
P20=P20-b+(Rx*Rx);
}
else
{
x++;
y--;
P20=P20-b+a+(Rx*Rx);
}
delay(100);
}while(y>0);
getch();
closegraph();
}

Output

PRACTICAL 6
Write a program in C to perform translation on a triangle.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<process.h>
void main()
{
int gd=DETECT, gm,ch;
int x1,x2,x3,y1,y2,y3,tx,ty;
printf("Enter coordinates of the triangle: ");
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
initgraph(&gd,&gm,"c:\\tc\\bgi");
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
printf("Enter the translation factors: ");
scanf("%d%d",&tx,&ty);
x1=x1+tx;
y1=y1+ty;
x2=x2+tx;
y2=y2+ty;
x3=x3+tx;
y3=y3+ty;
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getche();
closegraph();
}

Output

Enter the translation factors: 45 78

PRACTICAL 7
Write a program in C to perform rotation on a triangle.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<process.h>
void main()
{
int gd=DETECT, gm,ch;
int x1,x2,x3,y1,y2,y3,tx,ty;
float rangle;
printf("Enter coordinates of the triangle: ");
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
initgraph(&gd,&gm,"c:\\tc\\bgi");
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
printf("Enter the rotation angle:");
scanf("%f",&rangle);
rangle=(rangle*3.14)/180;
x1=x1*cos(rangle)-y1*sin(rangle);
y1=x1*sin(rangle)+y1*cos(rangle);
x2=x2*cos(rangle)-y2*sin(rangle);
y2=x2*sin(rangle)+y2*cos(rangle);
x3=x3*cos(rangle)-y3*sin(rangle);
y3=x3*sin(rangle)+y3*cos(rangle);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getche();
closegraph();
}

Output

Enter the rotation angle: 10

PRACTICAL 8
Write a program in C to perform scaling on a triangle.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<process.h>
void main()
{
int gd=DETECT, gm;
int x1,x2,x3,y1,y2,y3,sx,sy;
printf("Enter coordinates of the triangle: ");
scanf("%d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3);
initgraph(&gd,&gm,"c:\\tc\\bgi");
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
printf("Enter the scaling factors: ");
scanf("%d %d",&sx,&sy);
x1=x1*sx;
y1=y1*sy;
x2=x2*sx;
y2=y2*sy;
x3=x3*sx;
y3=y3*sy;
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
closegraph();
}

Output

Enter the scaling factors: 2 2

PRACTICAL 9
Write a program in C to perform Reflection on a triangle.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<process.h>
void main()
{
int gd=DETECT, gm,ch;
int x1,x2,x3,y1,y2,y3,x11,x22,x33,y11,y22,y33;
float angle;
printf("Enter coordinates of the triangle: ");
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
initgraph(&gd,&gm,"c:\\tc\\bgi");
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
printf("Enter the axis of reflection: ");
printf("\n1:about x-axis");
printf("\n2:about y-axis\n");
scanf("%d",&ch);
cleardevice();
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
if(ch==1)
{
x11=x1;
y11=180-y1;
x22=x2;
y22=180-y2;

x33=x3;
y33=180-y3;
}
if(ch==2)
{
x11=180-x1;
y11=y1;
x22=180-x2;
y22=y2;
x33=180-x3;
y33=y3;
}
gotoxy(1,24);
line(x11,y11,x22,y22);
line(x22,y22,x33,y33);
line(x33,y33,x11,y11);
getche();
closegraph();
}

Output

Enter the axis of reflection:


1: about x-axis
2: about y-axis
1

Enter the axis of reflection:


1: about x-axis
2: about y-axis
2

You might also like