Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
43 views18 pages

Practical File CGM

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 18

SHRI VAISHNAV VIDHYAPEETH

VISHWAVIDHYALAYA,INDORE

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERNG

PRACTICAL FILE
Computer Graphics and Multimedia

Submitted to: Submitted by:


Ms. Divyani Joshi Aayush
Aniket Gupta
Kumar
Aayush Jha
Gupta

CSE [Assistant Professor] B-Tech [C.S.E.]

SESSION:JUL-DEC 2022
Shri VaishnavVidyapeethVishwavidyalaya
Shri Vaishnav Institute of Information Technology
Department of Computer Science & Engineering
Computer Graphics & Multimedia(BTCS303)
LIST OF EXPERIMENTS

SN Name of Experiment Page Date of Sign/Remark


No. Experiment
1. To Study various in build graphics functions in C library 01-03

2. Write a program to draw a line using DDA algorithm. 04-06

3. Write a program to draw a line using Bresenham‘s 07-09


algorithm.

4. Write a program to draw a line using Mid Point Line 10-13


Drawing algorithm.
5. Write a program to draw a circle using Mid Point Circle 14-16
Drawing algorithm.
6.

7.

8.

9.

10

Note :-
Student you must complete your practical file according to all instruction given before.
Font style :-Times New Roman, text size 12,heading size 14 and bold,proper justify.
You must come for internal viva with lab file hard copy in proper format. Submit file as
instructed you in proper format.

1. Front page
2. Index page
3. List of experiments
Computer Graphics & Multimedia [BTCS303]

Experiment No.:- 1
Aim: - To Study various in build graphics functions in C library.
Ans: - Graphics using graphics.h functions can be used to draw different shapes, display
text in different fonts, change colors and many more. Using functions of graphics.h in
Turbo C compiler we can make graphics programs, animations, projects, and games. We
can draw circles, lines, rectangles, bars and many other geometrical figures. We can
change their colors using the available functions and fill them.

Following is a list of some main functions of


graphics.h header file. Every function is discussed with the arguments and its description.

 Initgraph

 initgraph initializes the graphics system by loading a graphics driver from


disk (or validating a registered driver), and putting the system into graphics
mode.

 Declaration: initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

 Getpixel

 getpixel function returns the color of pixel present at location(x, y).

 Declaration: int getpixel(int x, int y);

 Putpixel

 putpixel function plots a pixel at location (x, y) of specified color.

 Declaration: void putpixel(int x, int y, int color);

 Floodfill

 floodfill function is used to fill an enclosed area.

 Declaration: void putpixel(int x, int y, int color);

 Arc

 "arc" function is used to draw an arc with center (x, y) and stangle specifies
starting angle, endangle specifies the end angle and last parameter specifies
the radius of the arc. arc function can also be used to draw a circle but for
that starting angle and end angle should be 0 and 360 respectively.

 Declaration: void arc(int x, int y, int stangle, int endangle, int radius);

ANIKET KUMAR JHA 21100BTCSE09768 II YEAR/III SEM 1


Computer Graphics & Multimedia [BTCS303]

 Circle

 Circle function is used to draw a circle with center (x,y) and third parameter
specifies the radius of the circle. The code given below draws a circle.

 Declaration: void circle(int x, int y, int radius);

 Pieslice

 Pieslice function is used to draw and fill a pie slice with center at (x, y) and
given radius r. The slice travels from s_angle to e_angle which are starting
and ending angles for the pie slice. The angles for pie-slice are given in
degrees and are measured counterclockwise.

 Declaration: void pieslice(int x, int y, int s_angle, int e_angle, int r);

 Eclipse

 Ellipse is used to draw an ellipse (x,y) are coordinates of center of the


ellipse, stangle is the starting angle, end angle is the ending angle, and fifth
and sixth parameters specifies the X and Y radius of the ellipse. To draw a
complete ellipse strangles and end angle should be 0 and 360 respectively.

 Declaration:

void ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius);

 Fillelipse

 Fillellipse function use to draw and fill an ellipse with center at (x, y) and
(x_radius, y_radius) as x and y radius of ellipse.

 Declarations : void fillellipse(int x, int y, int xradius, int yradius);

 Sector

 Sector function draws and fills an elliptical pie slice.

 Declaration: void sector( int x, int y, int stangle, int endangle, int xradius, int
yradius);

 Getcolor

 getcolor function returns the current drawing color.

 Declaration : int getcolor();

