You are on page 1of 21

SHRI VAISHNAV VIDHYAPEETH

VISHWAVIDYALAYA

Department of Computer Science and


Engineering

PRACTICAL-FILE
Subject Name: - COMPUTER GRAPHICS AND MULTIMEDIA.
Subject Code: -BTCS-503.
Class: -B-Tech CSE 3RDYEAR 5THSEM.
Section: -I (Impetus (Big Data and Cloud Engineering)).

Submitted To: - Submitted By: -


SHWETA JAIN MAM Akshat Agrawal
INDEX
S. No EXPERIMENT NAME DATE OF SIGNATURE
EXPERIMENT
Experiment No.1

Aim: - Study and discuss various types of functions in computer graphics.

Theory: -

GRAPHICS FUNCTIONS: -
1. Putpixel
Purpose: -Putpixel function is to draw the pixel on the screen. Pixel is small
dot on the screen.
Syntax: -putpixel(x co-ordinate, y co-ordinate,COLOR);
Example: – putpixel(100,100,BLUE);
2. SetbkColor
Purpose:-Setbkcolor function is used to set background color of the screen.
Syntax: -setbkcolor(COLOR);
Example: -setbkcolor(RED);

3. Setlinestyle
Purpose: -setlinestyle function is used to set the current line style, width and
pattern
Syntax:-setlinestyle(linestyle, pattern, thickness);
Example:-setlinestyle(SOLID_LINE,1,2);

4. Setcolor
Purpose:-setcolor is to set color of the objects which is to be drawn after this
setcolor line.
Syntax:-setcolor(COLOR);
Example:-setcolor(RED);

5. Rectangle:-
Purpose:- Rectangle function is used to draw the rectangle on the
screen. X1,y1 are the lower left co-ordinates of the rectangle and the x2,y2
are the upper right co-ordinates of the rectangle.
Syntax:– rectangle(x1,y1,x2,y2);
Example:– rectangle(100,100,200,200);
6. Textheight
Purpose:-textheight returns the height of a string in pixels.
Syntax:-textheight(STRING);
Example:- i = textheight(“HELLO”);

7. Textwidth
Purpose:-textwidth returns the width of a string in pixels
Syntax:-textwidth(STRING);
Example:-i = textwidth(“HELLO”);
8. Getx
Purpose:-getx returns the current position’s of x o-ordinate
Syntax:-getx();
Example:-x=getx();

9. Gety
Purpose:-gety returns the current position’s of y co-ordinate
Syntax:-gety();
Example:-y=gety();

10. Getmaxx
Purpose:-getmaxxreturns the maximum x co-ordinate on the screen
Syntax:-getmaxx();
Example:-maxx=getmaxx();

11. Getmaxy
Purpose:-getmaxy returns the maximum y co-ordinate on the screen
Syntax:-getmaxy();
Example:-maxy=getmaxy();

12. Line
Purpose:-Line function is used to draw the line on the screen.
Syntax: line(x1,y1,x2,y2);
Example:-line(100,100,200,100);
13. Closegraph
Purpose:-closegraph function shut down the graphic system
Syntax:-closegraph();
Example:-closegraph();
14. Moveto
Purpose:-moveto function moves current cursor position on the screen
Syntax:-moveto(x co-ordinate, y co-ordinate);
Example:-moveto(getmaxx/2, getmaxy/2);

15. Settextstyle
Purpose:-settextstyle sets the current text characteristics like font, direction
and size
Syntax:-settextstyle(font, direction size);
Example:-settextstyle(1,1,10);
Font 1 DEFAULT
2 TRIPLEX
3 SMALL

4 SANS SERIF

5 GOTHIC

Direction 0 HORIZ_DIR
• VERT_DIR
Size 0 SMALL
10 large

