0% found this document useful (0 votes)
12 views81 pages

CG Lab Manual

Uploaded by

kumarishagun330
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)
12 views81 pages

CG Lab Manual

Uploaded by

kumarishagun330
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/ 81

Computer Graphics Laboratory S.E.C.

E(Sem-1) [2022-23]

STES’s
NBN SINGHAD SCHOOL OF ENGINEERING
Ambegaon (Bk), Pune
Department of Computer Engineering

LABORATORY MANUAL
2022-23
Computer Graphics Laboratory
SE-COMPUTER ENGINEERING
SEMESTER-1
Subject Code:210248
TEACHING SCHEME EXAMINATION SCHEME
Lectures: 4Hrs/Week Practical: 25 Marks
Practical: 4 Hrs/Week Term Work: 25
-: Name of Faculty :-
Prof. Pushpanjali S. Sajjanshetti
Prof . Asharani M. Chadchankar
Department of Computer Engineering
NBN Sinhgad School of Engineering

1| Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

Suggested List of Laboratory Experiments/Assignments


(Implementation of each problem statement is mandatory.)
Sr.
Group A
No.
1 Write C++ program to draw the line styles using DDA and Bresenham‘s algorithm (solid,
dotted, dashed, dash dot and thick). Inherit pixel class and Use Constructors.
2 Write C++ program to draw a Circle using DDA and Bresenham‘s algorithm. Inherit pixel
class and Use function overloading.
3 a) Write C++ program to draw the following pattern. Use DDA and Bresenham‘s Line
drawing algorithm

OR

b) Write C++ program to draw the following pattern. Use DDA and Bresenham‘s drawing
algorithm

Group B

4 Write C++ program to draw a 2-D object and perform following basic transformation,
Scaling b) Translation c) Rotation. Apply the concept of operator overloading.
5 Write C++ program generate fractal patterns using Koch Curves.

Group C

2| Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

6 Write OpenGL program to draw Sun Rise and Sun Set

7 a) Write C++ program to draw man walking in the rain with an umbrella. Apply the
concept of polymorphism.

Mini-Projects/ Case Study


8. Design and implement game / animation clip / Graphics Editor using open source graphics
library.

3| Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

Computer Graphics Lab

This is a step by step tutorial on how to install graphics.h for C++ on Linux [Ubuntu 20.04].
(Lines in bold are commands for the terminal)

Step 1:
Download the library required from given link:
http://download.savannah.gnu.org/releases/libgraph/libgraph-1.0.2.tar.gz

(Alternatively, you can visit http://download.savannah.gnu.org/releases/libgraph and find the latest


relevant release)

Step 2:
Launch the terminal.
(Use Ctrl + Alt + T for quick access)

Step 3:
Type

sudo apt-get install build-essential

and run the command (Press Enter).


IF you are logged in as Student, it may ask for the password. D not panic if the password is not
visible. That is a security measure. Type the password and press Enter to continue.
This will execute a few lines in the terminal. It may let you know if build-essential is already
installed or not. But installing multiple times does not cause any harm, so go ahead.

Step 4:
Following are the other libraries to be installed;

sudo apt-get install libsdl-image1.2 libsdl-image1.2-dev guile-2.0


sudo apt-get install guile-2.0-dev libsdl1.2debian libart-2.0-dev libaudiofile-dev
sudo apt-get install libesd0-dev libdirectfb-dev libdirectfb-extra libfreetype6-dev
sudo apt-get install libxext-dev x11proto-xext-dev libfreetype6 libaa1 libaa1-dev
sudo apt-get install libslang2-dev libasound2 libasound2-dev
sudo apt install libplib-dev

sudo apt-get install libqt4-dev


sudo apt-get install libjack0
sudo apt-get install libjack-dev
sudo apt-get install libasound2-dev
sudo apt-get install libsndfile1-dev

Step 5:
Find the package for the library on your computer. It is generally in the downloads folder.
The name may resemble something like “libgraph-1.0.2.tar.gz”.
4| Department of Computer Engineering, NBN SSOE,PUNE
Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

Step 6:
Extract the folder inside the archive.
Extracted folder tends to have the same name as the archive.

Step 7:
Change the terminal directory to the extracted folder.
Considering that the folder is in the Downloads directory, The command should look something
like this:
cd Downloads/libgraph-1.0.2
If you have any queries or get any errors, contact the lab faculty.

Step 8:
In the terminal, after changing to above mentioned directory, run the following command:
./configure

This will lead to a few lines being executed.

Step 9:
Now type make in the terminal and press Enter.

Step 10:
Then type
sudo make install

This will install the library.

Step 11:
Finally, type the following line:
sudo cp /usr/local/lib/libgraph.* /usr/lib

This concludes the installation process of the library graphics.h for the g++ compiler on Ubuntu
20.04 lts.

CG program Execution Steps:


➢ Go to command Prompt
ctrl+Alt+T

➢ open Text Editor commans:


gedit program_name.cpp //example: gedit p11.cpp

➢ Compile Program command:


g++ program_name -c //example: g++ p11.cpp -c

➢ Run program Command:

5| Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

g++ program_name -o traget_file -lgraph //example: g++ p11.cpp -o graph10 -lgraph

Open GL Program Execution steps:


gedit program_name.cpp //example: gedit p1.cpp

➢ Compile OpenGL Program command:


g++ programm_namecpp -lGL -lGLU -lglut //example: g++ p1.cpp -lGL -lGLU -lglut

➢ Run OpenGl program Command:


./a.out

6| Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

Assignment No. 1 (GROUP A)

Write C++ program to draw the line styles using


DDA and Bresenham‘s algorithm (solid, dotted,
Title dashed, dash dot and thick). Inherit pixel class
and Use Constructors.

Subject Computer Graphics Laboratory

Class S.E. (C.E)


Date

Signature

7| Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

Assignment No:1
Write C++ program for line drawing using Bradenham’s algorithm with patterns such
as solid, dotted, dashed, dash dot and thick
Title of Assignment: Draw a line with styles.

Problem Definition: Write C++ program for line drawing using DDA or
Bradenham’s algorithm with patterns such as solid, dotted, dashed, dash dot
and thick.

Prerequisite: Basic primitives of computer graphics and concepts of OOP.

Learning Objective: Understand the functions available in Graphics library.

Relevant Theory / Literature Survey:

LINE: It is the path between two end points. We can draw line by two methods
1] Using inbuilt function

2] Using pixel filling (by logic)

1] Using inbuilt function

Purpose: - draws a line between two specified points

Syntax: -void far line(int x1, int y1, int x2, int y2);

Comments: -Line() draws a line in the current color using current line style and
thickness.
Linestyle : This is a function used to sets the line style and width or pattern . Sets
8| Department of Computer Engineering, NBN SSOE,PUNE
Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

the style for all lines drawn by line, line to, rectangle,drawpoly, etc.

Syntax : void far setline style(int style, unsigned pattern, int thickness)

Total 5 styles are available in C Compiler

unpattern is a 16-bit pattern that applies only if line style is USERBIT_LINE (4).
In that case, whenever a bit in the pattern word is 1, the corresponding pixel in the
line is drawn in the current drawing color.
For example, a solid line corresponds to a unpattern of 0xFFFF (all pixels drawn),
while a dashed line can correspond to a unpattern of 0x3333(short dashes) or
0x0F0F(long dashes) or 0x3F3F (longer dashes).

Thickness: Sets the width of the line. Two available widths are given in the table.
Name Value Meaning
NORM_WIDTH 1 1 pixel
THICK_WIDTH 3 3 pixel

2] Using pixel filling (by logic)


• Solid Line

9| Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

We can use general line drawing algorithm to display solid line. Such as DDA,
Bradenham’s etc.
• Dotted Line
We can easily modify the general line drawing algorithm to
display dotted line. By plotting the alternate pixels in line we can
display the dotted line

• Dashed Line
We can easily modify the general line drawing algorithm to display dashed
line. We have to plot alternate group of pixels along the line to get dashed line.

• Thick Line
To produce a thick line we have to run two line drawing algorithms in parallel to
find the pixel along the line edges.

Properties of good line drawing algorithm


1. Straight lines should appear as straight lines.
2. They should start and end accurately.
3. Displayed lines should have constant brightness along their length, independent of
theline length and orientation

4. Line should be drawn rapidly

Setting font/text Style and Size


There are various functions in graphics library for text operations.
streetstyle() : Sets the current text characteristics i:e text font style, the text
direction and its font size

void far streetstyle(int Stylizing Horizontal/Verticality Size);


Available font styles are :

10 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

Two available directions are:

Name Value Direction


HORIZ_DIR 0 Left to Right
VERT_DIR 1 Bottom to
Top

Settextjustuify() : It is used for text alignment i:e left, right and centre

void far settextjustify(int hor, int vert)


Three available alignments are

Name Value Direction


LEFT_TEXT 0 Left
CENTER_TEXT 1 Center
RIGHT_TEXT 2 Right

Questions:-
Q 1: How to draw line with different styles without using inbuilt function?

Q 2: What are the properties of line drawing algorithm?


11 | Department of Computer Engineering, NBN SSOE,PUNE
Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

Q 3: Which arguments should pass for


line style?
Q 4: how to draw thick and thin line?

1. Conclusion:-
In This way we have studied that how to draw a line with line style.

12 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

#include <iostream>
#include <stdlib.h>
#include <graphics.h>
#include <math.h>
using namespace std;
int xmax,ymax,xmid,ymid;
class Line
{
public:
int x1,x2,y1,y2,ch;
void bss(int x1,int y1,int x2,int y2)
{
int dx,dy,x,y,s1,s2,ex,e,i,flag=0,temp;
dx=abs(x2-x1);
dy=abs(y2-y1);
x=x1;
y=y1;
putpixel(x+xmid,ymid-y,15);
if(x2>x1)
{
s1=1;
}
if(x2==x1)
{
s1=0;
}
if(x2<x1)
{
s1=-1;
}
if(y2>y1) {
s2=1;
}
if(y2==y1)
{
s2=0;
}
if(y2<y1)
{
s2=-1;
}
if(dy>dx)

13 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

{
temp=dx;
dx=dy;
dy=temp;
ex=1;
}
else
ex=0;

e=2*dy-dx;
i=1;

do
{
while(e>0)
{
if(ex==1)
x=x+s1;
else
y=y+s2;

e=e-2*dx;
}
while(e<0)
{
if(ex==1)
y=y+s2;
else
x=x+s1;
e=e+2*dy;
}
switch(ch)
{
case 1:
putpixel(x+xmid,ymid-y,15);
break;
case 2:
if(flag==0)
{putpixel(x+xmid,ymid-y,15);
delay(1000);}
if(i%5==0)
{

14 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

if(flag==1)
flag=0;
else
flag=1;
}
break;
case 3:
if(flag==0)
putpixel(x+xmid,ymid-y,15);
delay(100);
if(i%5==0)
{
if(flag==1)
flag=0;
else
flag=1;
}
if(i%3==0)
{
putpixel(x+xmid,ymid-y,15);
delay(1000);}
break;
case 4:
if(flag==0)
{delay(1000);
}
else
{
if(i%3==0)
{
putpixel(x+xmid,ymid-y,15);
delay(1000);
}
}
break;
case 5:
putpixel(x+xmid,ymid-y,15);
break;
}
i=i+1;
delay(50);
}while(i<=dx);

15 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

}
};
int main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,thick,wx,wy,i;
Line B;
cout<<"Enter two end points of line\n";
cin>>x1>>y1;
cin>>x2>>y2;
while(1)
{
cout<<"\nEnter the Style\n";
cout<<"1.Simple\n";
cout<<"2.Dash\n";
cout<<"3.Dash Dot\n";
cout<<"4.Dot\n";
cout<<"5.Thick\n";
cout<<"6.Exit\n";
cout<<"Enter your Style\n";
cin>>B.ch;
if(B.ch==5)
{

cin>>thick;
}
initgraph(&gd,&gm,NULL); cout<<"Enter The Thickness of line: ";

xmax=getmaxx();
ymax=getmaxy();
xmid=xmax/2;
ymid=ymax/2;

if(B.ch<=4)
{

B.bss(x1,y1,x2,y2);
delay(300);

}
else
{

16 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

B.bss(x1,y1,x2,y2);
delay(300);

if((y2-y1)/(x2-x1)<1)
{
wy=(thick-1)*sqrt(pow((x2-x1),2)+pow((y2-y1),2))/(2*fabs(x2-x1));
for(i=0;i<wy;i++)
{
B.bss(x1,y1-i,x2,y2-i);
delay(300);
B.bss(x1,y1+i,x2,y2+i);
delay(300);
}

}
else
wx=(thick-1)*sqrt(pow((x2-x1),2)+pow((y2-y1),2))/(2*fabs(y2-y1));
for(i=0;i<wx;i++)
{
B.bss(x1-i,y1,x2-i,y2);
delay(300);
B.bss(x1+i,y1,x2+i,y2);
delay(300);
}
}

if(B.ch==6)
{
cout<<"Exiting....";
exit(1);
}
closegraph();
}
return 0;
}

