0% found this document useful (0 votes)
813 views65 pages

Computer Graphics Lab Manual 2019-20 (1) .

This document provides information about the Computer Graphics & Multimedia Lab course offered to third year students of Computer Science & Engineering at Jaipur Engineering College and Research Center. It includes the vision, program outcomes, syllabus, experiments and mapping of course outcomes to program outcomes. The objectives are to impart knowledge of computer graphics concepts and algorithms through hands-on experiments.

Uploaded by

Aditya Pugalia
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)
813 views65 pages

Computer Graphics Lab Manual 2019-20 (1) .

This document provides information about the Computer Graphics & Multimedia Lab course offered to third year students of Computer Science & Engineering at Jaipur Engineering College and Research Center. It includes the vision, program outcomes, syllabus, experiments and mapping of course outcomes to program outcomes. The objectives are to impart knowledge of computer graphics concepts and algorithms through hands-on experiments.

Uploaded by

Aditya Pugalia
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/ 65

LAB MANUAL

Lab Name : Computer Graphics & Multimedia Lab

Lab Code : 5CS4-21

Branch : Computer Science & Engineering

Year : 3rd Year

Jaipur Engineering College and Research Center, Jaipur


Department of Computer Science & Engineering
(Rajasthan Technical University, KOTA)
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

INDEX
S.NO CONTENTS

1 VISION/MISION

2. PEO

3. PO’s

4. MAPPING OF PEO & PO

5. COURSE OUTCOMES & CO-PO MAPPING

6. PROGRAM SPECIFIC OUTCOMES & PSO-PO MAPPING

7. SYLLABUS

8. BOOKS

9. INSTRUCTIONAL METHODS

10. LEARNING MATERIALS

11. ASSESSMENT OF OUTCOMES

12. OUTCOMES WILL BE ACHIEVED THROUGH

13. DO’s, DONT’s and INSTRUCTIONS

14. EXPERIMENTS

Exp: - 1 Objective: - To learn basic graphics function

Exp: - 2 Objective: - To draw pixel, line, ellipse, heart, smiley face and circle
using function
Exp: -3 Objective: - To implement Bresenham’s Algorithm for Line Drawing

2
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

Exp: -4
Objective: - To Draw A Line Using DDA Algorithm

Exp: -5
Objective: - To Draw A Circle Using Bresenham’s Algorithm

Exp:-6 Objective: - To Draw An Ellipse Using Mid-Point Ellipse Drawing


Algorithm.

Exp:-7 Objective: - To implement Cohen Sutherland 2D line clipping


algorithm
Exp:-8 Objective: - To rotate an object About Origin.

Exp:-9 Objective: - To scale an object with scaling factors along X and Y


directions
Exp:-10 Objective: - To translate an object with translation parameters in X and Y
directions

Exp:-11 Objective: - To Rotate a Point About a Point

Exp:-12 Objective: - To rotate a point with a certain angle about origin

Exp:-13 Objective: - To Reflect a Triangle

Exp:-14 Objective: - To Draw a Hut Using Simple Graphic Functions

Exp:-15 Objective: - To Fill a Polygon

Exp:-16 Objective: - To draw a moving helicopter

Exp:-17 Objective: - To draw a moving fan

Exp:-18 Objective: - To draw a moving car

Exp:-19 Objective: - To draw a moving fish

3
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

1. VISION & MISSION

VISION: To become renowned Centre of excellence in computer science and engineering and make
Competent engineers & professionals with high ethical values prepared for lifelong learning.

MISSION:

• To impart outcome based education for emerging technologies in the field of computer

-Science and engineering.


• To provide opportunities for interaction between academia and industry.
• To provide platform for lifelong learning by accepting the change in technologies.
• To develop aptitude of fulfilling social responsibilities.

2. PEO

• To provide students with the fundamentals of Engineering Sciences with more emphasis in Computer
Science &Engineering by way of analyzing and exploiting engineering
Challenges.
• To train students with good scientific and engineering knowledge so as to comprehend,
Analyze, design, and create novel products and solutions for the real life problems.
• To inculcate professional and ethical attitude, effective communication skills,
Teamwork skills, multidisciplinary approach, entrepreneurial thinking and an ability to relate
Engineering issues with social issues.
• To provide students with an academic environment aware of excellence, leadership, written ethical
- Codes and guidelines, and the self-motivated life-long learning needed for a successful professional
Career.
• To prepare students to excel in Industry and Higher education by Educating Students
Along with High moral values and Knowledge

4
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

3. PROGRAM OUTCOMES
1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering fundamentals,
and an engineering specialization to the solution of complex engineering problems.
2. Problem analysis: Identify, formulate, research literature, and analyze complex engineering-
Problems reaching substantiated conclusions using first principles of mathematics, natural sciences,
and engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering problems and design
system components or processes that meet the specified needs with appropriate consideration for the
public health and safety, and the cultural, societal, and environmental considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and research-
methods including design of experiments, analysis and interpretation of data, and synthesis of the
information to provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern-
engineering and IT tools including prediction and modeling to complex engineering activities with
an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the
professional engineering practice.
7. Environment and sustainability: Understand the impact of the professional engineering solutions
in societal and environmental contexts, and demonstrate the knowledge of, and need for sustainable
development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms
of the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the engineering
community and with society at large, such as, being able to comprehend and write effective reports
and design documentation, make effective presentations, and give and receive clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of the engineering

5
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

and management principles and apply these to one’s own work, as a member and leader in a team,
to manage projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.

4. MAPPING OF PEOs & POs

PROGRAM PROGRAM OUTCOMES


OBJECTIVES
1 2 3 4 5 6 7 8 9 10 11 12

I 3 1 0 0 0 0 0 0 0 0 0 3

II 2 3 2 3 0 3 0 0 0 0 1 3

III 1 3 2 3 1 0 0 0 0 0 2 0

IV 0 0 0 1 2 0 3 2 3 0 2 0

V 0 0 0 0 0 0 0 0 2 2 0 0

[L=1, M=2, H=3]

5. COURSE OUTCOMES
Graduates would be able:
1. Apply basic concepts and different types of graphics drawing algorithm.
2. Analyze different transformations and animation.

6
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

MAPPING OF CO & PO

CO-PO Mapping

CGMT LAB 5CS4-21


P P P P
O P O O O PO1
PO1 PO2 3 PO4 O5 6 PO7 8 9 PO10 1 PO12
CO1: Apply
basic concepts
and different
types of 3 3 3 3 2 2 1 1 1 1 2 3
graphics
drawing
algorithm
CO2:Analyse
different
3 3 3 2 2 1 1 1 1 1 1 3
transformations
and animation