16. Circle
Purpose: Circle function is used to draw the circle on the screen
Syntax:– circle(x,y,radius);
Example:circle(100,100,50);
17. Cleardevice
Purpose: cleardevice function is used to clear the contents or graphic
images on the screen in graphics mode.
Syntax:cleardevice();
Example:cleardevice();
18. Outtextxy
Purpose: outtextxy function is used to print the text on the screen in
graphics mode.
Syntax:outtext(x,y,text);
Example:-outtextxy(100,100,”HELLO”);
19. Sector
Purpose:sector function draws and fills an elliptical pie slice.
Syntax:sector(x, y, starting angle, ending angle, xradius, yradius);
Example:sector(100,100,45 135 100 50);

20. Arc
Purpose:arc draws the arc on the screen, arc is a part of the circle
Syntax:arc(x, y, starting angle, ending angle, radius);
Example:arc( 100,100,90,180,50);
21. Setfillstyle
Purpose: setfillstyle is used to set the color and style to be filled in the object
using the flood fill method.
Syntax:stefillstyle(STYLE, COLOR);
Example:setfillstyle(1,RED)

22. Floodfill
Purpose:floodfill function is used to fill the color in the object, object may be
circle, rectangle or any other closed image.
Syntax:floodfill(x,y,boundary color);
Example:floodfill(100,100,BLUE);

23. Ellipse
Purpose:ellipse function is used to draw the ellipse on the screen.
Syntax:ellipse(x, y, starting angle, ending angle, xradius, yradius);
Example:ellipse(100,100,90,200,20,20);

24. Outtext
Purpose:outtext function is used to display the text on the screen, using this
function text is display in the current position.
Syntax:outtext(STRING);
Example:outtex(“HELLO”);

25. Getcolor
Purpose:getcolor returns the current drawing color.
Syntax:getcolor();
Example:intclr = getcolor();

26. Getpixel
Purpose:getpixel gets the color of a specified pixel.
Syntax:getpixel(x,y);
Example: color=getpixel(100,100);
Experiment No.2

Aim:- Write a program to display house using computer graphics.

Theory:- In this program, we will draw a hut on screen using line and rectangle
function and then fill it with different patterns and colours . We will use below some
graphics functions in this program.

Program:-

// C program to draw a house using

// graphics.h library

#include <conio.h>

#include <graphics.h>

#include <stdio.h>

void main()

// Initialize of gdriver with

// DETECT macros

int gdriver = DETECT, gmode;

// Initialize structure of

// the house

initgraph(&gdriver, &gmode, "");

// Create lines for structure

// of the House

line(100, 100, 150, 50);

line(150, 50, 200, 100);

line(150, 50, 350, 50);


line(350, 50, 400, 100);

// Draw rectangle to give proper

// shape to the house

rectangle(100, 100, 200, 200);

rectangle(200, 100, 400, 200);

rectangle(130, 130, 170, 200);

rectangle(250, 120, 350, 180);

// Set color using setfillstyle()

// which take style and color as

// an argument

setfillstyle(2, 3);

// Fill the shapes with colors white

floodfill(131, 131, WHITE);

floodfill(201, 101, WHITE);

// Change the filling color

setfillstyle(11, 7);

// Fill the shapes with changed colors

floodfill(101, 101, WHITE);

floodfill(150, 52, WHITE);

floodfill(163, 55, WHITE);

floodfill(251, 121, WHITE);

// Close the initialized gdriver

closegraph(); }
Output:-
Experiment No.3

Aim:- Write a program to display moving car using computer graphics.

Theory:- Program in C using graphics to move a car. A car is made using two
rectangles and two circles which act as tires of the car. A for loop is used to move
the car forward by changing the rectangle and circle coordinates and erasing the
previous contents on screen using clearviewport, you can also use cleardevice.
Speed of car can be adjusted using delay function, more the delay lesser will be the
speed or lesser the delay your car will move fast. In this program color of the car
also keeps on changing, this is accomplished by incrementing the color value by one
each time in the for loop, you can also use random function for this purpose. Before
you see a car moving you will be asked to press a key.

Program:-

#include <graphics.h>
#include <dos.h>
#include <conio.h>

