0% found this document useful (0 votes)
8 views53 pages

Class 1

The document provides an overview of the history and development of computer graphics from the 1940s to the present, highlighting key milestones such as the introduction of graphical displays, the creation of the first GUI program Sketchpad, and the rise of 3D graphics. It also explains fundamental concepts of computer graphics, including raster and vector graphics, input devices, and line drawing algorithms like the Digital Differential Analyzer (DDA). Additionally, it includes multiple-choice questions to test knowledge on computer graphics concepts and input devices.
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)
8 views53 pages

Class 1

The document provides an overview of the history and development of computer graphics from the 1940s to the present, highlighting key milestones such as the introduction of graphical displays, the creation of the first GUI program Sketchpad, and the rise of 3D graphics. It also explains fundamental concepts of computer graphics, including raster and vector graphics, input devices, and line drawing algorithms like the Digital Differential Analyzer (DDA). Additionally, it includes multiple-choice questions to test knowledge on computer graphics concepts and input devices.
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/ 53

Computer Graphics

Online class for


PSC and NEA

Associate Professor
9801104103
[email protected]

1/28/2025 Associate Professor Sujan Shrestha


CG history

• Early Development (1940s-1950s)


• The first graphical computer display
was developed on the Cathode Ray
Tube (CRT) in the 1940s.
• In 1951, Whirlwind I became one of
the first computers to use a graphical
display.
• 1960s: Foundations of Modern
Graphics
• Sketchpad (1963): Developed by
Ivan Sutherland also known as
(father of computer graphics), it
was the first graphical user
interface (GUI) program that
allowed users to draw directly on a
screen.
• Vector graphics became a
foundation for early graphical
systems.
1970s: Advancements in Hardware and Software
• Raster Graphics: These replaced vector graphics as CRT technology
improved.
• Lucasfilm and Pixar: Early groundwork for computer-generated
imagery (CGI) was laid during this era.
• The First Computer Animation was created by Ed Catmull and
Fred Parke in 1972.
1980s: Mainstream Adoption
• Personal computers like Apple Macintosh (1984) introduced graphical
user interfaces to consumers.
• The movie Tron (1982) popularized computer graphics in
entertainment.
1990s: Explosion of 3D Graphics
• Introduction of 3D Graphics in gaming, such as Doom (1993) and
Quake (1996).
• Movies like Toy Story (1995) showcased fully computer-generated
imagery.
2000s and Beyond
• Virtual Reality (VR) and Augmented Reality (AR) gained popularity.
• The rise of AI and machine learning introduced advanced techniques
like Deep Learning for Image Synthesis.
What is Computer Graphics?
• Computer graphics is an art of drawing lines, charts, pictures, etc. using
computers with the help of programming and tools.
• Computer graphics image is made up of number of pixels.
• Picture Element (Pixel) is the smallest addressable graphical unit represented on
the computer screen. Chart Title
14

12

10

2
Source: template.net
0
Category 1 Category 2 Category 3 Category 4
Series 1 Series 2 Series 3
Contd.
• There are two techniques used for producing images on the CRT
screen:
1. Vector scan/Random scan display.

Source: tutorialspoint.com
2. Raster scan display.

Horizontal
Vertical OF ON Retrace
Retrace F

Source: eenadupratibha.net
Random Scan v/s Raster Scan
Base of Raster Scan System Random Scan System
Difference

Electron The electron beam is swept The electron beam is directed only
Beam across the screen, one row at a to the parts of screen where a
time, from top to bottom. picture is to be drawn.

Resolution Its resolution is poor. Its resolution is good.

Picture Picture definition is stored as a Picture definition is stored as a set


Definition set of intensity values for all of line drawing instructions in a
screen points, called pixels in a display file.
refresh buffer area.
Contd.
Base of Raster Scan System Random Scan System
Difference

Realistic The capability of this system to These systems are designed for line-
Display store intensity values for pixel drawing and can’t display realistic
makes it well suited for the shaded scenes.
realistic display of scenes contain
shadow and color pattern.

Draw Screen points/pixels are used to Mathematical functions are used to


image draw an image. draw an image.