17 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

18 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

19 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

20 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

Assignment No. 2 (GROUP A)

Write C++ program to draw a Circle using


DDA and Bresenham‘s algorithm. Inherit
Title pixel class and Use function overloading.

Subject Computer Graphics Laboratory

Class S.E. (C.E)


Roll No.

Date

21 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

Assignment No:2
Write C++ program to draw circle using Bradenham’s algorithm

Title of Assignment: Draw a Circle.

Problem Definition: Write C++ program to draw circle using Bresenham‘s


algorithm.

2.1 Prerequisite: Line Generation algorithms.

2.2 Learning Objective:

Understand the concept of circle generation algorithms.

2.3 Relevant Theory / Literature Survey:

Drawing a circle on the screen is a little complex than drawing a line. There are
two popular algorithms for generating a circle − Bradenham’s Algorithm and
Midpoint Circle Algorithm. These algorithms are based on the idea of
determining the subsequent points required to draw the circle. Let us discuss the
algorithms in detail −

The equation of circle is


X2+Y2=r2, where r is radius.

22 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

For every pixel (x, y) it calculates, we draw a pixel in each of the 8 octants of the
circle as shown below,

Bresenham’s Algorithm
Now, we will see how to calculate the next pixel location from a previously
known pixel location (x, y). In Bradenham’s algorithm at any point (x, y) we
have two option either to choose the next pixel in the east i.e. (x+1, y) or in
th e south east i.e. (x+1, y-1).

And this can be decided by using the decision parameter d as:

• If d > 0, then (x+1, y-1) is to be chosen as the next pixel as it will be closer
to the arc.
• else (x+1, y) is to be chosen as next pixel.

23 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

Now to draw the circle for a given radius ‘r’ and center (xc, yc) We will start
from (0, r) and move in first quadrant till x=y (i.e. 45 degree). We should start
from listed initial condition:

d = 3 - (2 *
r) x = 0
y = r

Now for each pixel, we will do the following operations:

1. Set initial values of (xc, yc) and (x, y)


2. Set decision parameter d to d = 3 – (2 * r).

3. Repeat steps 4 to 8 until x < = y

4. call draw Circle(int xc, int yc, int x, int y) function.

5. Increment value of x.

6. If d < 0, set d = d + (4*x) + 6


7. Else, set d = d + 4 * (x – y) + 10 and decrement y by 1.

8. call draw Circle(int xc, int yc, int x, int y) function

draw Circle() function:

// function to draw all other 7 pixels


// present at symmetric position
draw Circle(int xc, int yc, int x,
int y)
{
put pixel(xc+x, yc+y,
RED); put pixel(xc-x,
yc+y, RED);
putpixel(xc+x, yc-y,

24 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

RED); putpixel(xc-x,
yc-y, RED);
putpixel(xc+y, yc+x,
RED); putpixel(xc-y,
yc+x, RED);
putpixel(xc+y, yc-x,
RED); putpixel(xc-y,
yc-x, RED);
}
2.4 Questions:-

Q 1: Explain the Bradenham’s circle drawing


algorithm? Q 2: Explain the circle equation.

Q 3: Difference between Bradenham’s and Mid point Circle drawing algorithm?

2.5 Conclusion:-

In This way we have studied that how to draw a circle on the screen.

25 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

#include<graphics.h>
#include<iostream>
using namespace std;
class pixel
{

public:
float x1,y1,r;
void brecircle(float x1,float y1,float r);

};
class point : public pixel
{
pixel p;

public:
void Drawcircle()
{
cout<<"Enter the starting co-ordinates of a center:";
cin>>p.x1>>p.y1;
cout<<"Enter the value of radius:";
cin>>p.r;
brecircle(p.x1,p.y1,p.r);
}
};
void pixel::brecircle(float x1,float y1,float r)
{
float x,y,p;
x=0;
y=r;
p=3-(2*r);
while(x<=y)
{
putpixel(x1+x,y1+y,WHITE);
putpixel(x1-x,y1+y,WHITE);
putpixel(x1+x,y1-y,WHITE);
putpixel(x1-x,y1-y,WHITE);
putpixel(x1+y,y1+x,WHITE);
putpixel(x1+y,y1-x,WHITE);
putpixel(x1-y,y1+x,WHITE);
putpixel(x1-y,y1-x,WHITE);
x=x+1;

26 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

if(p<0)
{
p=p+4*(x)+6;
}
else
{
p=p+4*(x-y)+10;
y=y-1;
}
delay(20);
}
}
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);

point pt;

pt.Drawcircle();

delay(50000);
closegraph();
return 0;
}

27 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

28 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

Assignment No. 3 (GROUP A)

Write C++ program to draw the following pattern.


Title Use DDA and Bresenham‘s Line drawing algorithm

Subject Computer Graphics Laboratory

Class S.E. (C.E)


Date

Signature

29 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

ExperimentNo:3

Title: Implement Following pattern using Line Drawing and Circle Drawing
Algorithm.

Aim: Write a program to draw using following algorithms- Circle generation and
Line Drawing.

Prerequisites:
Line and Line segment, Slope of Line, General Equation of Line
Objectives:
Understand DDA, Bresenham’s line drawing algorithm and Bresenham’s
Circle drawing. Further using these algorithms to draw real time pictures.

Theory:
DDA Line Drawing:
It is Digital Differential Analyzer (DDA).
It is also called as Vector generation line drawing algorithm.
Here we solve the differential equation for straight line. For drawing a line we
need to turn ON the pixels which are on the line segment. For this we consider
the slope of line.
For simplicity we divide the line segment in two types.
a) Line with Gentle Slope.
x
D Dy

Dx

30 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

Here Dx>Dy. We increment x by one unit and fine corresponding new value of y.
b) Line with Sharpe Slope

Dy

Dx