main()
{
int i, j = 0, gd = DETECT, gm;

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

settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(25,240,"Press any key to view the moving car");

getch();
setviewport(0,0,639,440,1);

for (i = 0; i <= 420; i = i + 10, j++)


{
rectangle(50+i,275,150+i,400);
rectangle(150+i,350,200+i,400);
circle(75+i,410,10);
circle(175+i,410,10);
setcolor(j);
delay(100);

if (i == 420)
break;

clearviewport();
}

getch();
closegraph();
return 0;
}
Output:-
Experiment No.4

Aim:- Write a program to implement Digital Differential Analyzer (DDA) Line


Algorithm.

Theory / Algorithm:-

1. Start.

2. Declare variables x,y,x1,y1,x2,y2,k,dx,dy,s,xi,yi and declare

gdriver=DETECT,gmode.

3. Initialise the graphic mode with the path location in TC folder.

4. Input the two-line endpoints and store the left endpoints in (x1,y1).

5. Load (x1,y1) into the frame buffer, that is plot the first point put x=x1,y=y1.

6. Calculate dx=x2-x1 and dy=y2-y1.

7. If abs(dx) > abs(dy), do s=abs(dx) otherwise s= abs(dy)..

9. Then xi=dx/s and yi=dy/s.

10. Start from k=0 and continuing till k<s,the points will be

i. x=x+xi.

ii. y=y+yi.

11. Place pixels using putpixel at points (x,y) in specified colour.

12. Close Graph.

13. Stop
Program:-

#include<graphics.h>

#include<conio.h>

#include<stdio.h>

void main()

intgd = DETECT ,gm, i;

float x, y,dx,dy,steps;

int x0, x1, y0, y1;

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

setbkcolor(WHITE);

x0 = 100 , y0 = 200, x1 = 500, y1 = 300;

dx = (float)(x1 - x0);

dy = (float)(y1 - y0);

if(dx>=dy)

steps = dx;

else

steps = dy;

dx = dx/steps;
dy = dy/steps;

x = x0;

y = y0;

i = 1;

while(i<= steps)

putpixel(x, y, RED);

x += dx;

y += dy;

i=i+1;

getch();

closegraph();

}
Output:-
Experiment No.5

Aim:- Write a Program to implement Bresenham’s Line Drawing Algorithm.

Theory / Algorithm:-

1. Start.

2. Declare variables x,y,x1,y1,x2,y2,p,dx,dy and also declare gdriver=DETECT,gmode.

3. Initialize the graphic mode with the path location in TC folder.

4. Input the two-line endpoints and store the left endpoints in (x1,y1).

5. Load (x1,y1) into the frame buffer; that is, plot the first point put x=x1,y=y1.

6. Calculate dx=x2-x1 and dy=y2-y1,and obtain the initial value of decision parameter
p as:

a. p=(2dy-dx).

7. Starting from first point (x,y) perform the following test:

8. Repeat step 9 while(x<=x2).

9. If p<0,next point is (x+1,y) and p=(p+2dy).

10. Otherwise, the next point to plot is (x+1,y+1) and p=(p+2dy-2dx).

11. Place pixels using putpixel at points (x,y) in specified colour.

12. Close Graph.

13. Stop.
Program:-

#include<stdio.h>

#include<graphics.h>

void drawline(int x0, int y0, int x1, int y1)

int dx, dy, p, x, y;

dx=x1-x0;

dy=y1-y0;

x=x0;

y=y0;

p=2*dy-dx;

while(x<x1)

if(p>=0)

putpixel(x,y,7);

y=y+1;

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

else

putpixel(x,y,7);

p=p+2*dy;}
x=x+1;

int main()

int gdriver=DETECT, gmode, error, x0, y0, x1, y1;

initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");

printf("Enter co-ordinates of first point: ");

scanf("%d%d", &x0, &y0);

printf("Enter co-ordinates of second point: ");

scanf("%d%d", &x1, &y1);

drawline(x0, y0, x1, y1);

return 0;

}
Output:-

You might also like