Cost They are cheaper than random It is more expensive than raster scan
display display
Contd.
Base of Raster Scan System Random Scan System
Difference

Refresh Refresh rate is 60-80 fps All components are drawn 30 to 60


rate times per second

interlacing It uses interlacing It doesn’t use interlacing.

Refresh Editing is Refresh area is Refresh area depends on complexity


area independent of picture of picture
complexity

Example CRT, TV, printer Pen plotter


Interlacing
• Interlacing in graphics refers to a technique used to display or render
images progressively by dividing them into alternating sets of lines or
pixels.
Input Devices in Computer Graphics
• Input devices in computer graphics are used to
interact with and provide data or commands to the
computer. These devices enable users to create,
manipulate, and edit graphics. Common input
devices include:
• Mouse: Used for pointing, clicking, and selecting
graphical elements.
• Keyboard: For typing commands or controlling
graphical operations.
• Joystick: For controlling movements in 3D space,
commonly used in gaming and simulations.
• Graphics Tablet: A flat surface that allows users to
draw directly into the computer.
• Light Pen: A pen-like device that can draw
or select on a screen.

• Scanner: Converts physical images and


documents into digital formats.

• Trackball: A pointing device similar to a


mouse but with a stationary base.
• Touchscreen: Allows users to
interact directly with the display.

• Data gloves : Devices like data


gloves is used for virtual reality and
CAD applications.
• Who is known as the father of computer graphics?
Sample MCQs: • (A) Ed Catmull
• (B) Ivan Sutherland
• (C) William Fetter
• (D) Alan Turing
Answer: B
• What was the name of the first graphical user interface (GUI)
program?
• (A) Paint
• (B) Sketchpad
• (C) Photoshop
• (D) CorelDRAW
Answer: B
• Which movie was the first to be entirely computer-generated?
• (A) Toy Story
• (B) Tron
• (C) Jurassic Park
• (D) Avatar
Answer: A
Graphics Concepts - MCQs
• What is the smallest addressable element of a screen?
A) Pixel
B) Frame
C) Resolution
D) Dot Pitch
Answer: A) Pixel
• What does GPU stand for in computer graphics?
A) General Processing Unit
B) Graphics Processing Unit
C) General Purpose Unit
D) Graphics Performance Unit
Answer: B) Graphics Processing Unit
Input Devices - MCQs
• Which of the following is a pointing device?
A) Mouse
B) Keyboard
C) Scanner
D) Printer
Answer: A) Mouse
• Which input device is commonly used for CAD applications?
A) Trackball
B) Joystick
C) Light Pen
D) Graphics Tablet
Answer: D) Graphics Tablet
• What type of device is a touch screen?
A) Output Device
B) Input Device
C) Both Input and Output Device
D) None of the above
Answer: C) Both Input and Output Device
MCQs on Input Devices in Computer Graphics
• Which of the following is NOT an input device in computer graphics?
a) Mouse
b) Printer
c) Light Pen
d) Joystick
Answer: b) Printer
• A device used to directly draw on the screen is called a:
a) Joystick
b) Light Pen
c) Graphics Tablet
d) Scanner
Answer: b) Light Pen
• Which input device is commonly used for 3D modeling and design?
a) Touchscreen
b) Joystick
c) 3D Mouse
d) Keyboard
Answer: c) 3D Mouse
• What is the primary use of a digitizer in computer graphics?
a) To convert digital images into physical objects
b) To capture the coordinates of a physical object
c) To input commands
d) To navigate the desktop environment
Answer: b) To capture the coordinates of a physical object
• Which of the following devices can be used for direct manipulation of
objects in virtual reality?
a) Data Glove
b) Scanner
c) Keyboard
d) Trackball
Answer: a) Data Glove
• Which input device works by detecting touch or stylus pressure?
a) Graphics Tablet
b) Mouse
c) Light Pen
d) Trackball
Answer: a) Graphics Tablet
Computer Graphics

Raster Graphics
and Algorithms

Associate Professor
9801104103
[email protected]

1/28/2025 Associate Professor Sujan Shrestha


