0% found this document useful (0 votes)
15 views

CG Unit 1 Notes

Uploaded by

PCB WALLAH
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

CG Unit 1 Notes

Uploaded by

PCB WALLAH
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

1|Page

Unit – I
Graphics Primitives & Scan Conversion
Algorithms

1. Introduction

1.1 Computer Graphics

Computer graphics is a field of computer science that focuses on creating, manipulating, and
displaying visual content using computers. This includes everything from simple 2D images to
complex 3D models and animations.

It displays the information in the form of graphics objects such as pictures, charts, graphs and
diagrams instead of simple text. Hence, we can say that computer graphics makes it possible
to express data in pictorial form.

1.2 Applications of Computer Graphics

Video Game Development: Used to create visually rich and immersive game environments,
character models, and dynamic effects in both 2D and 3D video games.

Movie and Animation Production: Essential for generating special effects, animated
characters, and realistic environments in films, TV shows, and animated features, including
CGI (Computer–Generated Imagery).

Computer–Aided Design (CAD): Widely used in engineering, architecture, and


manufacturing to design and visualize products, buildings, and mechanical systems before
physical production.

Medical Imaging: Important for visualizing complex medical data from MRI, CT scans, and
other imaging technologies, aiding in diagnosis, surgical planning, and medical research.

Presentation Graphics: For the preparation of reports or summarizing the financial, statistical,
mathematical, scientific, and economic data for research reports, and managerial reports,
moreover creation of bar graphs, pie charts, and time charts, can be done using the tools present
in computer graphics.
2|Page

Machine Drawing: Computer graphics are very frequently used for designing, modifying, and
creating various parts of a machine and the whole machine itself.

Graphical User Interface: The use of pictures, images, icons, pop–up menus, and graphical
objects helps in creating a user–friendly environment where working is easy and pleasant, using
computer graphics we can create such an atmosphere where everything can be automated and
anyone can get the desired action performed in an easy fashion.

2. Graphics Primitives

2.1 Pixel

A pixel is the smallest unit of information in an image or display on a computer, smartphone,


or television.

Pixels are tiny squares or dots that represent a single point of color. They are usually arranged
in a regular 2–dimensional grid. Each pixel is made up of a red, green, and blue (RGB) subpixel
that emits color at different intensities.

2.2 Resolution

Resolution refers to the amount of detail or clarity of the display, measured in pixels. It indicates
the number of pixels along the width and height of a screen or image. Higher resolutions have
more pixels and, therefore, can display more detail and sharper images.

Some common resolutions are:

640x480 – VGA (Video Graphics Array)

800x600 – SVGA (Super VGA)

1280x720 (720p) – HD (High Definition)

1920x1080 (1080p) – Full HD (FHD)

2.3 Aspect Ratio

Aspect ratio is the ratio of the width to the height of an image, screen, or display. It is usually
expressed as two numbers separated by a colon (e.g., 16:9). This ratio defines how the width
compares to the height, and it helps determine the shape of the image or screen.

Some common aspect ratios are:


3|Page

4:3 – Standard Definition (SD)

16:9 – High Definition (HD) / Full HD (FHD) / Ultra HD (UHD)

2.4 Frame Buffer

A frame buffer is a portion of memory used to store image data that will be displayed on a
screen. It acts as a temporary storage area where the graphical data is kept before it is sent to
the display. The frame buffer holds the color and pixel information for each point on the screen,
allowing for smooth and consistent image rendering.

2.5 Refresh Buffer

A refresh buffer is a type of memory used in display systems to store data that is continuously
refreshed or updated for display purposes. It ensures that the image on the screen remains stable
and clear by repeatedly sending the stored image data to the display.

2.6 Frame Rate

Frame rate is the number of frames (individual images) displayed per second in a video or
animation. It’s measured in frames per second (FPS).

It determines how smooth motion appears in videos, games, or animations. A higher frame rate
results in smoother and more fluid motion.

For example, a video playing at 30 FPS means 30 individual frames are shown each second.

2.7 Refresh Rate

Refresh rate is the number of times per second a display updates the image it shows. It’s
measured in hertz (Hz).

It indicates how often the screen refreshes the displayed image. A higher refresh rate can reduce
flicker and improve the overall visual experience.

A monitor with a 60 Hz refresh rate updates the image 60 times per second.

2.8 Color Depth

Color depth, also known as bit depth, refers to the number of bits used to represent the color
of a single pixel in an image or display. It determines the range of colors that can be displayed
or captured and affects the overall color accuracy and richness of images.
4|Page

Common Values:

 1–bit: 2 colors (black and white).

 8–bit: 256 colors (often used in indexed color images).

 16–bit: 65,536 colors (high color).

 24–bit: 16.7 million colors (true color, 8 bits per color channel).

 30–bit or 36–bit: More than 1 billion colors (deep color, with additional bits for greater
precision).

2.9 Persistence

Persistence refers to the duration for which a pixel or image remains visible before it fades or
is replaced by new information.

3. Introduction to OpenGL

OpenGL (Open Graphics Library) is a cross–platform, open–source API (Application


Programming Interface) used for rendering 2D and 3D graphics. It provides a standard interface
for developers to create and manage graphical content, such as visual effects, animations, and
interactive graphics in applications.