[L=1, M=2, H=3]

PROGRAM SPECIFIC OUTCOME (PSO):

PSO: Ability to interpret and analyze network specific and cyber security issues, automation in real
Word environment.
PSO2: Ability to Design and Develop Mobile and Web-based applications under realistic constraints.

7
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

CO-PSO Mapping
CO’s PSO1 PSO2

CO 1: Generate the report based on the Projects carried out for


demonstrating the ability to apply the knowledge of engineering field 2 2
during training

CO 2: Demonstrate Competency in relevant engineering fields 2 2


through problem identification, formulation and solution

8
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

5. SYLLABUS
Class: VI Sem. B.Tech. Evaluation
Branch: Computer Engg. Examination Time =Three (3) Hours
Schedule per Week Maximum Marks = 50
Practical Hrs.: 2 [Sessional/Mid-term (30) & Endterm (20)]

6. BOOKS: -
8.1 Text books:-
1. Hearn and Baker: Computer Graphics, PHI
2. Multimedia Systems Design, Prabhat Andleigh and Thakkar, PHI..
8.2 Reference Books:-

9
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

1. J. Foley, A. Van Dam, S. Feiner, J. Hughes: Computer Graphics- Principles and Practice, Pearson

9. INSTRUCTIONAL METHODS:-
9.1. Direct Instructions:
I. White board presentation
9.2. Interactive Instruction:
I. Algorithms
9.3. Indirect Instructions:

I. Problem solving
10. LEARNING MATERIALS: -
1. Text/Lab Manual
2. https://www.jecrcfoundation.com/student-corner/lab-videos

11. ASSESSMENT OF OUTCOMES: -


1. End term Practical exam (Conducted by RTU, KOTA)
2. Daily Lab interaction.

12. OUTCOMES WILL BE ACHIEVED THROUGH FOLLOWING: -


1. Lab Teaching (through marker and white board).
2. Discussion on Algorithms.
3. Lab Experiment Execution.

13. INSTRUCTIONS OF LAB


DO’s
1. Please switch off the Mobile/Cell phone before entering Lab.
2. Enter the Lab with complete source code and data.
3. Check whether all peripheral are available at your desktop before proceeding for program.
4. Intimate the lab in charge whenever you are incompatible in using the system or
in case software get corrupted/ infected by virus.
5. Arrange all the peripheral and seats before leaving the lab.
6. Properly shutdown the system before leaving the lab.
7. Keep the bag outside in the racks.
8. Enter the lab on time and leave at proper time.
9. Maintain the decorum of the lab.
10. Utilize lab hours in the corresponding experiment.
10
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

11. Get your Cd / Pen drive checked by lab in charge before using it in the lab.
DON’TS
1. No one is allowed to bring storage devices like Pan Drive /Floppy etc. in the lab.
2. Don’t mishandle the system.
3. Don’t leave the system on standing for long
4. Don’t bring any external material in the lab.
5. Don’t make noise in the lab.
6. Don’t bring the mobile in the lab. If extremely necessary then keep ringers off.
7. Don’t enter in the lab without permission of lab in charge.
8. Don’t litter in the lab.
9. Don’t delete or make any modification in system files.
10. Don’t carry any lab equipment outside the lab.

INSTRUCTIONS FOR STUDENT

BEFORE ENTERING IN THE LAB

• All the students are supposed to prepare the theory regarding the next program.
• Students are supposed to bring the practical file and the lab copy.
• Previous programs should be written in the practical file.
• Any student not following these instructions will be denied entry in the lab.

WHILE WORKING IN THE LAB


• Adhere to experimental schedule as instructed by the lab in charge.
• Get the previously executed program signed by the instructor.
• Get the output of the current program checked by the instructor in the lab copy.
• Each student should work on his/her assigned computer at each turn of the lab.
• Take responsibility of valuable accessories.
• Concentrate on the assigned practical and do not play games.
• If anyone caught red handed carrying any equipment of the lab, then he will have to face serious
consequences.

11
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

14. EXPERIMENTS

Experiment No. 1

Aim:

To learn basic graphic’s function

Description

BASIC GRAPHICS FUNCTION

INITGRAPH
• Initializes the graphics system.

Declaration
• Void far initgraph(int far *graphdriver)

Remarks
• To start the graphic system, you must first call initgraph.
• Initgraph initializes the graphic system by loading a graphics driver from disk (or
validating a registered driver) then putting the system into graphics mode.
• Initgraph also resets all graphics settings (color, palette, current position, viewport, etc) to
their defaults then resets graph.
GETPIXEL, PUTPIXEL
• Getpixel gets the color of a specified pixel.
• Putpixel places a pixel at a specified point.

Declaration
• Unsigned far getpixel(int x, int y)
• Void far putpixel(int x, int y, int color)

Remarks
• Getpixel gets the color of the pixel located at (x,y);
• Putpixel plots a point in the color defined at (x, y).
Return value
• Getpixel returns the color of the given pixel.
• Putpixel does not return.

12
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

CLOSE GRAPH
• Shuts down the graphic system.

Declaration
• Void far closegraph(void);
Remarks
• Close graph deallocates all memory allocated by the graphic system.
• It then restores the screen to the mode it was in before you called initgraph.

Return value
• None.
ARC, CIRCLE, PIESLICE
• arc draws a circular arc.
• Circle draws a circle
• Pieslice draws and fills a circular pieslice

Declaration
• Void far arc(int x, int y, int stangle, int endangle, int radius);
• Void far circle(int x, int y, int radius);
• Void far pieslice(int x, int y, int stangle, int endangle, int radius);

Remarks
• Arc draws a circular arc in the current drawing color
• Circle draws a circle in the current drawing color
• Pieslice draws a pieslice in the current drawing color, then fills it using the current fill
pattern and fill color.

ELLIPSE, FILLELIPSE, SECTOR


• Ellipse draws an elliptical arc.
• Fillellipse draws and fills ellipse.
• Sector draws and fills an elliptical pie slice.

Declaration
• Void far ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius)
• Void far fillellipse(int x, int y, int xradius, int yradius)
• Void farsectoe(int x, int y, int stangle, int endangle, int xradius, int yradius)

Remarks
• Ellipse draws an elliptical arc in the current drawing color.
• Fillellipse draws an elliptical arc in the current drawing color and then fills it with fill color
and fill pattern.
13
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

• Sector draws an elliptical pie slice in the current drawing color and then fills it using the
pattern and color defined by setfillstyle or setfillpattern.
FLOODFILL
• Flood-fills a bounded region.