Point
• Point plotting is done by converting a single
coordinate position furnished by an application 7
program into appropriate operations for the
output device in use. 6
• Example: Plot point 𝑃 (3, 2) 5
 Line 𝑥 = 3 4
 Line 𝑦 = 2
 Point 𝑃 (3, 2) 3
• To draw the point on the screen we use function 2
 𝑠𝑒𝑡𝑝𝑖𝑥𝑒𝑙 (𝑥, 𝑦) 1
• To draw the pixel in C language we use function 0
 𝑝𝑢𝑡𝑝𝑖𝑥𝑒𝑙 (𝑥, 𝑦, 𝑐𝑜𝑙𝑜𝑟) 0 1 2 3 4 5 6 7
• Similarly for retrieving color of pixel we have
function
 𝑔𝑒𝑡𝑝𝑖𝑥𝑒𝑙 (𝑥, 𝑦)
Line
• Line drawing is done by calculating intermediate
positions along the line path between two specified
endpoint positions.
• The output device is then directed to fill in those
positions between the 5
5 end points with some color.
• For some device such 4
4 as a pen plotter or random scan
display, a straight line
3
3 can be drawn smoothly from one
end point to other. 22
• Digital devices display
1
1
a straight line segment by
plotting discrete points
0
0
0 1
between
22
the two
34 4 6
endpoints.
• Discrete coordinate positions along the line path are
calculated from the equation of the line.
Contd.
• Screen locations are referenced with integer values.
• So plotted positions may only approximate actual
line positions between two specified endpoints. For
example line position of (12.36, 23.87) would be
converted to pixel position (12, 24).
• This rounding of coordinate values to integers
causes lines to be displayed with a stair step
appearance (“the Jaggies”).
Line Drawing Algorithms
• The Cartesian slop-intercept equation for a straight
line is
• 𝑦 = 𝑚𝑥 + 𝑏
• with ‘𝒎’ representing slop and ‘𝒃’ as the intercept.
• It is possible to draw line using this equation but for
efficiency purpose we use different line drawing
algorithm.
• DDA Algorithm
• Bresenham’s Line Algorithm
• We can also use this algorithm in parallel if we have
more number of processors.
Computer Graphics

Digital differential
analyzer

Associate Professor
9801104103
[email protected]

1/28/2025 Associate Professor Sujan Shrestha


Introduction to DDA Algorithm
• Full form of DDA is Digital Differential Analyzer
• DDA is scan conversion line drawing algorithm
based on calculating either ∆𝑦 or ∆𝑥 using line
equation.
• We sample the line at unit intervals in one
coordinate and find corresponding integer values
nearest the line path for the other coordinate.
• Selecting unit interval in either 𝑥 or 𝑦 direction
based on way we process line.
Unit Step Direction in DDA
Algorithm
• Processing from left to right.
Slope is “+ve”, & Magnitude is Less than Δ𝑋
1 =1
Slope is “-ve”, & Magnitude is Less than 1
Δ𝑌 = 1
Slope is “+ve”, & Magnitude is greater than 1
Δ𝑌 = −1
Slope is “-ve”, & Magnitude is greater than 1

• Processing from right to left. Δ𝑋 = −1


Slope is “+ve”, & Magnitude is Less than 1Δ𝑌 = −1
Slope is “-ve”, & Magnitude is Less than 1Δ𝑌 = 1
Slope is “+ve”, & Magnitude is greater than 1
Slope is “-ve”, & Magnitude is greater than 1
left to right; ∆x is positive

|∆y| > |∆x| it also means


|m| >1; ∆y =1, ∆x = 1/m
m is positive(x and y
both increasing); ∆y =1,
∆x = 1/m

Right to left; ∆x is negative left to right; ∆x is positive

|∆x| > |∆y| it also means |∆x| > |∆y| it also means
|m| < 1; ∆x = 1, ∆y = m |m| < 1; ∆x = 1, ∆y = m

m is positive(x is decreasing m is positive(check if x


and y is increasing); ∆y = m, and y both increasing); ∆y
∆x = -1 = m, ∆x = 1

left to right; ∆x is positive