3.1 Key Features of OpenGL

Cross–Platform Compatibility: OpenGL is designed to work across various operating


systems, including Windows, macOS, and Linux. This makes it a popular choice for developers
who need to create graphics applications that run on multiple platforms.

3D Graphics Rendering: OpenGL is widely used for 3D graphics rendering. It provides


functions for creating and manipulating 3D objects, applying textures, and implementing
lighting and shading effects.

2D Graphics Support: While it’s most known for 3D graphics, OpenGL also supports 2D
rendering. This is useful for creating user interfaces, 2D games, and other applications that
require 2D graphics.

Shaders and Programmable Pipeline: OpenGL includes support for programmable shaders,
which allow developers to write custom programs (shaders) for the GPU to control rendering
effects and perform complex visual computations.
5|Page

High Performance: OpenGL is designed to provide high performance for graphics rendering
by using the capabilities of modern GPUs (Graphics Processing Units). It allows for efficient
use of hardware resources to achieve smooth and detailed graphics.

Extensibility: OpenGL supports extensions that provide additional features and capabilities
beyond the core specification. This allows for customization and innovation in graphics
rendering.

Community and Ecosystem: Being an open standard, OpenGL has a large community of
developers and extensive documentation. There are many libraries, tools, and resources
available to assist in development.

3.2 Typical Uses of OpenGL

Video Games: For rendering graphics, creating game environments, and implementing visual
effects.

Simulations: For creating realistic simulations in fields like aviation, engineering, and science.

Graphics Applications: For designing and rendering complex 3D models and visualizations
in applications like CAD (Computer–Aided Design) and 3D modeling software.

3.3 Versions of OpenGL

OpenGL ES: A variant of OpenGL designed for embedded systems, such as mobile devices
and tablets. It’s used in applications where resources are limited and is often found in mobile
games and apps.

OpenGL 4.x: Represents the latest versions of the OpenGL specification, offering advanced
features and improved performance.

3.4 Architecture of OpenGL (OpenGL Pipeline)

The OpenGL architecture is structured as a state–based pipeline. Below is a simplified


diagram of this pipeline. Commands enter the pipeline from the left.
6|Page

Display List: Commands may either be accumulated in display lists, or processed immediately
through the pipeline. Display lists allow for greater optimization and command reuse, but not
all commands can be put in display lists.

Evaluator: The first stage in the pipeline is the evaluator. This stage effectively takes any
polynomial evaluator commands and evaluates them into their corresponding vertex and
attribute commands.

Per–Vertex Operations & Primitive Assembly: The second stage is the per–vertex
operations, including transformations, lighting, primitive assembly, clipping, projection, and
viewport mapping.

Rasterization: The third stage is rasterization. This stage produces fragments, which are series
of framebuffer addresses and values, from the viewport–mapped primitives as well as bitmaps
and pixel rectangles.

Per–fragment Operations: The fourth stage is the per–fragment operations. Before fragments
go to the framebuffer, they may be subjected to a series of conditional tests and modifications,
such as blending or z–buffering.

Parts of the framebuffer may be fed back into the pipeline as pixel rectangles. Texture memory
may be used in the rasterization process when texture mapping is enabled.

3.5 Primitives of OpenGL


7|Page

OpenGL supports various basic primitive types such as point, line and polygons. Sequence of
vertices is used to specify these primitives. There are ten different primitive types. OpenGL
interprets each primitive using following guidelines.

1. GL_POINTS Used to render a point

2. GL_LINES Used to draw unconnected line segments

3. GL_POLYGON Used to draw a solid convex polygon

4. GL_TRIANGLES Used to draw individual triangles

5. GL_QUADS Used to draw separate convex quadrilaterals


8|Page

Used to draw a sequence of connected line


6. GL_LINE_STRIP
segments

7. GL_LINE_LOOP Used to close the line strip

Used to draw a sequence of triangles that


8. GL_TRIANGLE_STRIP
share edges

Used to draw a sequence of triangles that


9. GL_TRIANGLE_FAN
share edges as well as vertices

Used to draw a sequence of quadrilaterals


10. GL_QUAD_STRIP
which share their edges

3.6 Attributes of OpenGL


9|Page

In OpenGL, attributes refer to various properties associated with vertices and other elements
that affect how they are rendered. Attributes are crucial for defining the appearance and
behavior of graphical objects. Here’s an overview of some OpenGL attributes:

1. Vertex Position: It specifies the coordinates of a vertex in 2D or 3D space. The function


used is ‘glVertex2f(x, y)’ or ‘glVertex3f(x, y, z)’.

2. Color: It defines the color of a vertex. The function used is ‘glColor3f(r, g, b)’ or
‘glColor4f(r, g, b, a)’.

3. Texture Coordinates: It specifies coordinates used to map textures onto geometry. The
function used is glTexCoord2f(x, y).

4. Line Width: It specifies the thickness of the lines. The function used is
‘glLineWidth(width)’.

5. Line Style: It defines the pattern of lines (e.g., dashed or solid). The function used is
‘glLineStipple(factor, pattern)’.

3.7 Simple Modelling and Rendering of 2D and 3D Geometric Objects