Declaration
• Void far floodfill(int x, int y, int border)

Remarks
• Floodfills an enclosed area on bitmap device.
• The area bounded by the color border is flooded with the current fill pattern and fill color.
• (x,y) is a “seed point”
➢ If the seed is within an enclosed area, the inside will be filled.
➢ If the seed is outside the enclosed area, the exterior will be filled.
• Use fillpoly instead of floodfill wherever possible so you can maintain code compatibility
with future versions.
• Floodfill doesnot work with the IBM-8514 driver.

Return value
• If an error occurs while flooding a region, graph result returns ‘1’.

GETCOLOR, SETCOLOR
• Getcolor returns the current drawing color.
• Setcolor returns the current drawing color.

Declaration
• Int far getcolor(void);
• Void far setcolor(int color)

Remarks
• Getcolor returns the current drawing color.
• Setcolor sets the current drawing color to color, which can range from 0 to getmaxcolor.
• To set a drawing color with setcolor , you can pass either the color number or the
equivalent color name.

LINE,LINEREL,LINETO
• Line draws a line between two specified pints.
• Onerel draws a line relative distance from current position(CP).
• Linrto draws a line from the current position (CP) to(x,y).

14
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

Declaration
• Void far lineto(int x, int y)
Remarks
• Line draws a line from (x1, y1) to (x2, y2) using the current color, line style and thickness.
It does not update the current position (CP).
• Linerel draws a line from the CP to a point that is relative distance (dx, dy) from the CP,
then advances the CP by (dx, dy).
• Lineto draws a line from the CP to (x, y), then moves the CP to (x,y).

Return value
• None

RECTANGLE
• Draws a rectangle in graphics mode.
Declaration
• Void far rectangle(int left, int top, int right, int bottom)

Remarks
• It draws a rectangle in the current line style, thickness and drawing color.
• (left, top) is the upper left corner of the rectangle, and (right, bottom) is its lower right
corner.

Return value
• None.

Experiment No. 2

Aim:

To draw pixel, line, ellipse, heart, smiley face and circle using function

Source Code:

/* String */

// C Implementation for putpixel()


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

// driver code
15
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

int main()
{
// gm is Graphics mode which is
// a computer display mode that
// generates image using pixels.
// DETECT is a macro defined in
// "graphics.h" header file
int gd = DETECT, gm, color;

// initgraph initializes the


// graphics system by loading a
// graphics driver from disk
initgraph(&gd, &gm, "");

// putpixel function
putpixel(85, 35, GREEN);
putpixel(30, 40, RED);
putpixel(115, 50, YELLOW);
putpixel(135, 50, CYAN);
putpixel(45, 60, BLUE);
putpixel(20, 100, WHITE);
putpixel(200, 100, LIGHTBLUE);
putpixel(150, 100, LIGHTGREEN);
putpixel(200, 50, YELLOW);
putpixel(120, 70, RED);

getch();

// closegraph function closes the


// graphics mode and deallocates
// all memory allocated by
// graphics system .
closegraph();

return 0;
}

Output

16
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

/*C graphics program to draw a line.*/

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

main()
{
int gd = DETECT, gm;

//init graphics
initgraph(&gd, &gm, "C:/TURBOC3/BGI");
/*
if you are using turboc2 use below line to init graphics:
initgraph(&gd, &gm, "C:/TC/BGI");

// C++ Implementation for drawing line


#include <graphics.h>

// driver code
int main()
{
// gm is Graphics mode which is a computer display
// mode that generates image using pixels.
// DETECT is a macro defined in "graphics.h" header file
int gd = DETECT, gm;

// initgraph initializes the graphics system


// by loading a graphics driver from disk
initgraph(&gd, &gm, "");

// line for x1, y1, x2, y2


line(150, 150, 450, 150);

// line for x1, y1, x2, y2


line(150, 200, 450, 200);

// line for x1, y1, x2, y2

17
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

line(150, 250, 450, 250);

getch();

// closegraph function closes the graphics


// mode and deallocates all memory allocated
// by graphics system .
closegraph();
}

Output

/* Ellipse*/
#include<stdio.h>
#include<graphics.h>
#include<conio.h>

int main(){
int gd = DETECT,gm;
int x ,y;
initgraph(&gd, &gm, "X:\\TC\\BGI");
/* Initialize center of ellipse with center of screen */
x = getmaxx()/2;
y = getmaxy()/2;

outtextxy(x-100, 50, "ELLIPSE Using Graphics in C");


/* Draw ellipse on screen */
ellipse(x, y, 0, 360, 120, 60);

getch();
closegraph();
return 0;
}
Output

18
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

/*Heart*/

#include<constream.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cleardevice();

for(int l=1;l<=50;l++)
{
int b=1;
while(!kbhit())
{
for(int i=1;i<=20;i++)

settextstyle(3,0,5);
outtextxy(270,230,"Ankit");
arc(280,250,400,2000,50);
arc(355,250,700,500,50);
line(320,350,235,270);
line(320,350,400,270);
b++;

delay(8);
cleardevice();

if(b==70)
{
19
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

break;
}}}}
Output

/* Smiley Face*/
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int gd = DETECT,gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
//for head
circle(200,200,30);
//for left eye
circle(190,190,5);
arc(190,190,50,130,10);
//for right eye
circle(210,190,5);
arc(210,190,50,130,10);
//for smiley lips
arc(200,210,180,360,10);
line(187,210,193,210);
line(207,210,213,210);
//for nose
line(198,195,195,200);
line(202,195,205,200);
line(195,200,200,205);
line(205,200,200,205);
getch();
closegraph();
}

20
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

OUTPUT:

// C Implementation for drawing circle


#include <graphics.h>

//driver code
int main()
{
// gm is Graphics mode which is
// a computer display mode that
// generates image using pixels.
// DETECT is a macro defined in
// "graphics.h" header file
int gd = DETECT, gm;

// initgraph initializes the


// graphics system by loading a
// graphics driver from disk
initgraph(&gd, &gm, "");

// circle fuction
circle(250, 200, 50);

getch();

// closegraph function closes the


// graphics mode and deallocates
// all memory allocated by
// graphics system .
closegraph();

return 0;
}
Output

21
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

Viva Question:

1. Draws a rectangle in graphics mode.


2. What are the line attributes
3. What are the ellipse attributes
4. What are the circle attributes

Experiment No. 3

Aim:

To implement Bresenham’s Algorithm for Line Drawing

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 end-points and store the left end-points 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.

22
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

12. Close Graph.


13. Stop.
Source Code:

/* C program to implement Lexical Analyzer */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x,y,x1,y1,x2,y2,p,dx,dy; int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\tc\\BGI:"); printf("\nEnter the x-coordinate of the
first point ::"); scanf("%d",&x1);
printf("\nEnter the y-coordinate of the first point ::");
scanf("%d",&y1);
printf("\nEnter the x-coordinate of the second point ::");
scanf("%d",&x2);
printf("\nEnter the y-coordinate of the second point ::");
scanf("%d",&y2);
x=x1; y=y1; dx=x2-x1; dy=y2-y1;
putpixel(x,y,2);
p=(2dy-dx);

while(x<=x2)
{
if(p<0)
{
x=x+1;
p=2*x-dx;
}
else
{
23
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

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

putpixel(x,y,7);
}
getch();
closegraph();
}

OutPut:

Result: Hence, Bresenham’s algorithm is implemented.

Viva Questions:

1. Differentiate DDA and Bresenham’s Line drawing algorithm

2. Explain the Bresenham’s Line drawing algorithm

24
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

Experiment No. 4

AIM: To Draw A Line Using DDA Algorithm


Algorithm

1. Start.
2. declare gdriver=DETECT,gmode.
3. Initialise the graphic mode with the path location in TC folder.
4. Input the two line end-points and store the left end-points 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).
8. 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.
14. Declare variables x,y,x1,y1,x2,y2,k,dx,dy,s,xi,yi and also
Source Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x,y,x1,x2,y1,y2,k,dx,dy,s,xi,yi;

int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\tc\\bgi:");
25
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

printf("enter first point");


scanf("%d%d",&x1,&y1);
printf("enter second point");
scanf("%d%d",&x2,&y2); x=x1;
y=y1;
putpixel(x,y,7);
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
s=abs(dx);
else
s=abs(dy);
xi=dx/s;

yi=dy/s;

x=x1;

y=y1;
putpixel(x,y,7);
for(k=0;k<s;k++)
{
x=x+xi;

y=y+yi;

putpixel(x,y,7);
}
getch();

closegraph();
}
Output:

26
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

Result: Hence, Line Using DDA Algorithm is implemented.

Viva questions:

1. Differentiate DDA and Bresenham’s Line drawing algorithm


2. What is DDA Algorithm
3. What does DDA stands for?

Experiment No.5

Aim: To Draw A Circle Using Bresenham’s Algorithm

Algorithm

1. Start.
2. Declare variables x,y,p and also declare gdriver=DETECT,gmode.
3. Initialise the graphic mode with the path location in TC folder.
4. Input the radius of the circle r.
5. Load x-0,y=r,initial decision parameter p=1-r.so the first point is (0,r).
6. Repeat Step 7 while (x<y) and increment x-value simultaneously.
7. If (p>0),do p=p+2*(x-y)+1.
8. Otherwise p=p+2*x+1 and y is decremented simultaneously.
9. Then calculate the value of the function circlepoints() with p.arameters (x,y).
10. Place pixels using putpixel at points (x+300,y+300) in specified colour in circlepoints()
function shifting the origin to 300 on both x-axis and y-axis.
11. Close Graph.
12. Stop.
Source Code:

#include<stdio.h>
#include<conio.h>
27
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

#include<graphics.h>
void circlepoints(int,int);
void main()
{
int x,y,p,r;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\tc\\bgi:"); clrscr();
printf("enter the radius");
scanf("%d",&r); x=0;y=r;p=1-r; while(x<y)
{
x++;
if(p>0)
{
}
else
p=p+2*(x-y)+1; y--;
p=p+2*x+1;
circlepoints(x,y);
}
getch(); closegraph();
}
void circlepoints(int x,int y)
{
putpixel(x+300,y+300,8);
putpixel(x+300,-y+300,8);
putpixel(-x+300,y+300,8);
putpixel(-x+300,-y+300,8);
putpixel(y+300,x+300,8);
putpixel(y+300,-x+300,8);
putpixel(-y+300,x+300,8);
putpixel(-y+300,-x+300,8);
}
{
putpixel(x+300,y+300,8); putpixel(x+300,-y+300,8); putpixel(-
x+300,y+300,8); putpixel(-x+300,-y+300,8);
putpixel(y+300,x+300,8); putpixel(y+300,-x+300,8); putpixel(-
y+300,x+300,8); putpixel(-y+300,-x+300,8);
}

Output:

28
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

Enter the radius 90

Result: Hence, Circle Using Bresenham’s Algorithm is implemented.

Viva Questions:

1. Explain Circle Function


2. Explain Bresenham’s Circle drawing algorithm

Experiment No. 6

Aim:

To Draw An Ellipse Using Mid-Point Ellipse Drawing Algorithm.

Algorithm

1. Start.
2. Initialize the graphic system using initgraph function.
29
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