|∆x| > |∆y| it also means
|m| < 1; ∆x = 1, ∆y = m
m is negative (x increasing
and y decreasing); ∆y = -
m, ∆x = 1
Derivation of DDA Algorithm
• We sample at unit 𝑥 interval (∆𝑥 = 1) and calculate each successive 𝑦
value as follow:
• 𝑦 = 𝑚𝑥 + 𝑏
• 𝑦1 = 𝑚 𝑥 + 1 + 𝑏 [For first intermediate point]
• 𝑦𝑘 = 𝑚 𝑥 + 𝑘 + 𝑏 [For 𝑘𝑡ℎ intermediate point]
• 𝑦𝑘+1 = 𝑚 𝑥 + 𝑘 + 1 + 𝑏 [For 𝑘 + 1𝑡ℎ intermediate point]
• Subtract 𝑦𝑘 from 𝑦𝑘+1
• 𝑦𝑘+1 − 𝑦𝑘 = 𝑚 𝑥 + 𝑘 + 1 + 𝑏 − 𝑚 𝑥 + 𝑘 − 𝑏
• 𝑦𝑘+1 = 𝑦𝑘 + 𝑚
• Using this equation computation becomes faster than normal line
equation.
• As 𝒎 is any real value calculated 𝒚 value must be rounded to nearest
integer.
Contd.
• We sample at unit 𝑦 interval (∆𝑦 = 1) and calculate each
successive 𝑥 value as follow:
• 𝑥 = (𝑦 − 𝑏)/𝑚
• 𝑥1 =((‫ݕ‬+1)−ܾ)/݉ [For first intermediate point]
• 𝑥𝑘 =((‫ݕ‬+k)−ܾ)/݉ [For 𝑘𝑡ℎ intermediate point]
• 𝑥𝑘+1 =((‫ݕ‬+k+1)−ܾ)/݉ [For 𝑘 + 1𝑡ℎ intermediate point]
Subtract 𝑥𝑘 from 𝑥𝑘+1
• 𝑥𝑘+1 − 𝑥𝑘 ={((‫ݕ‬+k+1)−ܾ)/݉} − {((‫ݕ‬+k)−ܾ)/݉}
• 𝑥𝑘+1 = 𝑥𝑘 + 1/𝑚
Similarly
• for ∆x = -1: we obtain 𝑦𝑘+1 = 𝑦𝑘 − 𝑚
• for ∆y = -1: we obtain 𝑥𝑘+1 = 𝑥𝑘 − 1/𝑚
Procedure for DDA line algorithm.
Void lineDDA (int xa, int ya, int xb, int yb)
{
int dx = xb – xa, dy = yb – ya, steps, k;
float xincrement, yincrement, x = xa, y = ya;
if (abs(dx)>abs(dy))
{
Steps = abs (dx);
}
else
{
Steps = abs (dy);
}
xincrement = dx/(float) steps;
yincrement = dy/(float) steps;

setpixel (ROUND (x), ROUND (y));


for(k=0;k<steps;k++)
{
x += xincrement;
y += yincrement;
setpixel (ROUND (x), ROUND (y));
}
}
(16,15)

(10,10)
Use DDA to draw line from (10,10) to (13,14)

LR means X +ve


Iteration (x,y) (x + ∆x, y + ∆y) Plot(x,y)
|∆x| = 3 < |∆y| = 4 means |m| > 1;
1 (10,10)
∆x = 1/m, ∆y = 1
m = 4/3 =1.33 so 2 (10,10) (10.75,11) (11,11)
∆x = 1/1.33 = 0.75 3 (10.75,11) (11.5,12) (12,12)
x and y both increasing so slope is +ve. 4 (11.5,12) (12.25,13) (12,13)
No change
5 (12.25,13) (13,14) (13,14)
Example DDA Algorithm
4
• Example: Draw line 𝐴𝐵 with coordinates 𝐴(2,2), 𝐵(6,4).
3
4−2 2 1 2
• 𝑚= = =
6−2 4 2 1
• 𝑚 is “+ve” and less than 1 so Δ𝑥 = 1. 0
• 𝑥0 = 2, 𝑦0 = 2 0 1 2 3 4 5 6 7 8
[Plot the initial point as given]
• 𝑥1 = 3, 𝑦1 = 𝑦0 + 𝑚 = 2 + 0.5 = 2.5
[Plot the first intermediate point by rounding it to (3, 3)]
• 𝑥2 = 4, 𝑦2 = 𝑦1 + 𝑚 = 2.5 + 0.5 = 3
[Plot the second intermediate point by rounding it to (4, 3)]
• 𝑥3 = 5, 𝑦3 = 𝑦2 + 𝑚 = 3 + 0.5 = 3.5
[Plot the third intermediate point by rounding it to (5, 4)]
• 𝑥4 = 6, 𝑦4 = 4
[Plot End point given]
Draw line for mentioned coordinates using DDA
i) (10,10) to (15,18)
ii) (10,10) to (18,15)
iii) (18,18) to (10,13)
iv) (18,18) to (13,10)