Here Dy>Dx, we increment y by one unit and calculate corresponding new value of
x.
Algorithm:
DDA line(Draw a figure)
Read the line endpoints (X1, Y1) and (X2, Y2) such that they are not equal.
1) Calculate
dx = X2 –
X1 dy = Y2 –Y1
2) if (dx >dy)

then step =
dx
else step = dy

end
if

3) Xin = dx /

step Yinc = dx / step


4) put pixel (X1, Y1,1) x = X1

y = Y1
5) i = 1
31 | Department of Computer Engineering, NBN SSOE,PUNE
Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

while (i <= step)


{
put pixel( x, ,y 1) x = x + dx
y = y + dy
i= i+1
}
Bresenham’s Line Drawing: (Draw a figure) Principle:
Either x or y is incremented by one unit depending on slope of line and increment
in other variable is determined by checking the distance between true line segment
and nearest pixel. This distance is called as decision variable.

Initially this decision variable s


set as e = 2 * dy - dx.

Algorithm:
1) Read the line endpoints (X1, Y1) and (X2, Y2) such that they are not equal.
2) Calculate
dx = X2 – X1 dy = Y2 – Y1
3) Initialize
x = X1
y = Y1
4) Calculate decision variable e = 2 * dy – dx

5) i = 1
put pixel ( x, y,1)
6) while ( e >= 0)
{
y=y+1
e = e – 2 * dx
}
32 | Department of Computer Engineering, NBN SSOE,PUNE
Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

x=x+1
e = e + 2 * dy
7) i = 1
8) if ( i <= dx) then go to step
9) Stop.

Questions:

1. Explain drawbacks of DDA line drawing algorithm?


2. Explain steps of Bradenham’s line drawing algorithm?
3. Discuss DDA advantages and disadvantages?
4. What is gentle slope and sharp slope?
5. How to calculate Bradenham’s Circle generation?
6. Explain mid-point circle drawing algorithm?
7. What are the 8 symmetric points for any point (x,y)?

33 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

# include <iostream>
# include <graphics.h>
# include <stdlib.h>
using namespace std;
class dcircle
{
private: int x0, y0;
public:
dcircle()
{
x0=0;
y0=0;
}
void setoff(int xx, int yy)
{
x0=xx;
y0=yy;
}
void drawc(int x1, int y1, int r)
{
float d;
int x,y;
x=0;
y=r;
d=3-2*r;
do
{
putpixel(x1+x0+x, y0+y-y1, 15);
putpixel(x1+x0+y, y0+x-y1,15);
putpixel(x1+x0+y, y0-x-y1,15);
putpixel(x1+x0+x,y0-y-y1,15);
putpixel(x1+x0-x,y0-y-y1,15);
putpixel(x1+x0-y, y0-x-y1,15);
putpixel(x1+x0-y, y0+x-y1,15);
putpixel(x1+x0-x, y0+y-y1,15);
if (d<=0)
{
d = d+4*x+6;
}
else
{
d=d+4*(x-y)+10;
y=y-1;

34 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

}
x=x+1;
}
while(x<y);
}
};

class pt
{
protected: int xco, yco,color;
public:
pt()
{
xco=0,yco=0,color=15;
}
void setco(int x, int y)
{
xco=x;
yco=y;
}
void setcolor(int c)
{
color=c;
}
void draw()
{
putpixel(xco,yco,color);
}
};
class dline:public pt
{
private: int x2, y2;
public:
dline():pt()
{
x2=0;
y2=0;
}
void setline(int x, int y, int xx, int yy)
{
pt::setco(x,y);
x2=xx;
y2=yy;

35 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

}
void drawl( int colour)
{
float x,y,dx,dy,length;
int i;
pt::setcolor(colour);
dx= abs(x2-xco);
dy=abs(y2-yco);
if(dx>=dy)
{
length= dx;
}
else
{
length= dy;
}
dx=(x2-xco)/length;
dy=(y2-yco)/length;
x=xco+0.5;
y=yco+0.5;
i=1;
while(i<=length)
{
pt::setco(x,y);
pt::draw();
x=x+dx;
y=y+dy;
i=i+1;
}
pt::setco(x,y);
pt::draw();
}
};
int main()
{
int gd=DETECT, gm;
initgraph(&gd, &gm, NULL);
int x,y,r, x1, x2, y1, y2, xmax, ymax, xmid, ymid, n, i;
dcircle c;
cout<<"\nenter coordinates of centre of circle : ";
cout<<"\n enter the value of x : ";
cin>>x;
cout<<"\nenter the value of y : ";

36 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

cin>>y;
cout<<"\nenter the value of radius : ";
cin>>r;
xmax= getmaxx();
ymax=getmaxy();
xmid=xmax/2;
ymid=ymax/2;
setcolor(1);
c.setoff(xmid,ymid);
line(xmid, 0, xmid, ymax);
line(0,ymid,xmax,ymid);
setcolor(15);
c.drawc(x,y,r);
pt p1;
p1.setco(100,100);
p1.setcolor(14);
dline l;
l.setline(x1+xmid, ymid-y1, x2+xmid, ymid-y2);
cout<<"Enter Total Number of lines : ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter co-ordinates of point x1 : ";
cin>>x1;
cout<<"enter coordinates of point y1 : ";
cin>>y1;
cout<<"Enter co-ordinates of point x2 : ";
cin>>x2;
cout<<"enter coordinates of point y2 : ";
cin>>y2;
l.setline(x1+xmid, ymid-y1, x2+xmid, ymid-y2);
l.drawl(15);
}
cout<<"\nEnter coordinates of centre of circle : ";
cout<<"\n Enter the value of x : ";
cin>>x;
cout<<"\nEnter the value of y : ";
cin>>y;
cout<<"\nEnter the value of radius : ";
cin>>r;
setcolor(5);
c.drawc(x,y,r);
getch();

37 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

delay(200);
closegraph();
return 0;
}

38 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

39 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

GROUP B
(2 ASSIGNMENT)

40 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

Assignment 4(GROUP B)
No.
Write C++ program to draw a 2-D object and
perform following basic transformation, Scaling b)
Translation c) Rotation. Apply the concept of
Title operator overloading.

Computer Graphics Laboratory


Subject

S.E. (C.E)
Class

Date

Signature

41 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

Assignment No: 4

Write C++ program to draw a 2-D object and perform following