Modeling and rendering are fundamental concepts in computer graphics. They involve
creating and displaying geometric objects on a screen. Here’s a brief overview of simple
approaches for 2D and 3D objects:

3.7.1 2D Geometric Objects

Modeling: It involves defining shapes in a two–dimensional plane. Common 2D primitives


include points, lines, triangles, rectangles, and circles.

To model a rectangle, we specify four vertices corresponding to the corners of the rectangle.

Rendering: Rendering 2D objects involves mapping the defined shapes onto the screen.

Steps:

1. Set up the rendering environment.

2. Use functions to specify the vertices of 2D shapes. For example, use


‘glBegin(GL_QUADS)’ and ‘glVertex2f(x, y)’ to define a rectangle.

3. Assign colors to shapes using ‘glColor3f(r, g, b)’.


10 | P a g e

4. Call rendering functions to draw shapes on the screen.

Example Code:

glBegin(GL_QUADS); // Start defining a rectangle

glVertex2f(–0.5f, –0.5f); // Bottom–left

glVertex2f( 0.5f, –0.5f); // Bottom–right

glVertex2f( 0.5f, 0.5f); // Top–right

glVertex2f(–0.5f, 0.5f); // Top–left glEnd(); // End definition

3.7.2 3D Geometric Objects

Modeling: It involves creating shapes in three–dimensional space. Basic 3D primitives include


points, lines, triangles, cubes, and spheres.

To model a cube, you specify the vertices of its six faces.

Rendering: Rendering 3D objects involves projecting them onto the 2D screen while
considering depth.

Steps:

1. Set up the rendering environment.

2. Define the camera view and projection using matrices. For example, use ‘ glOrtho’ for
orthographic projection or ‘glFrustum’ for perspective projection.

3. Specify vertices for 3D primitives. For example, define a cube using vertices and faces.

4. Configure lighting to affect how the object appears.

5. Draw the 3D shapes with appropriate transformations and lighting.

Example Code:

// Define vertices for a cube

glBegin(GL_QUADS); // Front face

glVertex3f(–0.5f, –0.5f, 0.5f); // Bottom–left

glVertex3f( 0.5f, –0.5f, 0.5f); // Bottom–right

glVertex3f( 0.5f, 0.5f, 0.5f); // Top–right


11 | P a g e

glVertex3f(–0.5f, 0.5f, 0.5f); // Top–left // Repeat for other faces...

glEnd();

3.8 OpenGL Library

OpenGL provides a simple collection of utilities, called the GL Utility Library or GLU in short.
Functions from the OpenGL library begin with ‘gl’, functions from GLU begin with glu, and
functions from GLUT begin with ‘glut’.

The gl.h header file needs to be included in each program for all OpenGL applications. Almost
all OpenGL applications use GLU i.e. OpenGL Utility Library, which needs glu.h header file
to be included in a program.

Due to this almost every OpenGL source file starts with

#include <GL/gl.h>

#include <GL/glu.h>

If we are using GLUT to access window manager tasks, then we should use

#include <GL/glut.h>

Where glut.h itself includes gl.h, and glu.h, so whenever we use glut.h, there’s no need
to include gl.h and glu.h separately.

3.9 GLUT

GLUT (OpenGL Utility Toolkit) is a library that provides a set of functions for creating and
managing windows with OpenGL contexts, handling user input, and performing other tasks
that are common in OpenGL applications.

GLUT simplifies the process of developing OpenGL programs by providing an easy–to–use


interface for managing graphics and windowing operations.

3.9.1 Key Features of GLUT

Window Management: Functions to create and manage windows where OpenGL renders
graphics. For example, glutCreateWindow("Window Title")

Event Handling: Functions to handle keyboard, mouse, and other user inputs. For example,
glutKeyboardFunc(keyboardCallback), where keyboardCallback is a function that handles
key presses.
12 | P a g e

Rendering: Functions to set up the rendering loop where OpenGL drawing commands are
executed. For example, glutDisplayFunc(displayCallback), where displayCallback is a
function that contains the OpenGL rendering code.

Timer and Idle Functions: Functions to set up timers and manage animations or periodic
tasks. For example, glutTimerFunc(millis, timerCallback, value)

Utility Functions: Functions to manage the OpenGL context and handle window resizing. For
example, glutReshapeFunc(reshapeCallback), where reshapeCallback is a function that
adjusts the viewport when the window is resized.

3.10 Simple Interaction with the Mouse and Keyboard in OpenGL

In OpenGL, keyboard and mouse interactions are typically handled through additional libraries
or toolkits that manage windowing and input events. OpenGL itself does not provide direct
mechanisms for handling input; instead, it relies on these external libraries to interface with the
system's input devices.

Two commonly used libraries for this purpose are GLUT (OpenGL Utility Toolkit) and
GLFW (Graphics Library Framework).

Here’s a general overview of how keyboard and mouse interactions can be managed using the
GLUT library:

Keyboard Interaction:

1. Register Callback Function:

Use glutKeyboardFunc to register a function that will be called whenever a key is pressed.