1/28/2025 Associate Professor Sujan Shrestha


Which algorithm is used for line drawing in computer
graphics?
A) Dijkstra's Algorithm
B) Digital Differential Analyzer
C) Newton-Raphson Method
D) Merge Sort
Answer: B) Digital Differential Analyzer
Computer Graphics

Bresenham’s Line
Drawing
algorithms
Associate Professor
9801104103
[email protected]

1/28/2025 Associate Professor Sujan Shrestha


Introduction to Bresenham’s Line
Algorithm
• An accurate and efficient raster line-generating
algorithm, developed by Bresenham.
• It scan converts line using only incremental integer
calculations.
• That can be modified to display circles and other
curves.
• Based on slop we take unit step in one direction and
decide pixel of other direction from two candidate
pixel.
• If |Δ𝑥| > |Δ𝑦| we sample at unit 𝒙 interval and vice
versa.
Line Path & Candidate pixel
• Example |ΔX| > |ΔY|, and ΔY is “+ve”. 4
3
Initial point (Xk, Yk) 2
Line path 1
Candidate pixels {(Xk+1, Yk), (Xk+1, Yk+1)} 0
0 1 2 3 4

 Now we need to decide which candidate pixel is more closer to


actual line.
 For that we use decision parameter (Pk) equation.
 Decision parameter can be derived by calculating distance of actual
line from two candidate pixel.
Derivation Bresenham’s Line Algorithm
• 𝑦 = 𝑚𝑥 + 𝑏 [Line Equation] 4
• 𝑦 = 𝑚(𝑥𝑘 ) + 𝑏 [Actual Y value at Xk 3
position] 2
1
• 𝑦 = 𝑚(𝑥𝑘 + 1) + 𝑏 [Actual Y value at Xk 0
+1position]
0 1 2 3 4
Distance between actual line position and lower
candidate pixel
• 𝑑1 = 𝑦 − 𝑦𝑘
• 𝑑1 = 𝑚(𝑥𝑘 + 1) + 𝑏 − 𝑦𝑘
Distance between actual line position and upper
candidate pixel
• 𝑑2 = (𝑦𝑘 +1) − 𝑦
• 𝑑2 = (𝑦𝑘 +1) − 𝑚 𝑥𝑘 + 1 − 𝑏
Contd.
Calculate 𝑑1 − 𝑑2
• 𝑑1 − 𝑑2 = 𝑚 𝑥𝑘 + 1 + 𝑏 − 𝑦𝑘 − (𝑦𝑘 +1 − 𝑚 𝑥𝑘 + 1 − 𝑏}
• 𝑑1 − 𝑑2 = 𝑚𝑥𝑘 + 𝑚 + 𝑏 − 𝑦𝑘 − {𝑦𝑘 + 1 − 𝑚𝑥𝑘 − 𝑚 − 𝑏}
• 𝑑1 − 𝑑2 = 2𝑚 𝑥𝑘 + 1 − 2𝑦𝑘 + 2𝑏 − 1
• 𝑑1 − 𝑑2 = 2(∆𝑦/∆𝑥) 𝑥𝑘 + 1 − 2𝑦𝑘 + 2𝑏 − 1 [Put 𝑚 = Δ𝑦/Δ𝑥]
Decision parameter
• 𝑝𝑘 = ∆𝑥(𝑑1 − 𝑑2 )
• 𝑝𝑘 = ∆𝑥 {2(∆𝑦/∆𝑥) 𝑥𝑘 + 1 − 2𝑦𝑘 + 2𝑏 − 1 }
• 𝑝𝑘 = 2∆𝑦𝑥𝑘 − 2∆𝑥𝑦𝑘 + 2∆𝑦 + 2∆𝑥𝑏 − ∆x
• 𝑝𝑘 = 2∆𝑦𝑥𝑘 − 2∆𝑥𝑦𝑘 + C [Replacing single constant C for simplicity]
Similarly
• 𝑝𝑘+1 = 2∆𝑦𝑥𝑘+1 − 2∆𝑥𝑦𝑘+1 + C
Contd.
Subtract 𝑝𝑘 from 𝑝𝑘+1
• 𝑝𝑘+1 − 𝑝𝑘 = 2∆𝑦𝑥𝑘+1 − 2∆𝑥𝑦𝑘+1 + C − 2∆𝑦𝑥𝑘 + 2∆𝑥𝑦𝑘 − C
• 𝑝𝑘+1 − 𝑝𝑘 = 2∆𝑦(𝑥𝑘+1 −𝑥𝑘 ) − 2∆𝑥(𝑦𝑘+1 −𝑦𝑘 )
[where (𝑥𝑘+1 −𝑥𝑘 ) = 1]
• 𝑝𝑘+1 = 𝑝𝑘 + 2∆𝑦 − 2∆𝑥(𝑦𝑘+1 −𝑦𝑘 )
[where (𝑦𝑘+1 −𝑦𝑘 ) = 0 or 1 depending on selection of previous pixel]