basic transformation, Scaling b) Translation c) Rotation. Apply the
concept of operator overloading.

Title: 2D Transformations

Problem Statement: Write C++ program to draw 2-D object and perform
following basic transformations,
a) Scaling
b) Translation
c) Rotation
Apply concept of operator overloading

PREREQUISITES:
1. Knowledge of matrix fundamentals and basic transformations on polygon -
translation, rotation & scaling.

2. Basic mathematics, vectors & matrices

COURSE OBJECTIVE:
1. To acquaint the learner with the basic concepts of Computer Graphics
2. To get familiar with mathematics behind the graphical transformations

COURSE OUTCOME: Develop scientific and strategic approach to solve


complex problems in the domain of Computer Graphics

THEORY:

2D tansformations:
Transformations allow us to uniformly alter the entire picture. The geometric
transformations considered here - translation, scaling and rotation are expressed in
terms of matrix multiplication.
Scaling
scaling refers to changing the size of the object either by increasing or decreasing.
Scaling can be achieved by multiplying the original coordinates of the object
42 | Department of Computer Engineering, NBN SSOE,PUNE
Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

with the scaling factor to get the desired result. The scaling factor SX, SY scales
the object in X and Y direction respectively. The above equations can also be
represented in matrix form as below −

Rotation:

In rotation, we rotate the object at particular angle θ (theta) from its origin. From the
following figure, we can see that the point P(X, Y) is located at angle φ from the
horizontal X coordinate with distance r from the origin.

Using standard trigonometric the original coordinate of point P(X, Y) can be


represented as − X = r cosϕ (1)
Y = r sinϕ... (2)
Same way we can represent the point P’ (X’,
Y’) as − x′ = r cos(ϕ+θ) = r cosϕ cosθ − r sinϕ
sinθ ......................................................... (3)
y′ = r sin(ϕ+θ) = r cosϕ sinθ + r sinϕ cosθ. (4)
Substituting equation (1) & (2) in (3) & (4) respectively,
we will get x′ = x cosθ –y sinθ
y′ = x sinθ + y cosθ

Representing the above equation in matrix form,

43 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

The rotation angle can be positive and negative.


For positive rotation angle, we can use the above rotation matrix. However, for
negative angle rotation, the matrix will change

Translation
A translation moves an object to a different position on the screen. You can
translate a point in 2D by adding translation coordinate (tx, ty) to the original
coordinate (X, Y) to get the new coordinate (X’, Y’).
Translation Distance: It is nothing but by how much units we should shift the
object from one location to another along x, y-axis.

44 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

From the above figure, you can write


that − X’ = X + tx
Y’ = Y + ty
The pair (tx, ty) is called the translation vector or shift vector. The translation
represented in matrix form is

Operator Overloading

Operator overloading is an important concept in C++. It is a type of polymorphism


in which an operator is overloaded to give user defined meaning to it. Overloaded
operator is used to perform operation on user-defined data type. For example '+'
operator can be overloaded to perform addition on various data types, like for
Integer, String(concatenation) etc.

45 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

46 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

Algorithm:

1. Read input object in the form of matrix

2. Multiply input matrix with transformation matrix

3. Repeat same for all transformations

4. Display transformed object

5. stop

OOP’s concepts used


1. Class

2. Operator Overloading

47 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

#include<iostream>
#include<graphics.h>
#include<math.h>
using namespace std;
class transform
{
public:
int m,a[20][20],c[20][20];
int i,j,k;
public:

void object();
void accept();
void operator *(float b[20][20])
{
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
c[i][j]=0;
for(int k=0;k<m;k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
}
};
void transform::object()
{
int gd,gm;
gd=DETECT;
initgraph(&gd,&gm,NULL);
line(300,0,300,600);
line(0,300,600,300);
for( i=0;i<m-1;i++)
{
line(300+a[i][0],300-a[i][1],300+a[i+1][0],300-a[i+1][1]);
}
line(300+a[0][0],300-a[0][1],300+a[i][0],300-a[i][1]);
for( i=0;i<m-1;i++)
{

48 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

line(300+c[i][0],300-c[i][1],300+c[i+1][0],300-c[i+1][1]);
}
line(300+c[0][0],300-c[0][1],300+c[i][0],300-c[i][1]);
int temp;
cout << "Press 1 to continue";
cin >> temp;
closegraph();
}
void transform::accept()
{
cout<<"\n";
cout<<"Enter the Number Of Edges:";
cin>>m;
cout<<"\nEnter The Coordinates :";
for(int i=0;i<m;i++)
{
for(int j=0;j<3;j++)
{
if(j>=2)
a[i][j]=1;
else
cin>>a[i][j];
}
}
}
int main()
{
int ch,tx,ty,sx,sy;
float deg,theta,b[20][20];
transform t;
t.accept();

cout<<"\nEnter your choice";


cout<<"\n1.Translation"
"\n2.Scaling"
"\n3.Rotation";
cin>>ch;
switch(ch)
{
case 1: cout<<"\nTRANSLATION OPERATION\n";
cout<<"Enter value for tx and ty:";
cin>>tx>>ty;
b[0][0]=b[2][2]=b[1][1]=1;
b[0][1]=b[0][2]=b[1][0]=b[1][2]=0;

49 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

b[2][0]=tx;
b[2][1]=ty;
t * b;

t.object();
break;
case 2: cout<<"\nSCALING OPERATION\n";
cout<<"Enter value for sx,sy:";
cin>>sx>>sy;
b[0][0]=sx;
b[1][1]=sy;
b[0][1]=b[0][2]=b[1][0]=b[1][2]=0;
b[2][0]=b[2][1]=0;
b[2][2] = 1;
t * b;
t.object();
break;
case 3: cout<<"\nROTATION OPERATION\n";
cout<<"Enter value for angle:";
cin>>deg;
theta=deg*(3.14/100);
b[0][0]=b[1][1]=cos(theta);
b[0][1]=sin(theta);
b[1][0]=sin(-theta);
b[0][2]=b[1][2]=b[2][0]=b[2][1]=0;
b[2][2]=1;
t * b;
t.object();
break;
default:
cout<<"\nInvalid choice";

getch();

return 0;
}

50 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

51 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

52 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

Assignment No. 5 (GROUP B)

Title Write C++ program generate fractal


patterns using Koch Curves.

Subject Computer Graphics Laboratory

Class S.E. (C.E)


Date

Signature

53 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

Experiment No:5
TITLE: Fractal Graphics

PROBLEM STATEMENT:
Write C++ program to generate fractal patterns by using Koch curves.

PREREQUISITES:
Basic knowledge of mathematic primitives

COURSE OBJECTIVE:
1. To acquaint the learnerwith the basic concepts of fractal graphics
2. To get familiar with mathematics behind the graphical transformations

COURSE OUTCOME:
Develop the competency to understand the concepts related to fractals

THEORY:
Fractals: Fractals are very complex pictures generated by a computer from a single
formula. They are created using iterations. This means one formula is repeated with
slightly different values over and over again, taking into account the results from the
previous iteration.
Fractals are used in many areas such as −
Astronomy − For analyzing galaxies, rings of Saturn, etc.
Biology/Chemistry − For depicting bacteria cultures, Chemical reactions, human
anatomy, molecules, plants,
Others − For depicting clouds, coastline and borderlines, data compression, diffusion,
economy, fractal art, fractal music, landscapes, special effect, etc.
Koch curve :
The Koch curve is also known as snowflake curve. The Koch curve originally
described by Helge von Koch is constructed with only one of the three sides of the
original triangle. In other words, three Koch curves make a Koch snowflake.

54 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

Figure: The first three iterations of the Koch snowflake

ALGORITHM:

1. The Koch snowflake can be constructed by starting with an equilateral triangle,


then recursively altering each line segment as follows:
o Divide the line segment into three segments of equal length.
o Draw an equilateral triangle that has the middle segment from step 1 as
its base and points outward.

• Remove the line segment that is the base of the triangle from step 2.

• After one iteration of this process, the resulting shape is the outline of a hexagram.

55 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

1. The Koch snowflake is the limit approached as the above steps are followed
over and over again.
2. The Koch curve originally described by Koch is constructed with only one of
the three sides of the original triangle.
3. In other words, three Koch curves make a Koch snowflake.

CONCLUSION: Fractals are studied by implementing Koch Snowflake curve.

QUESTIONS:-
1) Define Koch curve
2) How to construct Koch curve

