[go: up one dir, main page]

0% found this document useful (0 votes)
24 views29 pages

CG Lab Manual

The document is a lab manual for the Computer Graphics course at Techno India NJR Institute of Technology, detailing practical exercises for students. It includes programming tasks using various algorithms such as DDA, Bresenham's, and Midpoint for drawing lines and circles, as well as implementing flood fill and boundary fill techniques. The manual provides code examples and instructions for each practical exercise, aimed at enhancing students' understanding of computer graphics concepts.

Uploaded by

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

CG Lab Manual

The document is a lab manual for the Computer Graphics course at Techno India NJR Institute of Technology, detailing practical exercises for students. It includes programming tasks using various algorithms such as DDA, Bresenham's, and Midpoint for drawing lines and circles, as well as implementing flood fill and boundary fill techniques. The manual provides code examples and instructions for each practical exercise, aimed at enhancing students' understanding of computer graphics concepts.

Uploaded by

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

Techno India NJR Institute of Technology

Lab Manual
Computer Graphics (5CS4- 21)

Department of CSE
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

List of Practical’s

1. Write a program to draw a line using Digital Differential Analyzer (DDA) algorithm
2. Write a program to draw a line using Bresenham’s algorithm
3. Write a program to draw circle using midpoint algorithm.
4. Write a program to draw animated circles.
5. Write a program to implement Flood fill.
6. Write a program to implement Boundary fill.
7. Write a program for 2D scaling of an object.
8. Write a program to rotate an object by a given angle about a given point.

Page 2 of 37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

1. WAP to draw aline using Digital Differential Analyzer (DDA) algorithm

Code:

# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>

void dda_line(const int,const int,const int,const int);