• If 𝑝𝑘 < 0, the next point to plot is (𝑥𝑘 + 1, 𝑦𝑘 ) and


𝑥𝑘+1 = 𝑥𝑘 + 1,
𝑦𝑘+1 = 𝑦𝑘
𝑝𝑘+1 = 𝑝𝑘 + 2∆𝑦

Otherwise, the next point to plot is (𝑥𝑘 + 1, 𝑦𝑘 + 1) and


𝑥𝑘+1 = 𝑥𝑘 + 1,
𝑦𝑘+1 = 𝑦𝑘 + 1
𝑝𝑘+1 = 𝑝𝑘 + 2∆𝑦 − 2∆𝑥
Initial Decision parameter
• The first decision parameter 𝑝0 is calculated using equation of 𝑝𝑘.
• 𝑝𝑘 = 2∆𝑦𝑥𝑘 − 2∆𝑥𝑦𝑘 + 2∆𝑦 + 2∆𝑥𝑏 − ∆x
• 𝑝0 = 2∆𝑦𝑥0 − 2∆𝑥𝑦0 + 2∆𝑦 + 2∆𝑥𝑏 − ∆x [Put 𝑘 = 0]
• 𝑝0 = 2∆𝑦𝑥0 − 2∆𝑥𝑦0 + 2∆𝑦 + 2∆𝑥 𝑦0 − 𝑚𝑥0 − ∆x
[Substitute 𝑏 = 𝑦0 – 𝑚𝑥0]
• 𝑝0 = 2∆𝑦𝑥0 − 2∆𝑥𝑦0 + 2∆𝑦 + 2∆𝑥(𝑦0 − (∆𝑦/∆𝑥)𝑥0 ) − ∆x
[Substitute 𝑚 = ∆𝑦/Δ𝑥]
• 𝑝0 = 2∆𝑦𝑥0 − 2∆𝑥𝑦0 + 2∆𝑦 + 2∆𝑥𝑦0 − 2∆𝑦𝑥0 − ∆x
• 𝑝0 = 2∆𝑦 − ∆x
[Initial decision parameter with all terms are constant]
Bresenham’s Line Algorithm
1. Input the two line endpoints and store the left endpoint in
(𝑥0 , 𝑦0 ).
2. Load (𝑥0 , 𝑦0 ) into the frame buffer; that is, plot the first point.
3. Calculate constants ∆𝑥, ∆𝑦, 2∆𝑦, and 2∆𝑦 − 2∆𝑥, and obtain
the starting value for the decision parameter as
𝑝0 = 2∆𝑦 − ∆𝑥
4. At each 𝑥𝑘 along the line, starting at 𝑘 = 0, perform the
following test:
If 𝑝𝑘 < 0, the next point to plot is (𝑥𝑘 + 1, 𝑦𝑘 ) and
𝑝𝑘+1 = 𝑝𝑘 + 2∆𝑦
Otherwise, the next point to plot is (𝑥𝑘 + 1, 𝑦𝑘 + 1) and
𝑝𝑘+1 = 𝑝𝑘 + 2∆𝑦 − 2∆𝑥
5. Repeat step-4 ∆𝑥 times.
Description of Bresenham’s Line
Algorithm
• Bresenham’s algorithm is generalized to lines with arbitrary slope.
• For lines with positive slope greater than 1 we interchange the
roles of the 𝑥 and 𝑦 directions.
• Also we can revise algorithm to draw line from right endpoint to
left endpoint, both 𝑥 and 𝑦 decrease as we step from right to left.
• When 𝑝𝑘 = 0 we can choose either lower or upper pixel but same
for whole line.
• For the negative slope the procedure are similar except that now
one coordinate decreases as the other increases.
• The special case handle separately by loading directly into the
frame buffer without processing.
 Horizontal line (∆𝑦 = 0),
 Vertical line (∆𝑥 = 0)
 Diagonal line with |∆𝑥| = |∆𝑦|