3. Get the input of radius of major and minor arc from the user.
4. Store the values of major and minor arc in an another variable.
5. Square the values of major and minor arc.
6. Calculate decision parameter P = (square of minor axis – (square of major axis*minor axis)
+ (0.25* square of major axis).
7. Put the pixels symmetrically at = (0, length of minor axis).
8. while (2*(square of minor axis*x)<=2*(square of major axis*y)), repeat steps 9 to step 17.
9. increment x axis by 1.
10. If P < 0
11. new P = (P+( square of minor axis* square of major axis)+ square of major axis)
12. Else
13. new P = (P+( square of minor axis*x axis)-(2*square of major axis*y axis)+ square of
minor axis).
14. Decrement y by 1.
15. End of step 10 if else structure.
16. Plot symmetric points of ellipse in each quadrant.
17. End of step 8 loop.
18. This will give us ellipse only across minor axis now to draw an ellipse across major axis we
proceed further.
19. Get last point of ellipse in 1st quadrant.
20. Initialize e = square of (x axis+.5)
21. Initialize f = square of (y axis-1).
22. Decision parameter P1 = ((square of minor axis*e)+( square of major axis*f)-( square of
minor axis* square of major axis).
23. While y axis != 0 repeat steps 24 to step 32.
24. If P1>0
25. New P1 = (P1+ square of major axis-(2* square of major axis*x axis)).
26. Else
27. New P1 = (P1+(2*square of minor axis*(x axis+1))-(2* square of major axis*(y axis-

30
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

1))+square of major axis).


28. Increment x axis by 1.
29. End of step 25 if else structure
30. Decrement y axis by 1.
31. Plot symmetric point in all quadrants
32. End of step 23 while loop.
33. Close the graphic system.
34. Stop.

Source Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void ellips(int x,int y);
void completellipse(int r,int g,int u,int v)
{
float s,k,e,f,x; double p1,p2; s=r;k=g; e=(pow((s+.5),2));
f=(pow((k-1),2));
p2=((u*e)+(v*f)-(u*v)); ellips(s,k);
while(k>=0)
{
if(p2>0)
p2=(p2+v-(2*v*s));
else
{
}
k--;
p2=(p2+(2*u*(s+1))-(2*v*(k-1))+v); s++;
ellips(s,k);
}
}
void main()
{
int gdriver=DETECT,gmode; int a,b,x,y;
long u,v,p1; initgraph(&gdriver,&gmode,"C:\\tc\\bgi::"); printf("\n enter the length of major axis:");
scanf("\t%d",&a);
printf("\n enter the length of minor axis:"); scanf("\t%d",&b);
x=0;
y=b; u=pow(b,2);
31
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

v=pow(a,2);
p1=(u-(v*b)+(.25*v)); ellips(x,y); while(2*(u*x)<=2*(v*y))
{
x++;
if(p1<0)
p1=(p1+(2*u*v)+v);
else
{
}
p1=(p1+(2*u*x)-(2*v*y)+u); y--;
ellips(x,y);
}
completellipse(x,y,u,v);
getch();
closegraph();
}
void ellips(int x,int y)
{
putpixel(x+200,y+200,8); putpixel(-x+200,y+200,8); putpixel(x+200,-y+200,8); putpixel(-x+200,-y+200,8);
}

Output:

Viva Questions:

1. Explain Ellipse Function


2. Explain Mid-Point Ellipse Drawing Algorithm

32
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

Experiment No. 7

Aim: To implement Cohen Sutherland 2D line clipping algorithm

Algorithm

1. Start.
2. Initialize the graphic system using initgraph function.
3. Get the input of window co ordinates from the user and draw a window.
4. Get the input of line co ordinates from user and draw the line.
5. Calculate the region code of each end point of line using relation given in steps 6 to step
6. Let (x,y) be the co ordinates of end point of line and (xmin,ymin), (xmax,ymax) be co
ordinates of world window
7. If y –ymax = +ve
8. MSB region code = 1.
9. Else MSB region code = 0.
10. If ymin – y = +ve
11. Region code = 1.
12. Else Region code = 0.
13. If x – xmax = +ve
14. Region code = 1.
15. Else Region code = 0.
16. If xmin – x = +ve
17. LSB Region code = 1.
18. Else LSB Region code = 0.
19. Calculate region code of both end points.
20. Logically and both region code.
21. If Logically anded result is = 0
22. Line is not a clipping candidate.
23. Else.
24. Line is a clipping candidate.

33
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

25. Calculate slope of line using formula slope=(y2-y1)/(x2-x1).


26. If line is to be horizontally clipped.
27. New y = ymin or ymax.
28. New x = x1 + ((new y - y1)/slope).
29. If line is to be vertically clipped.
30. New x = xmin or xmax.
31. New y = y1+slope*(new x –x1).
32. Clip the lines from these intersection points.
33. Display the new line.
34. Close the graphic system.
35. Stop.
.

Source code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void storepoints(int,int,int,int,int,int,int[]); void main()
{
int gdriver=DETECT,gmode;
int x1,x2,y1,y2,xmax,ymax,xmin,ymin,a[10],b[10],xi1,xi2,yi1,yi2,flag=0; float m;
int i; clrscr();
printf("output"); printf("\n");
printf("enter the value of x1,y1,x2,y2: >"); scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
printf("enter the value of xmax,ymax,xmin,ymin:");
scanf("%d%d%d%d",&xmax,&ymax,&xmin,&ymin);
storepoints(x2,y2,ymin,ymax,xmax,xmin,b); for(i=1;i<=4;i++)
{
if(a[i]*b[i]==0)
flag=1;
else
}
flag=0;
if(flag==1)
{
m=(y2-y1)/(x2-x1); xi1=x1;
yi1=y1;
}
34
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

if(a[1]==1)
{
}
else
{
yi1=ymax; xi1=x1+((1/m)*(yi1-y1));
if(a[2]==1)
{
yi1=ymin; xi1=x1+((1/m)*(yi1-y1));
}
}
if(a[3]==1)
{
xi1=xmax; yi1=y1+(m*(xi1-x1));
}
if(a[4]==1)
{
}
else
xi1=xmin; yi1=y1+(m*(xi1-x1));
if(b[1]==1)
{
}
else
yi2=ymax; xi2=x2+((1/m)*(yi2-y2));
if(b[2]==1)
{
}
else
yi2=ymin; xi2=x2+((1/m)*(yi2-y2));
if(b[3]==1)
{
clrscr();
}
else
xi2=xmax; yi2=y2+((1/m)*(xi2-x2));
if(b[4]==1)
{
xi2=xmin; yi2=y2+(m*(xi2-x2));
}
initgraph(&gdriver,&gmode,"c://tc//bgi:");

35
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
delay(5000);
closegraph();
clrscr();
initgraph(&gdriver,&gmode,"c://tc//bgi:");
line(xi1,yi1,xi2,yi2);
rectangle(xmin,ymin,xmax,ymax); if(flag==0)
{
printf("\n no clipping is required");
}
getch(); closegraph();
}
void storepoints(int x1,int y1,int ymax,int xmax,int xmin,int ymin,int c[10])
{
if((y1-ymax)>0)
c[1]=1;
else
c[1]=0;
if((ymin-y1)>0)
c[2]=1;
else
c[2]=0;
if((x1-xmax)>0)
c[3]=1;
else
c[3]=0;
if((xmin-x1)>0)
c[4]=1;
else
}
c[4]=0;
}
Output:
enter the value of
x1,y1,x2,y2: >10 10
100
100
enter the value of
xmax,ymax,xmin,ymin50 50
0

36
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

0
Result: Hence, Cohen Sutherland 2D line clipping algorithm is implemented

Viva Questions:

1. What is Line Clipping


2. Explain Cohen Suthertland Line Clipping in detail

Experiment No. 8

Aim:
To rotate an object about origin

Source code:

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h> void main()
{
clrscr();
int graphdriver=DETECT,graphmode; initgraph(&graphdriver,&graphmode,"...\\bgi");
int x,y,x1,a[3][3];
double b[3][3],c[3][3];
cout<<"\n Enter Ist coordinates of triangle:";
cin>>a[0][0]>>a[1][0];
cout<<"\n Enter 2nd coordinates of triangle:";

37
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

cin>>a[0][1]>>a[1][1];
cout<<"\n Enter 3rd coordinates of triangle:";
cin>>a[0][2]>>a[1][2];
line(a[0][0],a[1][0],a[0][1],a[1][1]);
line(a[0][1],a[1][1],a[0][2],a[1][2]);
line(a[0][0],a[1][0],a[0][2],a[1][2]);
getch(); cleardevice();
cout<<"\n Enter angle of rotation:\n"; cin>>x;
b[0][0]=b[1][1]=cos((x*3.14)/180);
b[0][1]=-sin((x*3.14)/180);
b[1][0]=sin((x*3.14)/180); b[2][2]=1; b[2][0]=b[2][1]=b[0][2]=b[1][2]= 0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{ c[i][j]=0;
for (int k=0; k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
} x1=(c[i][j]+0.5);
a[i][j]=x1;
}
}
cout<<"\n Triangle after rotation is:\n" ;
line(a[0][0],a[1][0],a[0][1],a[1][1]);
line(a[0][1],a[1][1],a[0][2],a[1][2]);
line(a[0][0],a[1][0],a[0][2],a[1][2]);
getch(); closegraph();
}