int main( )
{
int driver=VGA;
int mode=VGAHI;
int x_1=0;
int y_1=0;
int x_2=0;
int y_2=0;
do
{
gotoxy(8,10);
cout<<"Coordinates of Point-I (x1,y1) :";

gotoxy(8,11);
cout<<"=================================";

gotoxy(12,13);
cout<<"Enter the value of x1 = ";
cin>>x_1;

gotoxy(12,14);

Page 3 of 37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur
cout<<"Enter the value of y1 = ";

Page 4 of 37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

cin>>y_1;

gotoxy(8,18);
cout<<"Coordinates of Point-II (x2,y2) :";

gotoxy(8,19);
cout<<"==================================";

gotoxy(12,21);
cout<<"Enter the value of x2 = ";
cin>>x_2;

gotoxy(12,22);
cout<<"Enter the value of y2 = ";
cin>>y_2;

initgraph(&driver,&mode,"c:\\tc\\Bgi");

setcolor(15);
dda_line(x_1,y_1,x_2,y_2);

setcolor(15);
outtextxy(110,460,"Press <Enter> to continue or any other key to
exit.");
int key=int(getch( ));
if(key!=13)
break;
}while(1);

return 0;
}

Page 5 of 37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

void dda_line(const int x_1,const int y_1,const int x_2,const int y_2)
{
int color=getcolor( );

int x1=x_1;
int y1=y_1;

int x2=x_2;
int y2=y_2;

if(x_1>x_2)
{
x1=x_2;
y1=y_2;

x2=x_1;
y2=y_1;
}
float dx=(x2-x1);
float dy=(y2-y1);

int steps=abs(dy);

if(abs(dx)>abs(dy))
steps=abs(dx);

float x_inc=(dx/(float)steps);
float y_inc=(dy/(float)steps);

float x=x1;
float y=y1;

Page 6 of 37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

putpixel(x,y,color);
for(int count=1;count<=steps;count++)
{
x+=x_inc;
y+=y_inc;
putpixel((int)(x+0.5),(int)(y+0.5),color);
}
}

Output:

Coordinates of Point-I (x1,y1)

=================================

Enter the value of x1 =40

Enter the value of y1=40

Coordinates of Point-II (x2,y2)

=================================

Enter the value of x2 =90

Enter the value of y2=90

Page 7 of 37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

2. WAP to draw aline using Bresenham’s algorithm

Code:

# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>

void bresenham_line(const int,const int,const int,const int);


int main( )
{
int driver=VGA;
int mode=VGAHI;

int x_1=0;
int y_1=0;

int x_2=0;
int y_2=0;

do
{
gotoxy(8,10);
cout<<"Coordinates of Point-I (x1,y1) :";

gotoxy(8,11);
cout<<"================================";

gotoxy(12,13);
cout<<"Enter the value of x1 = ";
cin>>x_1;

Page 8 of 37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

gotoxy(12,14);
cout<<"Enter the value of y1 = ";
cin>>y_1;

gotoxy(8,18);
cout<<"Coordinates of Point-II (x2,y2) :";

gotoxy(8,19);
cout<<"==================================";

gotoxy(12,21);
cout<<"Enter the value of x2 = ";
cin>>x_2;

gotoxy(12,22);
cout<<"Enter the value of y2 = ";
cin>>y_2;
initgraph(&driver,&mode,"c:\\tc\\Bgi");

setcolor(15);
bresenham_line(x_1,y_1,x_2,y_2);

setcolor(15);
outtextxy(110,460,"Press <Enter> to continue or any other key to
exit.");
int key=int(getch( ));
if(key!=13)
break;
} while(1);
return 0;
}

Page 9 of 37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

void bresenham_line(const int x_1,const int y_1,const int x_2,const int y_2)
{
int color=getcolor( );

int x1=x_1;
int y1=y_1;

int x2=x_2;
int y2=y_2;

if(x_1>x_2)
{
x1=x_2;
y1=y_2;

x2=x_1;
y2=y_1;
}

int dx=abs(x2-x1);
int dy=abs(y2-y1);
int inc_dec=((y2>=y1)?1:-1);

if(dx>dy)
{
int two_dy=(2*dy);
int two_dy_dx=(2*(dy-dx));
int p=((2*dy)-dx);

int x=x1;
int y=y1;

Page 10 of
37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

putpixel(x,y,color);
while(x<x2)
{
x++;
if(p<0)
p+=two_dy;
else
{
y+=inc_dec;
p+=two_dy_dx;
}
putpixel(x,y,color);
}
}
else
{
int two_dx=(2*dx);
int two_dx_dy=(2*(dx-dy));
int p=((2*dx)-dy);

int x=x1;
int y=y1;

putpixel(x,y,color);

while(y!=y2)
{
y+=inc_dec;
if(p<0)
p+=two_dx;
else

Page 11 of
37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

{
x++;
p+=two_dx_dy;
}
putpixel(x,y,color);
}
}
}

Output:

Coordinates of Point-I (x1,y1)

=================================

Enter the value of x1 =40

Enter the value of y1=40

Coordinates of Point-II (x2,y2)

=================================

Enter the value of x2 =90

Enter the value of y2=90

Page 12 of 37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

3. Wap to draw circle using midpoint algorithm.

Code:

# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>

void midpoint_circle(const int,const int,const int);

int main( )
{
int driver=VGA;
int mode=VGAHI;

int h=0;
int k=0;
int r=0;

do
{
gotoxy(8,10);
cout<<"Central Point of the Circle : (x,y) :";

gotoxy(8,11);
cout<<"======================================";

gotoxy(12,13);
cout<<"Enter the value of x_center = ";
cin>>h;

Page 13 of 37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

gotoxy(12,14);
cout<<"Enter the value of y_center = ";
cin>>k;

gotoxy(8,18);
cout<<"Radius of the Circle : r :";

gotoxy(8,19);
cout<<"========================================";

gotoxy(12,21);
cout<<"Enter the value of r = ";
cin>>r;

initgraph(&driver,&mode,"c:\\tc\\Bgi");

setcolor(15);
midpoint_circle(h,k,r);

setcolor(15);
outtextxy(110,460,"Press <Enter> to continue or any other key to
exit.");

int key=int(getch( ));

if(key!=13)
break;
}while(1);
return 0;
}

Page 14 of 37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

void midpoint_circle(const int h,const int k,const int r)


{
int color=getcolor( );
int x=0;
int y=r;

int p=(1-r);

do
{
putpixel((h+x),(k+y),color);
putpixel((h+y),(k+x),color);
putpixel((h+y),(k-x),color);
putpixel((h+x),(k-y),color);
putpixel((h-x),(k-y),color);
putpixel((h-y),(k-x),color);
putpixel((h-y),(k+x),color);
putpixel((h-x),(k+y),color);

x++;

if(p<0)
p+=((2*x)+1);
else
{
y--;
p+=((2*(x-y))+1);
}
}while(x<=y);
}

Page 15 of 37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

Output:-

Central Point of the Circle : (x,y) :


======================================
Enter the value of x_center =100
Enter the value of y_center =100

Radius of the Circle : r :


=====================================
Enter the value of r =50

Page 16 of 37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

4. Write a program to draw animated circles

Code:

#include<stdlib.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

void main()

int x,y,i;

int g=DETECT,d;

initgraph(&g,&d,"c:\\tc\\bgi");

cleardevice();

x=getmaxx()/2;

y=getmaxy()/2;

settextstyle(TRIPLEX_FONT, HORIZ_DIR, 3);

setbkcolor(rand());

setcolor(4);

outtextxy(30,100,"Press");

outtextxy(30,130,"any");

outtextxy(30,160,"key");

outtextxy(30,190, "to");

outtextxy(30,220,"Quit");

Page 17 of 37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

while (!kbhit())

setcolor(rand());

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

circle(x,y,i );

setcolor(rand());

for (int j=70;j<120;j++)

circle(x,y,j);

setcolor(rand());

for (int k=140;k<190;k++)

circle(x,y,k);

setcolor(rand());

for (int l=210;l<230;l++)

circle(x,y,l);

delay(200);

getch();

closegraph();

Page 18 of 37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

Output:

Page 19 of 37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

5. Write a program to implement Flood fill

Code:

# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>

void Flood_fill(const int,const int,const int,const int);

int main( )
{
int driver=VGA;
int mode=VGAHI;

initgraph(&driver,&mode,"c:\\tc\\Bgi");

setcolor(15);
circle(175,175,40);

Flood_fill(175,175,10,0);

getch( );
return 0;
}

void Flood_fill(const int x,const int y, const int fill_color,const int old_color)
{
if(getpixel(x,y)==old_color)
{

Page 20 of 37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

putpixel(x,y,fill_color);

Flood_fill((x+1),y,fill_color,old_color);
Flood_fill((x-1),y,fill_color,old_color);
Flood_fill(x,(y+1),fill_color,old_color);
Flood_fill(x,(y-1),fill_color,old_color);
}
}

Output:

Page 21 of 37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

6. Write a program to implement Boundary fill

Code:

# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>

void Boundary_fill(const int,const int,const int,const int);

int main( )
{
int driver=VGA;
int mode=VGAHI;

initgraph(&driver,&mode,"c:\\tc\\Bgi");

setcolor(10);
circle(175,175,40);

Boundary_fill(175,175,5,10);

getch( );
return 0;
}

void Boundary_fill(const int x,const int y, const int fill_color,const int


boundary_color)
{
if(getpixel(x,y)!=boundary_color&& getpixel(x,y)!=fill_color)
{

Page 22 of 37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

putpixel(x,y,fill_color);

Boundary_fill((x+1),y,fill_color,boundary_color);
Boundary_fill((x-1),y,fill_color,boundary_color);
Boundary_fill(x,(y+1),fill_color,boundary_color);
Boundary_fill(x,(y-1),fill_color,boundary_color);
}
}

Output:

Page 23 of 37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

7. C program to demonstrate scaling of an object


#include<stdio.h>
#include<graphics.h>

// Matrix Multiplication to find new Coordinates.


// s[][] is scaling matrix. p[][] is to store
// points that needs to be scaled.
// p[0][0] is x coordinate of point.
// p[1][0] is y coordinate of given point.
void findNewCoordinate(int s[][2], int p[][1])
{
int temp[2][1] = { 0 };

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


for (int j = 0; j < 1; j++)
for (int k = 0; k < 2; k++)
temp[i][j] += (s[i][k] * p[k][j]);

p[0][0] = temp[0][0];
p[1][0] = temp[1][0];
}

// Scaling the Polygon


void scale(int x[], int y[], int sx, int sy)
{
// Triangle before Scaling
line(x[0], y[0], x[1], y[1]);
line(x[1], y[1], x[2], y[2]);
line(x[2], y[2], x[0], y[0]);

// Initializing the Scaling Matrix.


int s[2][2] = { sx, 0, 0, sy };
Page 24 of 37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

int p[2][1];

// Scaling the triangle


for (int i = 0; i < 3; i++)
{
p[0][0] = x[i];
p[1][0] = y[i];

findNewCoordinate(s, p);

x[i] = p[0][0];
y[i] = p[1][0];
}

// Triangle after Scaling


line(x[0], y[0], x[1], y[1]);
line(x[1], y[1], x[2], y[2]);
line(x[2], y[2], x[0], y[0]);
}

// Driven Program
int main()
{
int x[] = { 100, 200, 300 };
int y[] = { 200, 100, 200 };
int sx = 2, sy = 2;

int gd, gm;


detectgraph(&gd, &gm);
initgraph(&gd, &gm," ");

scale(x, y, sx,sy);

Page 25 of 37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

getch();
return 0;
}

Output:

Page 26 of 37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

8. C program to rotate an object by a given angle about a given point


#include <math.h>
#include <stdio.h>

// Using macros to convert degree to radian


// and call sin() and cos() as these functions
// take input in radians
#define SIN(x) sin(x * 3.141592653589 / 180)
#define COS(x) cos(x * 3.141592653589 / 180)

// To rotate an object
void rotate(float a[][2], int n, int x_pivot, int y_pivot,
int angle)
{
int i = 0;
while (i < n) {
// Shifting the pivot point to the origin
// and the given points accordingly
int x_shifted = a[i][0] - x_pivot;
int y_shifted = a[i][1] - y_pivot;

// Calculating the rotated point co-ordinates


// and shifting it back
a[i][0] = x_pivot
+ (x_shifted * COS(angle)
- y_shifted * SIN(angle));
a[i][1] = y_pivot
+ (x_shifted * SIN(angle)
+ y_shifted * COS(angle));
printf("(%f, %f) ", a[i][0], a[i][1]);
i++;
}

Page 27 of 37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

// Driver Code
int main()
{
// 1st Example
// The following figure is to be
// rotated about (0, 0) by 90 degrees
int size1 = 4; // No. of vertices

// Vertex co-ordinates must be in order


float points_list1[][2] = { { 100, 100 },
{ 150, 200 },
{ 200, 200 },
{ 200, 150 } };
rotate(points_list1, size1, 0, 0, 90);

// 2nd Example
// The following figure is to be
// rotated about (50, -50) by -45 degrees
/*int size2 = 3;//No. of vertices
float points_list2[][2] = {{100, 100}, {100, 200},
{200, 200}};
rotate(points_list2, size2, 50, -50, -45);*/
return 0;
}
Output:
(-100, 100), (-200, 150), (-200, 200), (-150, 200)

Page 28 of 37
Computer Graphics & Multimedia Lab Manual (CS Vth Sem.) Techno India NJR Institute of Technology, Udaipur

Page 29 of 37

You might also like