Example Bresenham’s Line
Algorithm
• Example: Draw line 𝐴𝐵 with coordinates 𝐴(2,2), 𝐵(6,4). 4
• Plot left end point 𝐴(2, 2). 3
• Calculate: 2
• ∆𝑥 = 4, 1
• ∆𝑦 = 2,
• 2∆𝑦 = 4,
0
• 2∆𝑦 − 2∆𝑥 = −4,
0 1 2 3 4 5 6 7 8
• 𝑝0 = 2∆𝑦 − ∆𝑥 = 0
• Now 𝑝0 ≮ 0 so we select upper pixel (3, 3).
• 𝑝1 = 𝑝0 + 2∆𝑦 − 2∆𝑥 = 0 + −4 = −4
• Now 𝑝1 < 0 so we select lower pixel (4, 3).
• 𝑝2 = 𝑝1 + 2∆𝑦 = −4 + 4 = 0
• Now 𝑝2 ≮ 0 so we select upper pixel (5, 4).
• 𝑝3 = 𝑝2 + 2∆𝑦 − 2∆𝑥 = 0 + −4 = −4
• Now 𝑝3 < 0 so we select lower pixel (6, 4).
(20,20) to (30,13)

Draw line for mentioned coordinates using BLA


i) (10,10) to (15,18)
ii) (10,10) to (18,15)
iii) (18,18) to (10,13)
iv) (18,18) to (13,10)

1/28/2025 Associate Professor Sujan Shrestha


• Step 1. Start Step 8. if ∆x>∆y(i.e. |m|<1)
• Step 2. Declare variables x1,y1,x2,y2, calculate pk = 2∆y-∆x
for(k=0; k< ∆x; k++)
lx,ly,∆x, ∆y, pk, pk if (pk<0)
• Step 3. Read values of x1, y1,x2,y2 xk = xk+l
• Step 4. Calculate ∆x = absolute(x2- yk = yk
x1 ) pk = pk+2∆y
else
∆y = absolute(y2-y1) xk = xk+l
• Step 5. if (x2>x1) yk = yk+l
assign lx = 1 pk = pk+2∆y-2∆x
plot(xk, yk)
else else
assign lx = -1 calculate pk = 2∆x-∆y
for(k=0; k< ∆x; k++)
• Step 6. if (y2-y1)
if(pk<0)
assign ly = 1 xk = xk
else yk = yk+l
pk = pk+2∆x
assign ly = -1
else
• Step 7. Plot (x1, y1) xk = xk+l
yk = yk+l
pk = pk+2∆x2∆y
BLA Algorithm Step 9. Stop
Plot(xk,yk)
1/28/2025 Associate Professor Sujan Shrestha

You might also like