ANIKET KUMAR JHA 21100BTCSE09768 II YEAR/III SEM 2


Computer Graphics & Multimedia [BTCS303]

 Setcolor

 In Turbo Graphics each color is assigned a number. Total 16 colors are


available. Strictly speaking number of available colors depends on current
graphics mode and driver.For Example :- BLACK is assigned 0, RED is
assigned 4 etc. setcolor function is used to change the current drawing
color.e.g. setcolor(RED) or setcolor(4) changes the current drawing color to
RED. Remember that default drawing color is WHITE.

 Declaration: void setcolor(int color);

 Line

 line function is used to draw a line from a point(x1,y1) to point(x2,y2) i.e.


(x1,y1) and (x2,y2) are end points of the line.The code given below draws a
line.

 Declaration: void line(int x1, int y1, int x2, int y2);

 Linerel

 Linerel function draws a line from the current position(CP) to a point that is
a relative distance (x, y) from the CP, then advances the CP by (x, y). You
can use getx and gety to find the current position.

 Declaration: linerel(int x, int y);

 Lineto

 Function lineto draws a line from the current position (CP) to the point (x,
y), you can get current position using getx and gety function.

 Declaration: lineto(int x, int y);

ANIKET KUMAR JHA 21100BTCSE09768 II YEAR/III SEM 3


Computer Graphics & Multimedia [BTCS303]

Experiment No.:- 2
Aim: - Write a program to draw a line using DDA algorithm.
Program: -
#include <iostream.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
#include <dos.h>
void main()
{
clrscr();
int gd=DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
cout << " Digital Differential Analyzer Algorithm \n"<<endl;
int x1, y1, x2, y2, del_x, del_y, steps, i, x_inc, y_inc;
cout << "Enter value for x1 : " ;
cin >> x1;
cout << "Enter value for y1 : " ;
cin >> y1;
cout << "Enter value for x1 : " ;
cin >> x2;
cout << "Enter value for y2 : " ;
cin >> y2;
del_x = x2 - x1 ;
del_y = y2 - y1 ;
cout << "The Value of del_x : " << del_x << endl ;
cout << "The Value of del_y : " << del_y << endl ;
if (del_x > del_y)
steps = del_x ;
else
steps = del_y ;

ANIKET KUMAR JHA 21100BTCSE09768 II YEAR/III SEM 4


Computer Graphics & Multimedia [BTCS303]

x_inc = del_x / steps ;


y_inc = del_y / steps ;
for(i=0;i<steps;i++)
{
putpixel (x1,y1,WHITE) ;
x1 = x1 + x_inc ;
y1 = y1 + y_inc ;
}
getch();
closegraph();
}

ANIKET KUMAR JHA 21100BTCSE09768 II YEAR/III SEM 5


Computer Graphics & Multimedia [BTCS303]

Output:-

ANIKET KUMAR JHA 21100BTCSE09768 II YEAR/III SEM 6


Computer Graphics & Multimedia [BTCS303]

Experiment No.:- 3
Aim: - Write a program to draw a line using Bresenham‘s algorithm.
Program:-
#include <iostream.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
#include <dos.h>
void main()
{
clrscr();
int gd=DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
int x1, y1, x2, y2, del_x, del_y,p;
cout << " BRESENHAM ALGORITHM \n"<<endl;
cout << "Enter value for x1 : " ;
cin >> x1;
cout << "Enter value for y1 : " ;
cin >> y1;
cout << "Enter value for x1 : " ;
cin >> x2;
cout << "Enter value for y2 : " ;
cin >> y2;
del_x = x2 - x1 ;
del_y = y2 - y1 ;
cout << "The Value of del_x : " << del_x << endl ;
cout << "The Value of del_y : " << del_y << endl ;
p = 2*del_y-del_x;
while(x1<x2)
{
if(p>=0)

ANIKET KUMAR JHA 21100BTCSE09768 II YEAR/III SEM 7


Computer Graphics & Multimedia [BTCS303]

{
putpixel(x1,y1,WHITE);
y1=y1+1;
p=p+(2*del_y)-(2*del_x);
}
else
{
putpixel(x1,y1,RED);
p=p+(2*del_y);
}
x1=x1+1;
}
delay(100);
getch();
}