56 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

#include <iostream>
#include <math.h>
#include <graphics.h>
using namespace std;
class kochCurve
{
public:
void koch(int it,int x1,int y1,int x5,int y5)
{
int x2,y2,x3,y3,x4,y4;
int dx,dy;
if (it==0)
{
line(x1,y1,x5,y5);
}
else
{
delay(10);
dx=(x5-x1)/3;
dy=(y5-y1)/3;
x2=x1+dx;
y2=y1+dy;
x3=(int)(0.5*(x1+x5)+sqrt(3)*(y1-y5)/6);
y3=(int)(0.5*(y1+y5)+sqrt(3)*(x5-x1)/6);
x4=2*dx+x1;
y4=2*dy+y1;
koch(it-1,x1,y1,x2,y2);
koch(it-1,x2,y2,x3,y3);
koch(it-1,x3,y3,x4,y4);
koch(it-1,x4,y4,x5,y5);
}
}
};
int main()
{
kochCurve k;
int it;
cout<<"Enter Number Of Iterations : "<<endl;
cin>>it;
int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);
k.koch(it,150,20,20,280);
k.koch(it,280,280,150,20);

57 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

k.koch(it,20,280,280,280);
getch();
closegraph();
return 0;
}

58 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

59 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

GROUP C
(2 ASSIGNMENT)

60 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

Assignment No. 6 (GROUP C)

Title Write OpenGL program to draw Sun Rise


and Sun Set

Subject Computer Graphics Laboratory

Class S.E. (C.E)


Date

Signature

61 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

Experiment No:6

Aim: Write OpenGL program to draw Sun Rise and Sun Set

Theory: OpenGL Basics:


Open Graphics Library (OpenGL) is a cross-language (language independent), cross-
platform (platform independent) API for rendering 2D and 3D Vector Graphics (use of
polygons to represent image). OpenGL is a low-level, widely supported modeling and
rendering software package, available across all platforms. It can be used in a range of
graphics applications, such as games, CAD design, or modeling. OpenGL API is
designed mostly in hardware.
➢ Design: This API is defined as a set of functions which may be called by the client
program. Although functions are similar to those of C language but it is language
independent.

➢ Development: It is an evolving API and Khronos Group regularly releases its new
version having some extended feature compare to previous one. GPU vendors may also
provide some additional functionality in the form of extension.

➢ Associated Libraries: The earliest version is released with a companion library


called OpenGL utility library. But since OpenGL is quite a complex process. So in order to
make it easier other library such as OpenGL Utility Toolkit is added which is later
superseded by freeglut. Later included library were GLEE, GLEW and glbinding.

➢ Implementation: Mesa 3D is an open source implementation of OpenGL. It can


do pure software rendering and it may also use hardware acceleration on BSD, Linux,
and other platforms by taking advantage of Direct Rendering Infrastructure.

Overview of an OpenGL program

➢ Main
• Open window and configure frame buffer (using GLUT for example)
• Initialize GL states and display (Double buffer, color mode, etc.)
➢ Loop
• Check for events
if window event (resize, unhide, maximize etc.) modify the viewport and Redrawvzelse if
input event (keyboard and mouse etc.)
handle the event (such as move the camera or change the state)
and usually draw the scene

62 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

➢ Redraw
• Clear the screen (and buffers e.g., z-buffer)
• Change states (ifdesired)
• Render
• Swap buffers (if double buffer)

OpenGL order of operations


➢ Construct shapes (geometric descriptions of objects – vertices, edges, polygons etc.)
➢ Use OpenGL to
⚫ Arrange shape in 3D (using transformations) Select your
vantage point (and perhaps lights)
⚫ Calculate color and texture properties of each object
⚫ Convert shapes into pixels onscreen

OpenGL Syntax
➢ All functions have the form: gl*
• glVertex3f() – 3 means that this function take three arguments, and f means that
the type of those arguments is float.
• glVertex2i() – 2 means that this function take two arguments, and i means that
the type of those arguments is integer
➢ All variable types have the form: GL*
• In OpenGL program it is better to use OpenGL variable types (portability)
▪ Glfloat instead of float
▪ Glint instead of int
OpenGL primitives
Drawing two lines
glBegin(GL_LINES);
glVertex3f(_,_,_); // start point of line 1
glVertex3f(_,_,_); // end point of line 1
glVertex3f(_,_,_); // start point of line 2
glVertex3f(_,_,_); // end point of line 2 glEnd();
We can replace GL_LINES with GL_POINTS, GL_LINELOOP, GL_POLYGON etc.