Output:

38
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

Result: Hence, an object about origin, is rotated.

Viva Questions:

1. Explain Triangle About Origin.

Experiment No.9

Aim:
To scale an object with scaling factors along X and Y directions

39
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

Source Program:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm; initgraph(&gd, &gm,"");
cleardevice();
int x1,y1,x2,y2,x3,y3,x4,y4;
float sx,sy;
cout<<"Enter the first coordinates of triangle\n";
cin>>x1>>y1;
cout<<"Enter the second coordinates of triangle\n";
cin>>x2>>y2;
cout<<"Enter the third coordinates of triangle\n";
cin>>x3>>y3;
int poly[8]={x1,y1,x2,y2,x3,y3,x1,y1};
cleardevice();
drawpoly(4,poly); getch();
cout<<"Enter the scaling factors\n";
cin>>sx>>sy;
x4=sx*x1-x1; y4=sy*y1-y1;
x1=sx*x1-x4; y1=sy*y1-y4; x2=sx*x2-x4;
y2=sy*y2-y4; x3=sx*x3-x4; y3=sy*y3-y4;
poly[0]=x1; poly[1]=y1; poly[2]=x2; poly[3]=y2;
poly[4]=x3; poly[5]=y3; poly[6]=x1; poly[7]=y1;
getch();
cleardevice();
drawpoly(4,poly);
getch();
closegraph();
}
Output:

40
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

Result:

Hence, scaling factors along with X and Y directions are implemented.

Viva Questions:

1. What is scaling
2. What are the different transformation

41
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

Experiment No. 10

Aim:
To translate an object with translation parameters in X and Y directions

Description:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
void main()
{
clrscr();
int graphdriver=DETECT,graphmode; initgraph(&graphdriver,&graphmode,"...\\bgi");
int x,y,x1,y1,x2,y2,x3,y3;
cout<<"\n Enter Ist coordinates of triangle:"; cin>>x1>>y1;
cout<<"\n Enter 2nd coordinates of triangle:"; cin>>x2>>y2;
cout<<"\n Enter 3rd coordinates of triangle:"; cin>>x3>>y3;
cleardevice(); line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x1,y1,x3,y3); getch(); cleardevice();
cout<<"\n Enter translatio factors :\n"; cin>>x>>y;
x1-=x;
y1-=y;
x2-=x;
y2-=y;
x3-=x;
y3-=y;
cleardevice(); line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x1,y1,x3,y3);
getch();
closegraph();
}
42
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

Output:

Result:
Hence translation of an object is implemented

Viva Questions:
1. What is basic transformation
2. What is translation

Experiment No. 11

Aim: To Rotate a Point About a Point

Source Code
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h> void main()
{
clrscr();
int gm,gd=DETECT; initgraph(&gd,&gm,"");
int h,k,x1,y1,x2,y2,x3,y3; float t;
cout<<" OUTPUT"<<endl;

43
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

cout<<"Enter the coordinates of point"<<endl; cin>>x2>>y2;


putpixel(x2,y2,2);
cout<<"Enter the coordinates of point around which rotation is done"<<endl; cin>>h>>k;
putpixel(h,k,2);
cout<<"Enter the angle for rotation"<<endl; cin>>t;
cleardevice(); x1=(h*cos(t))-(k*sin(t));
y1=(h*sin(t))+(k*cos(t)); x3=x1+x2-h;
y3=y1+y2-k;
cout<<"Point after rotation is:";
putpixel(x3,y3,2);
getch();
closegraph();
}
Output:

Result: Hence Rotation of a Point about a Point is implemented

44
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

Viva Question:
1. What is rotation
2. What is basic transformation

Experiment No. 12

Aim: To Rotate a Point about Origin

Source Code
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h> void main()
{
clrscr();
int gm,gd=DETECT;
initgraph(&gd,&gm,"");
int h,k,x1,y1,x2,y2,x3,y3;
float t;
cout<<" OUTPUT"<<endl;
cout<<"Enter the coordinates of point"<<endl; cin>>x2>>y2;
putpixel(x2,y2,2);
cout<<"Enter the angle for rotation"<<endl; cin>>t;
cleardevice();
x1=int(x2*cos(t*3.14/180))-(y2*sin(t*3.14/180));
y1=int(x2*sin(t*3.14/180))+(y2*cos(t*3.14/180));
cout<<"Point after rotation is:";
putpixel(x1,y1,2);
getch();

45
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

closegraph();
}
Output:

Result: Hence Rotation of a Point About Origin is implemented

Viva Question:
1. What is transformation

Experiment No. 13

Aim: To Reflect a Triangle