void keyboard(unsigned char key, int x, int y) {

// Handle keyboard input

if (key == 'q') {

exit(0); // Quit the program when 'q' is pressed

int main(int argc, char** argv) {


13 | P a g e

glutInit(&argc, argv);

glutCreateWindow("GLUT Keyboard Example");

glutKeyboardFunc(keyboard); // Register keyboard callback

glutMainLoop();

return 0;

2. Callback Parameters

unsigned char key: The ASCII value of the key pressed.

int x, int y: The x and y coordinates of the mouse when the key was pressed (if
applicable).

Mouse Interaction:

1. Register Callback Function:

Use glutMouseFunc to register a function that will be called when mouse buttons are
pressed or released.

void mouse(int button, int state, int x, int y) {

if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {

// Handle left mouse button press

int main(int argc, char** argv) {

glutInit(&argc, argv);

glutCreateWindow("GLUT Mouse Example");

glutMouseFunc(mouse); // Register mouse callback

glutMainLoop();

return 0;

}
14 | P a g e

2. Callback Parameters

int button: The mouse button that was pressed (e.g., GLUT_LEFT_BUTTON,
GLUT_RIGHT_BUTTON).

int state: The state of the button (e.g., GLUT_DOWN, GLUT_UP).

int x, int y: The x and y coordinates of the mouse pointer when the button was pressed
or released.

4. Scan Conversion

4.1 Comparison between Random Scan and Raster Scan

Sr No Random Scan Raster Scan


1 Represents images as a collection of Represents images as a collection of pixels
lines, curves, and other geometric arranged in rows and columns (raster
shapes (vector graphics). graphics).

2 Uses a beam of electrons to draw The electron beam sweeps across the
lines directly on the screen in any screen from left to right and top to bottom
order. to illuminate the pixels.

3 Resolution–independent; the quality Resolution–dependent; the quality of


of images doesn’t degrade when images can degrade when scaled due to
scaled. pixelation.

4 Variable refresh rate, depending on Fixed refresh rate, typically 60Hz or


the complexity of the image. higher, to maintain consistent image
display.

5 Ideal for applications involving line Suitable for rendering complex images,
drawings, CAD, and simple graphics. including photos, videos, and detailed
graphics.

6 Requires less memory since only the Requires more memory to store the entire
coordinates of the endpoints of lines frame buffer, which holds the pixel data
are stored. for the entire screen.
15 | P a g e

7 Limited color capability; typically Supports a wide range of colors, making it


used for monochrome displays or suitable for displaying complex images
simple color schemes. and multimedia.

4.1 DDA Line Drawing Algorithm

The Digital Differential Analyzer (DDA) Line Drawing Algorithm is a popular algorithm used
in computer graphics to draw lines between two points on a grid or screen. The DDA algorithm
is simple and efficient for generating lines with arbitrary slopes.

The idea behind the DDA algorithm is to calculate values between the start and end points by
incrementing either the x–coordinate or the y–coordinate based on the line's slope. The
algorithm steps through one coordinate at a time (either x or y) and calculates the corresponding
y or x value using the line's slope.

Steps in the DDA Algorithm:

1. Write the start and end points of the line, (x0, y0) and (x1, y1)

2. Calculate Δx and Δy:

o Compute the differences Δx and Δy between the start and end points:

Δx = x1 − x0

Δy = y1 − y0

3. Determine the number of steps:

o The number of steps required to generate the line is the maximum of ∣Δx∣ and
∣Δy∣.

o steps = max(∣Δx∣, ∣Δy∣)

o This ensures that we cover the line's entire length.

4. Calculate the increments:

o Compute the increment values for each step:

o xi =
16 | P a g e

o yi =

5. Start drawing the line:

o Initialize the starting point (x, y) = (x0, y0).

o For each step, plot the pixel at the current (x, y) coordinates, then update the
coordinates:

o x = x + xi

o y = y + yi

6. Round the coordinates and plot each point:

o Since pixel coordinates must be integers, round the values of x and y to the
nearest integer before plotting the pixel.

Advantages of DDA:

 Simple and easy to implement.

 Can handle all types of slopes (positive, negative, zero, and infinite).

Disadvantages of DDA:

 The algorithm involves floating–point arithmetic, which can be slower and less efficient
on systems without floating–point hardware.

 The rounding step can introduce errors in drawing the line.

Example: Consider a line segment from A(−2, −1) to B(6, 3). Use DDA Line drawing algorithm
to rasterize this line.

Step 1: Starting points: A(x0, y0) = (−2,−1)

Ending points: B(x1,y1) = (6, 3)

Step 1: Calculate Δx and Δy:

Δx = (x1 − x0) = [6−(−2)] = 6 + 2 = 8

Δy = (y1 − y0) = [3−(−1)] = 3 + 1 = 4

Step 2: Determine the number of steps:


17 | P a g e

steps = max(∣Δx∣, ∣Δy∣) = max(8, 4) = 8

Step 3: Calculate the increments:

xi = = =1

yi = = = 0.5

Step 4: Start drawing the line:

Initialize starting point: (x, y) = (−2, −1)

For each step, plot the pixel at the current (x, y) coordinates, then update the coordinates:

x = x + xi

y = y + yi

Step 5: Round the coordinates and plot each point:

Steps x y Plotted Point

1 −1 −1 (−1, −1)

2 0 0 (0, 0)

3 1 0.5 (1, 1)

4 2 1 (2, 1)

5 3 1.5 (3, 2)

6 4 2 (4, 2)

7 5 2.5 (5, 3)

8 6 3 (6, 3)

4.2 Bresenham’s Line Drawing Algorithm

Bresenham's Line Drawing Algorithm is an efficient method used in computer graphics to draw
lines on a pixel–based display. Unlike the DDA (Digital Differential Analyzer) algorithm,
Bresenham's algorithm uses only integer arithmetic, making it faster and more suitable for
systems that lack floating–point hardware.
18 | P a g e

The algorithm determines the closest pixel to the theoretical line at each step, using incremental
error terms to decide whether to move horizontally, vertically, or diagonally.

Steps in Bresenham's Line Drawing Algorithm:

Step 1: Calculate Δx and Δy:

Δx = x1 − x0

Δy = y1 − y0

Step 2: Initialize Decision Parameter:

Define the initial decision parameter pk based on the slope:

pk = 2Δy – Δx

This parameter helps in deciding the next pixel to plot.

Step 3: Plot the initial points:

Start by plotting the first point (x0, y0).

Step 4: Iterate over x or y for steep lines and plot points:

For each x from x0 to x1, do the following:

 If pk < 0, the next point is (xk + 1, yk). Update the decision parameter:

o pk+1 = pk + 2Δy

 If pk ≥ 0, the next point is (xk + 1, yk+1). Update the decision parameter:

o pk+1 = pk + 2Δy − 2Δx

Repeat the above steps until you reach (x1, y1).

Example: Consider a line from (1, 1) to (6, 4). Use Bresenham’s algorithm to rasterize this
line.

Step 1: Calculate Δx and Δy:

Δx = x1 − x0 = 6 – 1 = 5

Δy = y1 − y0 = 4 – 1 = 3

Step 2: Initialize Decision Parameter:


19 | P a g e

pk = 2Δy – Δx = 2(3) – 5 = 1

Step 3: Plot the initial points:

Start by plotting the first point (1, 1).

Step 4: Iterate over x or y for steep lines and plot points:

pk+1 = pk + 2Δy − 2Δx = 1 + 2(3) – 2(5) = –3 = updated pk

pk+1 = pk + 2Δy = –3 + 2(3) = 3 = updated pk

pk+1 = pk + 2Δy – 2Δx = 3 + 2(3) – 2(5) = –1 = updated pk

pk+1 = pk + 2Δy = –1 + 2(3) = 5 = updated pk

Steps pk x y Plotted Point

1 1 2 2 (2, 2)

2 –3 3 2 (3, 2)

3 3 4 3 (4, 3)

4 –1 5 3 (5, 3)

5 5 6 4 (6, 4)

4.3 Expression for Decision Parameter in Bresenham’s line drawing algorithm

To derive an expression that helps determine the next pixel to be drawn when rasterizing a line
with a slope 0 ≤ m ≤ 1, ensuring the algorithm only uses integer arithmetic.
20 | P a g e

Consider a line with slope, 𝑚 =

We know, y = mx + c

But x = xk+1

∴ y = m(xk+1) + c

Also, xk+1 = xk + 1 and yk+1 = yk + 1

To find the decision parameter, pk

pk = Δx(d1–d2)

Now, d1 = y – yk = m(xk+1) + c – yk

And, d2 = yk+1 – y = yk + 1 – m(xk+1) – c

d1 – d2 = m(xk+1) + c – yk – yk – 1 + m(xk+1) + c

= 2m(xk+1) – 2yk + 2c – 1

pk = Δx [2m(xk+1) – 2yk + 2c – 1]

We know, 𝑚 =

∴ pk = Δx [2 (xk+1) – 2yk + 2c – 1]

= 2Δyxk + 2Δy – 2ykΔx + 2cΔx – Δx

= 2Δyxk + 2Δy – 2ykΔx + Δx (2c – 1)

To find pk+1

pk+1 = 2Δyxk+1 + 2Δy – 2yk+1 Δx + Δx (2c – 1)

Now,

pk+1 – pk = 2Δyxk+1 + 2Δy – 2yk+1 Δx + Δx (2c – 1) – 2Δyxk – 2Δy + 2ykΔx – Δx (2c – 1)

pk+1 – pk = 2Δy (xk+1 – xk ) – 2Δx (yk+1 – yk )

pk+1 – pk = 2Δy (xk +1 – xk ) – 2Δx (yk+1 – yk )

∴ pk+1 = pk + 2Δy – 2Δx (yk+1 – yk )

As y = mx + c, c = y – mx
21 | P a g e

∴ pk = 2xkΔy + 2Δy – 2ykΔx + Δx [2(y – mx) – 1]

= 2xkΔy + 2Δy – 2ykΔx + 2yΔx – 2xΔy – Δx [∵ 𝑚 = ]

As we have considered, x = xk and y = yk

∴ pk = 2xΔy + 2Δy – 2yΔx + 2yΔx – 2xΔy – Δx

∴ pk = 2Δy – Δx

This is the derivation for the decision parameter in Bresenham’s Line drawing Algorithm.

4.4 Comparison between DDA Line Drawing Algorithm and Bresenham’s Line
Drawing Algorithm

S. No. DDA Line Drawing Algorithm Bresenham’s Line Drawing Algorithm


Uses floating–point arithmetic to Uses integer arithmetic to determine the

1 calculate intermediate points along closest pixel to the line.


the line.

Involves addition, subtraction, and Primarily involves integer addition and


2 multiplication of floating–point subtraction.
numbers.
Can introduce rounding errors More accurate in plotting pixels close to
3 because of the floating–point the actual line path, with no rounding
operations. issues.

Slower due to the use of floating– Faster, as it uses only integer arithmetic,
4 point calculations, which require making it more efficient on systems
more processing power. without floating–point hardware.

Simpler to understand and Slightly more complex due to the


5 implement. decision parameter and multiple cases
for different slopes.

May produce lines with small Produces smooth lines with evenly
6 deviations due to rounding, resulting distributed pixels, closely following the
in uneven pixel distribution. actual line.
22 | P a g e

Requires more memory due to Requires less memory due to the use of
7
floating–point operations. integer arithmetic.

4.4 DDA Algorithm of Circle

The Digital Differential Analyzer (DDA) algorithm for drawing a circle is a method used to
plot the points of a circle in a rasterized form. The DDA algorithm is more commonly
associated with line drawing, but it can be adapted for circles. However, the Bresenham's
algorithm is typically preferred for circle drawing due to its efficiency.

Circle Equation:

Consider a circle centered at the origin (0,0):

x2 + y2 = r2

DDA Algorithm Steps:

1. Initialize Parameters:

o Start from a point on the circle, typically at the topmost point (0, r).

o Set initial coordinates x = 0 and y = r.

2. Calculate the Incremental Steps:

o Calculate the angle increment, θ, which is small enough to ensure that the points
are close to each other:

Δ𝜃 =

o Calculate the incremental steps for x and y:

Δx = cos (θ) × Δθ

Δy=sin (θ) × Δθ

3. Iterate and Plot Points:

o For each step, update the coordinates using:

x = x + Δx
23 | P a g e

y = y −Δy

o Plot the pixel at (x, y).

o Repeat until x exceeds y.

4. Symmetry Consideration:

o Because of the circle's symmetry, you can mirror the points in all eight octants
of the circle. This means that for each calculated point (x, y), you can also plot
the points (y, x), (−x, y), (−y, x), and so on.

4.5 Bresenham’s Circle Drawing Algorithm

Bresenham's Circle Drawing Algorithm is a rasterization algorithm used to draw a circle in


computer graphics.

The algorithm is efficient because it uses only integer addition, subtraction, and bit shifting,
making it much faster than other methods that involve floating–point arithmetic.

Key Concepts:

 Symmetry: A circle is symmetric about its center, which means that when you calculate
one point on the circumference, you can easily find the corresponding points in the
other seven octants of the circle.

 Decision Parameter: The algorithm uses a decision parameter to decide whether to


move horizontally or diagonally at each step to approximate the circle.

Steps of the Algorithm:

1. Initial Setup:

o Consider a circle centered at (0,0) with radius r.

o Start from the topmost point (0, r).

o The initial decision parameter p0 is calculated as p0 = 3 – 2r

o This decision parameter helps in determining whether the next point will be
directly to the right or diagonally down to the right.

2. Iterative Calculation:
24 | P a g e

o For each point (x, y) on the circle's perimeter, the next point is determined based
on the value of the decision parameter pk.

o If pk < 0, the next point is (x+1, y) and the decision parameter is updated as:

pk + 1 = pk + 4xk + 6

o If pk ≥ 0 the next point is (x+1, y−1), and the decision parameter is updated as:

pk + 1 = pk + 4(xk − yk) + 10

3. Symmetry Exploitation:

o For every calculated point (x, y), the corresponding points in all eight octants of
the circle are plotted. The symmetry points are (x, y), (y, x), (−x, y), (−y, x),
(x,−y), (y,−x), (−x,−y), (−y,−x)

4. Stopping Condition:

o The algorithm continues iterating until x becomes greater than or equal to y.

4.6 Derivation of Decision Parameter in Bresenham’s Circle Drawing Algorithm

Assume that the circle is at some random pixel P whose coordinates are (xk, yk).

To find the next pixel, we need to decide whether the next pixel would be in North or South
direction.
25 | P a g e

Bresenham's circle drawing algorithm helps us decide this by calculating the difference
between radius and the coordinates of the next pixels.

The shortest of d1 and d2 helps us decide our next pixel.

xk+1 = xk + 1

As xk+1 is the next consecutive pixel of xk

Similarly,

yk–1 = yk – 1

Equation of Circle with Radius r:

(x– h)2 + (y – k)2 = r2

When coordinates of center are at origin i.e., (h=0, k=0)

x2 + y2 = r2 (Using Pythagoras Theorem)

Function of Circle Equation

F(C) = x2 + y2 – r2

Function of Circle at N

F(N) = (xk+1)2 + (yk)2 – r2 (Positive)

Here the value of F(N) is positive because N is outside the circle and that makes

(xk+1)2 + (yk)2 greater than r2

Function of Circle at S

F(S) = (xk+1)2 + (yk–1)2 – r2 (Negative)

Here the value of F(S) will be Negative because S is inside the circle and that makes

(xk+1)2 + (yk–1)2 less than r2

Now we need a decision parameter which helps us decide the next pixel, i.e., pk

pk = F(N)+F(S)

Here, we get either a positive or a negative value of pk


26 | P a g e

If pk < 0, F(S) is bigger than F(N), which means that Point N is closer to the circle than point S,
so we select pixel N as our next pixel.

If pk > 0, F(N) is bigger than F(S), which means that point S is closer to the circle than point N,
and we select pixel S as our next pixel.

To find pk

pk = (xk+1)2 + (yk)2 – r2 + (xk+1)2 + (yk–1)2 – r2

Replacing xk+1 with xk + 1 and yk–1 with yk – 1

pk = (xk + 1)2 + (yk)2 – r2 + (xk + 1)2 + (yk –1)2 – r2

pk = 2(xk + 1)2 + (yk)2 + (yk –1)2 – 2r2 …(1)

To find pk+1

Replacing every k with k+1

pk+1 = 2(xk+1 + 1)2 +(yk+1)2 + (yk+1 –1)2 – 2r2

pk+1 = 2(xk+1 + 1)2 + (yk+1)2 + (yk+1 –1)2 – 2r2

Replacing xk+1 with xk + 1, but we cannot replace yk+1 as we do not know the exact value of yk

pk+1 = 2(xk+1+ 1)2 + (yk+1)2 + (yk+1 –1)2 – 2r2

pk+1 = 2(xk+2)2 + (yk+1)2 + (yk+1 –1)2 – 2r2 …(2)

Now to find pk+1 – pk

Subtracting Equation 1 from Equation 2:

pk+1 – pk = 2(xk+2)2 + (yk+1)2 + (yk+1 –1)2 – 2r2 – [ 2(xk + 1)2 + (yk)2 + (yk –1)2 – 2r2]

pk+1 – pk = 2(xk)2 + 8xk + 8 + (yk+1)2 + (yk+1)2 – 2yk+1 + 1 – 2r2 – 2xk – 4xk – 2 – (yk)2 –
(yk)2 + 2yk – 1 + 2r2

pk+1 – pk = 4xk + 2(yk+1)2 – 2yk+1 – 2(yk)2 – 2yk + 6

pk+1 = pk + 4xk + 2(yk+1)2 – 2yk+1 – 2(yk)2 – 2yk + 6 …(3)

If (pk < 0), we choose point N as discussed above, i.e., (xk+1, yk)

That means our next x coordinate is xk+1 and next y coordinate is yk,
27 | P a g e

i.e., yk+1 = yk,

Substituting yk in Equation (3) instead of yk+1

Now, pk+1 = pk + 4xk + 2(yk)2 – 2yk – 2(yk)2 – 2yk + 6

pk+1 = pk + 4xk + 6

If (pk > 0), we choose point S.

i.e. (xk+1, yk–1)

That means our next x coordinate is xk+1 and next y coordinate is yk

i.e. yk+1 = yk–1

Substituting yk–1 in Equation 3 instead of yk+1

Now, pk+1 = pk + 4xk + 2(yk–1)2 – 2yk–1 – 2(yk)2 – 2yk + 6

We know

yk–1 = yk – 1

Therefore,

pk+1 = pk + 4xk + 2(yk –1)2 – 2(yk –1) – 2(yk)2 – 2yk + 6

pk+1 = pk + 4xk + 2(yk) 2 + 2 – 4yk – 2yk +2 – 2(yk)2 – 2yk + 6

pk+1 = pk + 4xk – 4yk + 10

pk+1 = pk + 4(xk – yk) + 10

To find initial decision parameter (p0), we use the starting points (0, r)

Here, the value of y is r

Putting (0, r) in Equation (1), we get:

p0 = 2(0 + 1)2 + r2 + (r –1)2 – 2r2

p0 = 2 + r2 +r2 + 1 – 2r – 2r2

p0 = 3–2r

This is the derivation for the decision parameter in Bresenham’s Circle drawing Algorithm.
28 | P a g e

4.7 Midpoint Circle Drawing Algorithm

The Midpoint Circle Drawing Algorithm is another efficient way to draw a circle on a raster
display. It is conceptually similar to Bresenham's Circle Drawing Algorithm but is derived
directly from the midpoint method, which uses the midpoint between two candidate pixels to
determine which one is closer to the actual circle.

Concept:

The Midpoint Circle Drawing Algorithm uses the symmetry of a circle to minimize the number
of calculations. By calculating the points for one octant (1/8th of the circle), the algorithm can
reflect those points into the other seven octants, significantly reducing the amount of work
required.

Steps of the Midpoint Circle Drawing Algorithm:

1. Circle Equation:

o The equation of a circle centered at the origin (0, 0) with radius r is:

x2 + y2 = r2

2. Initial Setup:

o Start at the topmost point on the circle: (x0, y0) = (0, r).

o Initialize the decision parameter p0 = 1.25 − r

This decision parameter helps determine whether the next pixel should be
directly East or South–East of the current pixel.

3. Decision Making: For each pixel, the algorithm needs to decide between two possible
next points:

o Directly to the right (East): (x+1, y)

o Diagonally down and to the right (South–East): (x+1, y − 1)

o The decision is made based on the value of the decision parameter pk:

o If pk < 0: The midpoint is inside the circle, so the next point is East.

o If pk ≥ 0: The midpoint is outside or on the circle, so the next point is South–


East.
29 | P a g e

4. Updating the Decision Parameter:

o If pk < 0 (choosing East): pk+1 = pk + 2xk + 3

o If pk ≥ 0 (choosing South–East): pk + 1 = pk + 2(xk − yk) + 5

5. Plotting Points:

o For each calculated point (x,y) in the first octant, plot it and use the circle’s
symmetry to plot the corresponding points in the other seven octants; (x, y), (y,
x), (−x, y), (−y, x), (x,−y), (y,−x), (−x,−y), (−y,−x)

6. Continue Until x ≥ y: The process continues until x ≥ y, which means the entire octant
has been drawn.

4.7 Derivation of Midpoint Circle Drawing Algorithm

Assume that there is a random pixel P whose coordinates are (xk, yk).

Now we need to determine the next pixel.

The circle is moving forward and downwards so y can never be increased, either it can be the
same or decremented.

Similarly, x will always be increasing as the circle is moving forward too.

Now we need to decide whether we should go with point N or S.


30 | P a g e

For that decision, midpoint circle drawing technique helps us decide whether our next pixel be
towards N or S.

As xk+1 is the next pixel, we can write:

xk+1 = xk + 1

Similarly,

yk−1 = yk − 1

Let M be the midpoint between N (xk+1, yk) and S (xk+1, yk−1).

And coordinates of point M are

M =

= ,

1
= ( 𝑥 + 1, 𝑦 − )
2

Equation of Circle with Radius r:

(x– h)2 + (y – k)2 = r2

When coordinates of center are at Origin i.e., (h=0, k=0)

x2 + y2 = r2

Function of Circle Equation

F(C) = x2 + y2 – r2

1
Function of Midpoint M ( 𝑥 + 1, 𝑦 − ) in circle equation
2

1
F(M) = xk+12 + (𝑦 − )2 – r2
2

1
F(M) = (xk+1)2 + (𝑦 − )2 – r2
2

The above equation is also our decision parameter pk

1
pk = (xk+1)2 + (𝑦 − )2 – r2 …(1)
2
31 | P a g e

To find out the next decision parameter we need to get pk+1

1
pk+1 = (xk+1+1)2 + (𝑦 − )2 – r2
2

Now to find pk+1 – pk:

1 1
pk+1 – pk = (xk+1+1)2 + (𝑦 − )2 – r2 –[(xk+1)2 + (𝑦 − )2 – r2]
2 2

1 1
pk+1 – pk = [(xk+1) 2 + (1) 2] + (𝑦 − )2 – (xk+1)2 – (𝑦 − )2
2 2

1 1
pk+1 – pk = (xk+1)2 + 12 + 2(xk+1) + yk+12 + ( ) – yk+1 – (xk+1)2 – yk2 – ( ) + yk+1
4 4

pk+1 – pk = 2(xk+1) + yk+12 – yk2 – yk+1 + yk +1

pk+1 – pk = 2(xk+1) + (yk+12 – yk2) – (yk+1 – yk) + 1

pk+1 = pk + 2(xk+1) + (yk+12 – yk2) – (yk+1 – yk) +1 ...(2)

Now let us conclude the initial decision parameter

For that we have to choose coordinates of starting point i.e. (0, r)

Substituting (0, r) in Equation (1)

p0 = (0+1)2 + (r – )2 – r2

p0 = 1 + r 2 + – r – r2

p0 = 1 + – r

p0 = –r ...(initial decision parameter)

If pk ≥ 0 that means midpoint is outside the circle and S is closest pixel so we will
choose S (xk+1, yk–1)

That means yk+1 = yk–1

Putting coordinates of S in (ii) then,

pk+1 = pk + 2(xk+1) + (yk–12 – yk2 ) – (yk–1 – yk) +1

= pk + 2(xk+1) + (yk –1)2 – yk2 ) – [(yk–1) – yk] +


32 | P a g e

= pk + 2(xk+1) + yk 2+1 – 2yk – yk2 – yk+1 + yk +1

= pk + 2(xk+1) – 2yk +2 + 1

= pk + 2(xk+1) – 2(yk –1) + 1

As we know (xk+1 = xk+1) and (yk – 1 = yk–1)

Therefore,

pk+1 = pk + 2xk+1 – 2yk–1 + 1

And if pk < 0 that means midpoint is inside the circle and N is closest pixel so we will
choose N (xk+1, yk)

i.e. yk+1 = yk

Now put coordinates of N in Equation 2

pk+1 = pk + 2(xk+1) + (yk2 – yk2) – (yk – yk) +1

= pk + 2(xk+1) + (yk2 – yk2) – (yk – yk) +1

= pk + 2(xk+1) +1

As xk+1 = xk+1, therefore,

pk+1 = pk + 2xk+1 +1

Hence, we have derived the midpoint circle drawing algorithm.

You might also like