OpenGL states
➢ On/off (e.g., depth buffer test)
• glEnable( GLenum )
• glDisable( GLenum )
• Examples:
63 | Department of Computer Engineering, NBN SSOE,PUNE
Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

▪ glEnable(GL_DEPTH_TEST);
▪ glDisable(GL_LIGHTING);
➢ Mode States
• Once the mode is set the effect stays until reset
• Examples:
▪ glShadeModel(GL_FLAT) or glShadeModel(GL_SMOOTH)
▪ glLightModel(…) etc.

Drawing in 3D
➢ Depth buffer (or z-buffer) allows scene to remove hidden surfaces.
Use
glEnable(GL_DEPTH_TEST) to enable it.

• glPolygonMode( Face, Mode )


➢ Face: GL_FRONT, GL_BACK, GL_FRONT_AND_BACKMode: GL_LINE,
GL_POINT, GL_FILL
• glCullFace( Mode )
➢ Mode: GL_FRONT, GL_BACK,GL_FRONT_AND_BACK
• glFrontFace( Vertex_Ordering )
• Vertex Ordering: GL_CW or GL_CCW

Viewing transformation
➢ glMatrixMode ( Mode )
• Mode: GL_MODELVIEW, GL_PROJECTION, orGL_TEXTURE
➢ glLoadIdentity()
➢ glTranslate3f(x,y,z)
➢ glRotate3f(angle,x,y,z)
➢ glScale3f(x,y,z)

OpenGL provides a consistent interface to the underlying graphics hardware. This


abstraction allows a single program to run a different graphics hardware easily. A
program written with OpenGL can even be run in software (slowly) on machines with no
graphics acceleration. OpenGL function names always begin with gl, such as glClear(),
and they may end with characters that indicate the types of the parameters, for example
glColor3f(GLfloat red, GLfloat green, GLfloat blue) takes three floating- point color
parameters and glColor4dv(const GLdouble *v) takes a pointer
to an array that contains 4 double-precision floating-point values. OpenGL constants
begin with GL, such as GL DEPTH. OpenGL also uses special names for types that are
passed to its functions, such as GLfloat or GLint, the corresponding C types are
compatible, that is float and int respectively.

GLU is the OpenGL utility library. It contains useful functions at a higher level than those
64 | Department of Computer Engineering, NBN SSOE,PUNE
Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

provided by OpenGL, for example, to draw complex shapes or set up cameras. All GLU
functions are written on top of OpenGL. Like OpenGL, GLU function names begin with
glu, and constants begin with GLU.

GLUT, the OpenGL Utility Toolkit, provides a system for setting up callbacks for
interacting with the user and functions for dealing with the windowing system. This
abstraction allows a program to run on different operating systems with only a recompile.
Glut follows the convention of prepending function names with glut and constants with
GLUT.
Writing an OpenGL Program with GLUT

An OpenGL program using the three libraries listed above must include the appropriate
headers. This requires the following threelines: #include <GL/gl.h>
#include <GL/glu.h> #include
<GL/glut.h>

Before OpenGL rendering calls can be made, some initialization has to be done. With
GLUT, this consists of initializing the GLUT library, initializing the display mode, creating
the window, and setting up callback functions. The following lines initialize a full color,
double buffered display: glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

Double buffering means that there are two buffers, a front buffer and a back buffer. The
front buffer is displayed to the user, while the back buffer is used for rendering operations.
This prevents flickering that would occur if we rendered directly to the front buffer.

Next, a window is created with GLUT that will contain the viewport which displays the
OpenGL front buffer with the following three lines: glutInitWindowPosition(px, py);
glutInitWindowSize(sx, sy); glutCreateWindow(name);

To register callback functions, we simply pass the name of the function that handles the event
to the appropriate GLUT function. glutReshapeFunc(reshape);
glutDisplayFunc(display);
Here, the functions should have the following prototypes:
void reshape(int width, int height); void display();

In this example, when the user resizes the window, reshape is called by GLUT, and when
65 | Department of Computer Engineering, NBN SSOE,PUNE
Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

the display needs to be refreshed, the display function is called. For animation, an idle
event handler that takes no arguments can be created to call the display function to
constantly redraw the scene with glutIdleFunc. Once all the callbacks have been set up, a
call to glutMainLoop allows the program torun.

In the display function, typically the image buffer is cleared, primitives are rendered to it,
and the results are presented to the user. The following line clears the image buffer, setting
each pixel color to the clear color, which can be configured to be anycolor:

glClear(GL_COLOR_BUFFER_BIT);

The next line sets the current rendering color to blue. OpenGL behaves like a state
machine, so certain state such as the rendering color is saved by OpenGL and used
automatically later as it is needed.

glColor3f(0.0f, 0.0f, 1.0f);

To render a primitive, such as a point, line, or polygon, OpenGL requires that a call to
glBegin is made to specify the type of primitive being rendered.

glBegin(GL_LINES);

Only a subset of OpenGL commands is available after a call to glBegin. The main
command that is used is glVertex, which specifies a vertex position. In GL LINES mode,
each pair of vertices define endpoints of a line segment. In this case, a line would be drawn
from the point at ( x0, y0) to (x1, y1).

glVertex2f(x0, y0);
glVertex2f(x1, y1);

A call to glEnd completes rendering of the current primitive. glEnd(); Finally, the back buffer needs to
be swapped to the front buffer that the user will see, which GLUT can handle for us:

glutSwapBuffers();

66 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

67 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-
23]

Assignment No. 7 (GROUP C)

Write C++ program to to draw man walking in


Title the rain with an umbrella. Apply the concept of
polymorphism.

Subject Computer Graphics Laboratory

Class S.E. (C.E)


Date

Signature

68 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-
23]

Group C
Assignment No: 7

PROBLEM STATEMENT:
Write C++ program to to draw man walking in the rain with an umbrella.
Apply the concept of polymorphism.

PREREQUISITES:
Basic knowledge of graphics primitives.

COURSE OBJECTIVE:
1. To implement the features of graphics.
2. To interface the applications of graphics to the real world.
3. To give some benefits to the disability.
4. To make the life easier.
5. To become familiarization with Graphics and its logical coding.