Source Code
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h> void main()
{
clrscr();
int graphdriver=DETECT,graphmode; initgraph(&graphdriver,&graphmode,"...\\bgi");
int x,y,x1,a[3][3];
double b[3][3],c[3][3];
cout<<"\n Enter Ist coordinates of triangle:"; cin>>a[0][0]>>a[1][0];
cout<<"\n Enter 2nd coordinates of triangle:"; cin>>a[0][1]>>a[1][1];
cout<<"\n Enter 3rd coordinates of triangle:"; cin>>a[0][2]>>a[1][2];

46
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

cout<<"\n Enter 1. for reflection in x-axis:\n"; cout<<"\n Enter 2. for reflection in y-axis:\n";
cout<<"\n Enter 3. for reflection in both the axis:\n"; cin>>x;
cleardevice(); line(320,0,320,479); line(0,240,639,240);
line(a[0][0],a[1][0],a[0][1],a[1][1]);
line(a[0][1],a[1][1],a[0][2],a[1][2]);
line(a[0][0],a[1][0],a[0][2],a[1][2]);
switch(x)
{
case 1:b[0][0]=640-a[0][0];
b[0][1]=640-a[0][1];
b[0][2]=640-a[0][2];
b[1][0]=a[1][0];
b[1][1]=a[1][1];
b[1][2]=a[1][2]; line(320,0,320,479); line(0,240,639,240);
line(b[0][0],b[1][0],b[0][1],b[1][1]);
line(b[0][1],b[1][1],b[0][2],b[1][2]);
line(b[0][0],b[1][0],b[0][2],b[1][2]);
getch(); break;
case 2:b[1][0]=480-a[1][0];
b[1][1]=480-a[1][1];
b[1][2]=480-a[1][2];
b[0][0]=a[0][0];
b[0][1]=a[0][1];
b[0][2]=a[0][2]; line(320,0,320,479); line(0,240,639,240);
line(b[0][0],b[1][0],b[0][1],b[1][1]);
line(b[0][1],b[1][1],b[0][2],b[1][2]);
line(b[0][0],b[1][0],b[0][2],b[1][2]);
getch(); break;
case 3: b[0][0]=640-a[0][0];
b[0][1]=640-a[0][1];
b[0][2]=640-a[0][2];
b[1][0]=a[1][0];
b[1][1]=a[1][1];
b[1][2]=a[1][2]; line(320,0,320,479); line(0,240,639,240);
line(b[0][0],b[1][0],b[0][1],b[1][1]);
line(b[0][1],b[1][1],b[0][2],b[1][2]);
line(b[0][0],b[1][0],b[0][2],b[1][2]); b[1][0]=480-a[1][0];
b[1][1]=480-a[1][1];
b[1][2]=480-a[1][2];
b[0][0]=a[0][0];
b[0][1]=a[0][1];

47
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

b[0][2]=a[0][2]; line(320,0,320,479); line(0,240,639,240);


line(b[0][0],b[1][0],b[0][1],b[1][1]);
line(b[0][1],b[1][1],b[0][2],b[1][2]);
line(b[0][0],b[1][0],b[0][2],b[1][2]);
getch(); break;
}
getch(); closegraph();
}
Output:

48
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

Result: Hence Reflection a Triangle is implemented

Viva Question:
1. What is Shearing
2. What is reflection

Experiment No. 14

Aim: To Draw a Hut Using Simple Graphic Functions

Source Code
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
#include<process.h> void main()
{
int graphdriver=DETECT,graphmode; initgraph(&graphdriver,&graphmode,"...\\bgi");
line(100,100,150,50);

49
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

line(150,50,200,100); line(100,100,200,100); line(150,50,350,50); line(200,100,350,100);


line(350,50,350,100);
circle(150,75,10); rectangle(100,100,200,300);
rectangle(200,100,350,300); rectangle(250,175,300,225);
line(250,175,300,225); line(300,175,250,225); line(125,300,125,225); line(175,300,175,225);
arc(150,225,0,180,25);
getch(); closegraph();
}
Output:

Result: Hence a Hut Using Simple Graphic Functions is drawn

Viva Question:
1. What is graphics function

50
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

Experiment No. 15

Aim: To Fill a Polygon

Source Code

#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
#include<process.h> void main()
{
int graphdriver=DETECT,graphmode; initgraph(&graphdriver,&graphmode,"...\\bgi");
int p=1,x;
int a[12]={100,100,150,150,200,100,200,200,100,200,100,100};
drawpoly(6,a);
for(int i=100;i<200;i++)
{
p=1;
for(int j=100;j<=200;j++)
{
x=getpixel(j,i);
for(int d=0;d<11;d++)
{
if(j==a[d]&&i==a[d+1] ) break;
else
{
if(x>0&&d==10) p++;

51
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

if(p%2==0)
putpixel(j,i,4);
}
}
}
}
getch();
closegraph();
}

Output:

Result: Hence the Polygon is filled

Viva Question:
1. What is graphics function

52
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

Experiment No. 16

Aim: To draw a moving helicopter

Source Code
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>

void bird();
void handup();
void handdown();
void heli();
void para();
int a,b,c,d,e,k;

void main()
{

int gdriver = DETECT,gmode;


initgraph(&gdriver,&gmode,"C:\\Turboc3\\BGI");

//setcolor(GREEN);
b=0;
c=0;
d=0;

for(a=0;a<80;a++)
{
heli();
delay(100);
cleardevice();
k=k+3;
}

53
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

heli();
for(a=0;a<90;a++)
{
para();
heli();
if(a>20){k=k+6;}
delay(100);
cleardevice();
if(a<50){
b=b+2;
}
}

getch();

}
void heli(){
ellipse(100+k,200,270,90,20,15);
line(100+k,185,80+k,185);
line(100+k,215,80+k,215);
ellipse(80+k,210,160,270,15,5);
ellipse(80+k,190,90,170,15,5);
line(65+k,190,5+k,195);
line(65+k,210,5+k,200);
//circle(5+k,197,15);
if(a%2==0){
pieslice(5+k,197,30,60,15);
pieslice(5+k,197,210,240,15);
}
else{
pieslice(5+k,197,120,150,15);
pieslice(5+k,197,300,330,15);
}
bar(90+k,185,92+k,170);
bar(70+k,168,110+k,170);
line(94+k,190,94+k,196);

54
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

line(96+k,200,110+k,200);
ellipse(102+k,195,160,220,8,8);
line(90+k,215,85+k,230);
line(95+k,215,100+k,230);
bar(80+k,230,108+k,232);
}