ANIKET KUMAR JHA 21100BTCSE09768 II YEAR/III SEM 8


Computer Graphics & Multimedia [BTCS303]

Output:-

ANIKET KUMAR JHA 21100BTCSE09768 II YEAR/III SEM 9


Computer Graphics & Multimedia [BTCS303]

Experiment No.:- 4
Aim: - Write a program to draw a line using Mid Point Line Drawing
algorithm.
Program:-
#include <iostream.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
#include <dos.h>

void midPoint(int X1, int Y1, int X2, int Y2)


{
int dx = X2 - X1;
int dy = Y2 - Y1;
cout<<" Mid Point Line Drawing Algorithm "<<endl;
if(dy<=dx)
{
int d = dy - (dx/2);
int x = X1, y = Y1;
putpixel(x,y,RED);
cout << x << "," << y << "\n";
while (x < X2)
{
x++;
if (d < 0)
d = d + dy;
// NE or North East is chosen
else
{
d += (dy - dx);
y++;
}

ANIKET KUMAR JHA 21100BTCSE09768 II YEAR/III SEM 10


Computer Graphics & Multimedia [BTCS303]

putpixel(x,y,WHITE);
cout << x << "," << y << "\n";
}
}
else if(dx<dy)
{
int d = dx - (dy/2);
int x = X1, y = Y1;
putpixel(x,y,CYAN);
cout << x << "," << y << "\n";
while (y < Y2)
{
y++;

// E or East is chosen

if (d < 0)
d = d + dx;

// NE or North East is chosen

else
{
d += (dx - dy);
x++;
}

putpixel(x,y,BLUE);
cout << x << "," << y << "\n";
}
}
}

ANIKET KUMAR JHA 21100BTCSE09768 II YEAR/III SEM 11


Computer Graphics & Multimedia [BTCS303]

int main()
{
int gd = DETECT, gm;

initgraph (&gd, &gm, "C:\\TURBOC3\\BGI");

int X1 = 200, Y1 = 200, X2 = 225, Y2 = 225;


midPoint(X1, Y1, X2, Y2);
return 0;
}

ANIKET KUMAR JHA 21100BTCSE09768 II YEAR/III SEM 12


Computer Graphics & Multimedia [BTCS303]

Output:-

ANIKET KUMAR JHA 21100BTCSE09768 II YEAR/III SEM 13


Computer Graphics & Multimedia [BTCS303]

Experiment No.:- 5
Aim: - Write a program to draw a circle using Mid Point Circle Drawing
algorithm.
Program:-
#include <iostream.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
#include <dos.h>
void main()
{
clrscr();
int gd=DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
float r=100,a=340,b=280,x,y,p0;
cout<<" Mid Point Circle Drawing Algorithm \n"<<endl;
cout<<"Center of the Circle lie at "<<endl;
cout<<"x co-ordinate = "<<a<<endl;
cout<<"y co-ordinate = "<<b<<endl;
cout<<"Radius of the circle = "<<r<<endl;
x=0;
y=r;
putpixel (a, b+r, WHITE);
putpixel (a, b-r, WHITE);
putpixel (a-r, b, WHITE);
putpixel (a+r, b, WHITE);
p0=(5/4)-r;
while(x<=y)
{
if(p0<0)
{
x=x+1;

ANIKET KUMAR JHA 21100BTCSE09768 II YEAR/III SEM 14


Computer Graphics & Multimedia [BTCS303]

p0 = p0+(4*x)+6;
}
else
{
y=y-1;
p0 = p0+2*(x-y)+5;
}
x=x+1;
putpixel(a + x, b + y, WHITE);
putpixel(a + y, b + x, WHITE);
putpixel(a - y, b + x, WHITE);
putpixel(a - x, b + y, WHITE);
putpixel(a - x, b - y, WHITE);
putpixel(a - y, b - x, WHITE);
putpixel(a + y, b - x, WHITE);
putpixel(a + x, b - y, WHITE);
}
getch();

ANIKET KUMAR JHA 21100BTCSE09768 II YEAR/III SEM 15


Computer Graphics & Multimedia [BTCS303]

Output:-

ANIKET KUMAR JHA 21100BTCSE09768 II YEAR/III SEM 16

You might also like