COURSE OUTCOME:
To understand the concepts related to polymorphism and graphics primitives.

THEORY:
Computer Graphics has revolutionized almost every computer-based
application in science and technology. Information technology is a trend
today.The importance of computer graphics lies in its applications. In
engineering applications (e.g. automotive and aerospace) the ability to quickly
visualize newly designed shapes is indispensible. Computer graphics has also
expanded the boundaries of art and entertainment. Movies such as
“JURASSIC PARK” make extensive use of computer graphics to create
images that test the bounds of imagination. The development of computer
graphics has made possible virtual reality, a synthetic reality that exists only
inside a computer. Virtual reality is fast becoming an indispensable tool in
education. Flight simulators are used to train pilot for extreme conditions.
Surgical simulators are used to train novice surgeons without endangering
patients.

69 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-
23]

Figure: Sample graphics program


This program initializes graphics mode and then closes it after a key is
pressed. To begin with we have declared two variables of int type gd and
gm for graphics driver and
graphics mode respectively, you can choose any other variable name as you
wish. DETECT is a macro defined in "graphics.h" header file, then we have
passed three arguments to initgraph function first is the address of gd, second
is the address of gm and third is the path where your BGI files are present (we
have adjusted this accordingly where our turbo compiler is installed).
Initgraph function automatically decides an appropriate graphics driver and
mode such that maximum screen resolution is set, getch () helps us to wait until
a key is pressed, closegraph () function closes the graphics mode and finally
return statement returns a value 0 to main indicating successful execution of
your program.

Some of the function included in<graphics.h> used in our


program:

Line function: 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);

Circle function: 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);

70 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-
23]

Setcolor function: 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);

Outtextxy function:Outtextxy function display text or string at a


specified point(x, y) on the screen.
Declaration: - void Outtextxy (int x, int y, char *string);
x, y are coordinates of the point and third argument contains the
address of string to be displayed.

Getmaxx function: getmaxx function returns the maximum X


coordinate for current graphics mode and driver.
Declaration:-int getmaxx ();
Getmaxy function: getmaxy function returns the maximum Y
coordinate for current graphics mode and driver.
Declaration:- intgetmaxy();

Setlinestyle function: Setlinestyle function sets line style,


thickness, unsigned upatternetc.
Declaration: - void setlinestyle (intlinestyle, unsigned upattern, int
thickness);

71 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

Putpixel function: putpixel function plots a pixel at location (x, y) of specified


color.
Declaration: - void putpixel (int x, int y, int color);
For example if we want to draw a GREEN color pixel at (35, 45) then we will
write putpixel (35, 35, GREEN); in our c program, putpixel function can be
used to draw circles, lines and ellipses using various algorithms.

Setfillstyle function: setfillstyle function sets the current fill pattern and
fill color.

Declaration: - void setfillstyle (int pattern, int color);


Figure: Walking man in the rain with an umbrella

CONCLUSION: Using the concept of transformation, an illusion of moving


object is achieved.

72 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

#include<iostream>
#include<graphics.h>
#include<stdlib.h>
using namespace std;
class walkingman
{
int rhx,rhy;
public:
void draw(int,int);
void draw(int);
};
void walkingman::draw(int i)
{
line(20,380,580,380);
if(i%2)
{
line(25+i,380,35+i,340);
line(45+i,380,35+i,340);
line(35+i,310,25+i,330);
delay(20);
}
else
{
line(35+i,340,35+i,310);
line(35+i,310,40+i,330);
delay(20);
}
line(35+i,340,35+i,310);
circle(35+i,300,10);
line(35+i,310,50+i,330);
line(50+i,330,50+i,280);
line(15+i,280,85+i,280);
arc(50+i,280,0,180,35);
arc(55+i,330,180,360,5);
}
void walkingman::draw(int x,int y)
{

73 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

int j;
rhx=x;
rhy=y;
for
(j=0;j<100;j++)
{
outtextxy(rand()%rhx,rand()%(rhy-50),"|");
setcolor(WHITE);
}
}
int main()
{
int gd=DETECT,gm;
int rhx,rhy,j,i;
walkingman obj;
initgraph(&gd,&gm,"");
for(i=0;i<500;i++)
{
obj.draw(i);
rhx=getmaxx();
rhy=getmaxy();
obj.draw(rhx,rhy);
delay(150);
cleardevice();
}
getch();
}

74 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

75 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

MINI PROJECT

76 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

MINI PROJECT
Title: Mini project for animation of moving car

Theory:
Function used:

(1) initgraph : It initializes the graphics system by loading the passed graphics
driver then Charging the system into graphics mode.

(2) get maxx : It returns the maximum & Co-ordinate in current graphics mode and
diver

(3) getmaxy: It returns the maximum X Cordinate in Current graphics mode and
driver

(4) Set color: It changes the Current drawing colour. Default Colour is White. Each
color is assigned a number like BLACK is and RED is 4. Here we are using
colour constants defined Inside graphics.h header file.

(5) Setfillstyle: It states the Current Fill pattern and fill color.

(6) Circle: It draws a Circle with radius and centre of (x,y).

(7) Line: It draws a straight line between two points on Screen.

(8) Arc: It draws a Circular arc from start angle till end angle.

(9) Floodfill: It is used to fill a closed area with current fill pattern and fill color. It
takes any point inside. Closed area and Color of the boundary as input.

(10) Cleardevice: It clear the screen and sets Current position to (0,0).

(11) Delay: It is used to suspend execution of a program for M milliseconds.

(12) Close graph: It includes the graphics drivers and sets the Screen back to meet
mode.

77 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

Approach:
1. In this program we first draw a red color car on left side of the Screen (x,y) and
then erases it wing cleardevice function.
2. we again draw this car at (x+5,y).
3. This will look like a moving car from left to right direction.
4. we will repeat above step, until car reaches then right side of screen.
Conclusion.-
In this Ways created a mini project for animation of moving car using graphics and
successfully implemented it.

78 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

#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);
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);

79 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

/* Add delay of 0.1 milli seconds */


delay(100);
}

getch();
closegraph();
return 0;

80 | Department of Computer Engineering, NBN SSOE,PUNE


Computer Graphics Laboratory S.E.C.E(Sem-1) [2022-23]

81 | Department of Computer Engineering, NBN SSOE,PUNE

You might also like