void para()
{
arc(340,260+b,0,180,20);
arc(340,263+b,0,180,20);
line(320,263+b,330,280+b);
line(360,263+b,350,280+b);
line(330,280+b,331,290+b);
line(328,280+b,330,290+b);
line(350,280+b,348,290+b);
line(349,280+b,347,290+b);
ellipse(340,280+b,0,360,4,6);
rectangle(336,285+b,344,302+b);
bar(330,288+b,336,289+b);
bar(338,302+b,339,310+b);
bar(342,302+b,343,310+b);
line(336,302+b,339,312+b);
ellipse(344,312+b,0,360,2,1);
ellipse(338,312+b,0,360,2,1);

Output:

55
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

Result: Hence a moving helicopter is drawn


Viva Question:
1. What is graphics function
2. What is rectangle function

Experiment No. 17

Aim: To draw a moving fan

Source Code
#include<graphics.h> //for graphics
#include<stdio.h> //standard input/output
#include<string.h> //string function
#include<conio.h> //console input output
#include<math.h> //math calculation
#define PI 3.14 //value constant pi=3.14
char speed[4]; //array declaration for speed
int theta=0,change=1,temp=0,ch;
float x,y,r=100;
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi"); //path where ur BGI file is stored
strcpy(speed,"on 1");
outtextxy(200,20,"Working Fan");
outtextxy(150,50,"Use number 0 1 2 3 to change fan speed");
outtextxy(475,375,"Fan Speed");
fan:

56
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

do
{
cleardevice();
outtextxy(200,20,"Working Fan");
outtextxy(150,50,"Use number 0 1 2 3 to change fan speed");
outtextxy(475,375,"Fan Speed");
if(kbhit())
{
ch=getch();
if(ch=='0')
{
rectangle(495,395,535,410);
floodfill(515,405,15);
temp=1;
}
if(ch=='1')
{
strcpy(speed,"on 1");
change=1;
}
else if(ch=='2')
{
strcpy(speed,"on 2");
change=3;
}
else if(ch=='3')
{
strcpy(speed,"on 3");

57
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

change=18;
}
}
outtextxy(500,400,speed);
circle(320,240,(r/12));
circle(320,240,(r/6));
circle(320,240,(r/4));
x=r*(cos((PI * theta)/180));
y=r*sin((PI * theta)/180);
line(320+(x/4),240+(y/4),320+(1.6*x),230+(1.6*y)); /*draw the line for fan*/
line(320+(x/4),240+(y/4),320+(1.6*x),250+(1.6*y));
line(320+(1.6*x),230+(1.6*y),320+(1.6*x),250+(1.6*y));
x=r*cos((PI *(theta+120))/180);
y=r*sin((PI *(theta+120))/180);
line(320+(x/4),240+(y/4),320+(1.6*x),230+(1.6*y));
line(320+(x/4),240+(y/4),320+(1.6*x),250+(1.6*y));
line(320+(1.6*x),230+(1.6*y),320+(1.6*x),250+(1.6*y));
x=r*cos((PI * (theta+240))/180);
y=r*sin((PI * (theta+240))/180);
line(320+(x/4),240+(y/4),320+(1.6*x),230+(1.6*y));
line(320+(x/4),240+(y/4),320+(1.6*x),250+(1.6*y));
line(320+(1.6*x),230+(1.6*y),320+(1.6*x),250+(1.6*y));
if(temp!=1) delay(36/change);
else
{
ch=getche();
if(ch=='\r') exit(0);
if(ch=='0') temp=1;

58
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

if(ch=='1') //for speed


{
strcpy(speed,"on 1");
temp=0;
change=1;
}
else if(ch=='2')
{
strcpy(speed,"on 2");
temp=0;
change=3;
}
else if(ch=='3')
{
strcpy(speed,"on 3");
temp=0;
change=18;
}
else
{
strcpy(speed,"off!");
temp=1;
}
}
if(theta==360) theta=0;
theta++;
}
while(temp==0);

59
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

goto fan;
}

Output:

Result: Hence a moving fan is drawn

Viva Question:
1. What is graphics function
2. What is rectangle function

Experiment No. 18

60
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

Aim: To draw a moving car

Source Code

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

int main() {
int gd = DETECT, gm;
int i, maxx, midy;

/* initialize graphic mode */


initgraph(&gd, &gm, "X:\\TC\\BGI");
/* maximum pixel in horizontal axis */
maxx = getmaxx();
/* mid pixel in vertical axis */
midy = getmaxy()/2;

for (i=0; i < maxx-150; i=i+5) {


/* clears screen */
cleardevice();

/* draw a white road */


setcolor(WHITE);
line(0, midy + 37, maxx, midy + 37);

/* Draw Car */
setcolor(YELLOW);
setfillstyle(SOLID_FILL, RED);

line(i, midy + 23, i, midy);


line(i, midy, 40 + i, midy - 20);
line(40 + i, midy - 20, 80 + i, midy - 20);
line(80 + i, midy - 20, 100 + i, midy);
line(100 + i, midy, 120 + i, midy);
line(120 + i, midy, 120 + i, midy + 23);
line(0 + i, midy + 23, 18 + i, midy + 23);
arc(30 + i, midy + 23, 0, 180, 12);
line(42 + i, midy + 23, 78 + i, midy + 23);

61
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

arc(90 + i, midy + 23, 0, 180, 12);


line(102 + i, midy + 23, 120 + i, midy + 23);
line(28 + i, midy, 43 + i, midy - 15);
line(43 + i, midy - 15, 57 + i, midy - 15);
line(57 + i, midy - 15, 57 + i, midy);
line(57 + i, midy, 28 + i, midy);
line(62 + i, midy - 15, 77 + i, midy - 15);
line(77 + i, midy - 15, 92 + i, midy);
line(92 + i, midy, 62 + i, midy);
line(62 + i, midy, 62 + i, midy - 15);
floodfill(5 + i, midy + 22, YELLOW);
setcolor(BLUE);
setfillstyle(SOLID_FILL, DARKGRAY);
/* Draw Wheels */
circle(30 + i, midy + 25, 9);
circle(90 + i, midy + 25, 9);
floodfill(30 + i, midy + 25, BLUE);
floodfill(90 + i, midy + 25, BLUE);
/* Add delay of 0.1 milli seconds */
delay(100);
}

getch();
closegraph();
return 0;
}

62
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

Output

Result: Hence a moving car is drawn

Viva Question:
1. What is graphics function
2. What is rectangle function

Experiment No. 19

Aim: To draw a moving fish

Source Code
#include<time.h>
#include<conio.h>

63
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

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

void main()
{
int gd=DETECT,gm;
int x=10,y=200,x1=675,y1=380;
int stangle=35,endangle=140,radius=90;

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

while(!kbhit())
{
cleardevice();
setbkcolor(BLACK);

if(x<640)
{
x+=5;
y+=1;
arc(x,y,stangle,endangle+35,radius);
arc(x,y-110,190,323,radius+2);
circle(x+40,y-60,5);
line(x-90,y-90,x-90,y-8);
}
else
{
x1-=5;
y1-=1;
arc(x1,y1,stangle-30,endangle+4,radius);
arc(x1,y1-110,217,350,radius+2);
circle(x1-40,y1-60,5);
line(x1+90,y1-90,x1+90,y1-10);
}
setcolor(YELLOW);

64
Jaipur Engineering College and Research Centre,Jaipur
Department of Computer Science and Engineering
Computer Graphics & Multimedia Lab [5CS4 - 21]

delay(90);
}
closegraph();
}

Result: Hence a moving fish is drawn

Viva Question:
1. What is graphics function
2. What is rectangle function

65

You might also like