Line Drawing Algorithms
2D Graphics Pipeline
Clipping
Object window to
Object Applying
subset viewport
World Coordinates world window
mapping
Simple 2D Drawing Pipeline
Object
Display Rasterization
Screen coordinates
Four Major Tasks
• Four major task for rendering geometric objects
– modeling
– geometric processing Modeling
• transformations
• clipping Geometry
• shading
• hidden-surface removal
Rasterize
• projection
– Rasterization (Scan Conversion)
Display
– display
Computer Graphics 7
Scan Conversion
• Rasterization or Scan Conversion is defined as the
process of representing continuous graphic object as a
collection of discrete pixels.
– Various graphic objects are
• Point
• Line
• Rectangle, Square
• Circle, Ellipse
• Sector, Arc
• Polygons……
– Hardware, Software, Firmware Implementation
February 12, 2025 Computer Graphics 8
Scan Conversion
• Scan Conversion is required in order to convert vector
data to Raster format for a scan line display device
– convert each graphic object to a set of regular pixels.
– determine inside/outside areas when filling polygons.
– scan-convert curves
February 12, 2025 Computer Graphics 9
Rasterization (Scan Conversion)
Convert high-level geometry description to
pixel colors in the frame buffer
Example: given vertex x,y coordinates
determine pixel colors to draw line
Two ways to create an image:
Scan existing photograph
Procedurally compute values (rendering)
Viewport Rasterization
Transformation
Rasterization
A fundamental computer graphics function
Determine the pixels’ colors, illuminations,
textures, etc.
Implemented by graphics hardware
Rasterization algorithms
– Lines
– Circles
– Triangles
– Polygons
Rasterization Operations
Drawing lines on the screen
Manipulating pixel maps (pixmaps):
copying, scaling, rotating, etc
Compositing images, defining and
modifying regions
Drawing and filling polygons
Previously glBegin(GL_POLYGON), etc
Aliasing and antialiasing methods
Scan Conversion
• The scan conversion process is interleaved with other
processes to deliver and improve the final image, some of
which are not entirely restricted to the discretization of vectors
but are involved in generally determining the colour of each
pixel in the raster. e.g:
– shading & illumination,
– hidden surface removal
– texture mapping
– depth testing
February 12, 2025 Computer Graphics 13
Scan Conversion Algorithms
1. Scan Conversion of Point
2. Scan Conversion of Line
3. Scan Conversion of Circle
4. Scan Conversion of Ellipse
5. Scan Conversion of Polygons
February 12, 2025 Computer Graphics 14
Scan Converting a Point
A pixel (10,
Y 6)
X
A point (-12.3,
10.3, 0)
Z
Y
MODELLING CO-ORDINATES SCREEN COORDINATES
• Mathematically vectors are defined in • a.k.a device co-ordinates, pixel co-ordinates
an infinite, “real-number” Cartesian co- • On display hardware we deal with finite,
ordinate system discrete coordinates
• X, Y values in positive integers
• 0,0 is measured from top-left usually with +Y
pointing down
February 12, 2025 Computer Graphics 15
Scan Converting a Point
• Each pixel on graphic display does not represent a
mathematical point like P(2.6,3.33). But it can be
accommodated to the nearest position by applying few
mathematical functions such as
– CEIL P (3,4)
– FLOOR P (2,3)
– GREATEST INTEGER FUNCTION P (3,3)
– ROUND P (3,3)
February 12, 2025 Computer Graphics 16
Scan Conversion Algorithms
1. Scan Conversion of Point
2. Scan Conversion of Line
3. Scan Conversion of Circle
4. Scan Conversion of Ellipse
February 12, 2025 Computer Graphics 17
How does computer draw line?
Screen made of pixels
High-level language specifies line
System must color pixels
Towards the ideal Line
We can only do a discrete approximation
Illuminate pixels as close to the true path
as possible, consider bi-level display only
Pixels are either lit or not lit
19
What is an ideal line?
Must appear straight and continuous
Only possible axis-aligned and 45o lines
Must interpolate both defining end
points
Must have uniform density and intensity
Consistent within a line and over all lines
What about antialiasing?
Must be efficient, drawn quickly
Lots of them are required!!!
20
Line Drawing Algorithm
Programmer specifies (x,y) values of end pixels
Need algorithm to figure out which intermediate
pixels are on line path
Pixel (x,y) values constrained to integer values
Actual computed intermediate line values may be
floats
Rounding may be required. E.g. computed point
(10.48, 20.51) rounded to (10, 21)
Rounded pixel value is off actual line path (jaggy!!)
Sloped lines end up having jaggies
Vertical, horizontal lines, no jaggies
Simple Line
Based on slope-intercept algorithm from
algebra:
y = mx + b ( (also Ax + By + C = 0)
Simple approach:
increment x, solve for y
Floating point arithmetic required
22
The Problem of Scan Conversion
A line segment in a scene is defined by
the coordinate positions of the line end-
points y
(7, 5)
(2, 2)
x
The Problem of Scan Conversion
But what happens when we try to draw
this on a pixel based display?
How do we choose which pixels to turn on?
Considerations
Considerations to keep in mind:
The line has to look good
Avoid jaggies
It has to be lightening fast!
How many lines need to be drawn in a typical
scene?
This is going to come back to bite us again and
again
Line Equations
Let’s quickly review the equations
involved in drawing lines
y
Consider 2 end points (X0,Y0) and (X end, Y end)
Slope-intercept line
yend equation:
y =m ⋅ x +b
y0 where:
y end − y 0
m=
x0 xend
x
x end − x 0
b= y 0 − m⋅ x 0
For any given x intervals dx along a line,
we can compute the corresponding y if
interval dY from eqn
dy=m dx
Similarly, we can obtain the x interval dx
corresponding to a specified dy as
dx = dy/m
A Very Simple Solution (cont…)
Slant Slope Case IF M>1
Sharp Slope Case
IF M<1
7 7
6 6
5 5
4 4
3 3
2 2
1 1
0 0
Increment of x or y depends on slope
Sharp Slope Case:
If m>1, increment y and find x
dy can be set propos ional to a small vertical deflection
voltage and the corresponding horizontal deflection
voltage set propos ional to dx as calculated from eqn.
Slant Slope Case:
If m<1, increment x and find y
dx can be set proposional to a small horizontal deflection
voltage and the corresponding vertical deflection voltage
set propos ional to dy as calculated from eqn
If m=1, dx=dy and horizontal and vertical deflections
voltages are equal.
Lines & Slopes
The slope of a line (m) is defined by its
start and end coordinates
The diagram below shows some
examples of lines and their slopes
m=∞
m = -4 m=4
m = -2 m=2
m = -1 m=1
m = -1/2 m = 1/2
m = -1/3 m = 1/3
m=0 m=0
A Very Simple Solution
We could simply work out the
corresponding y coordinate for each unit x
coordinate
Let’s consider
y the following example:
(7, 5)
5
2
(2, 2)
x
2 7
A Very Simple Solution (cont…)
y
(7, 5)
LINE EQUATION : y= mx+b
5
First work out m and b:
5−2 3
m= =
2
7−2 5
(2, 2)
3 4
b=2 − ∗2=
2 3 4 5 6 7
x 5 5
Now for each x value work out the y value:
3 4 3 3 4 1
y ( 3 ) = ⋅3 + =2 y ( 4 ) = ⋅ 4+ =3
5 5 5 5 5 5
3 4 4 3 4 2
y ( 5 ) = ⋅5+ =3 y ( 6 )= ⋅ 6+ =4
5 5 5 5 5 5
A Very Simple Solution (cont…)
Now just round off the results and turn on
these pixels to draw our line
3
7 y ( 3 ) =2 ≈ 3
6 5
5 1
y ( 4 ) =3 ≈ 3
4 5
3
4
2 y ( 5 ) =3 ≈ 4
1 5
0 2
y ( 6 )=4 ≈ 4
0 1 2 3 4 5 6 7 8
5
A Very Simple Solution (cont…)
However, this approach is just way too
slow
In particular look out for:
The equation y = mx + b requires the
multiplication of m by x
Rounding off the resulting y coordinates
We need a faster solution
Issues with Line Drawing
Line drawing is such a fundamental algorithm that it must be
done fast
Use of floating point calculations does not facilitate speed
Furthermore, lines must be drawn without gaps
Gaps look bad
If you try to fill a polygon made of lines with gaps the fill will
leak out into other portions of the display
Eliminating gaps through direct implementation of any of
the standard line equations is difficult
Finally, we don’t want to draw individual points more than once
That’s wasting valuable time
DDA Algorithm
DDA = Digital Differential Analyser
Finite differences
Start with starting and ending coordinates of the line:
(x0, y0) and (x1, y1)
Color first pixel (round to nearest integer)
Suppose x1-x0 > y1-y0 (gentle slope)
There will be x1-x0 steps (# pixels to be colored)
Set x=x0, y=y0
At each step,
increment x by (x1-x0)/numsteps, and
increment y by (y1-y0)/numsteps
For each step, round off x and y to nearest integer, and
color pixel
DDA Pseudo-code
// assume that slope is gentle
DDA(float x0, float x1, float y0, float y1) {
float x, y; Q: For each step, how many floating
int dx = x1-x0, dy = y1-y0; point operations are there?
int numsteps = max(abs(dx),abs(dy));
A: 2
float xinc, yinc;
Q: For each step, how many integer
xinc = dx / numsteps; operations are there?
yinc = dy / numsteps; A: 2
x = x0;
y = y0;
ColorPixel(Round(x),Round(y));
for (int i=0; i<numsteps; i++) {
x += xinc;
y += yinc;
ColorPixel(Round(x),Round(y));
}
}
DDA Example
dx = 12 – 2 = 10, dy = 8-3 =5
numsteps = max(abs(dx),abs(dy)=max(10,5)=10
Suppose we want to xinc = dx/numsteps = 10/10 = 1.0
yinc = dy/numsteps = 5/10 = 0.5
draw a line starting at t x y R(x) R(y)
pixel (2,3) and ending 0 2 3 2 3
at pixel (12,8). 1 3 3.5 3 4
What are the values 2 4 4 4 4
of the variables x and 3 5 4.5 5 5
y at each timestep? 4 6 5 6 5
What are the pixels 5 7 5.5 7 6
colored, according to 6 8 6 8 6
the DDA algorithm? 7 9 6.5 9 7
8 10 7 10 7
9 11 7.5 11 8
10 12 8 12 8
DDA Example
dx = 5 – 1 = 4, dy = 5-2 =3
numsteps = max(abs(dx),abs(dy)=max(4,3)=4
Suppose we want to xinc = dx/numsteps = 4/4 = 1.0
yinc = dy/numsteps = 3/4 = 0.75
draw a line starting at t x=x+xinc y=y+yinc R(x) R(y)
pixel (1,2) and ending 0 1 2 1 2
at pixel (5,5). 1 2 2.75 2 3
What are the values 2 3 3.5 3 4
3 4 4.25 4 4
of the variables x and 4 5 5 5 5
y at each timestep?
What are the pixels
colored, according to
the DDA algorithm?
10
P(x) P(y) 9
2 3
8
3 4
4 4 7
5 5
6
6 5
7 6 5
8 6
4
9 7 3
10 7
2
11 8
12 8 1
0 0 1 2 3 4 5 6 7 8 9 10 11 12 13
DDA Algorithm (continued)
Y_inc
X_inc
… but floating point operations and
rounding operations are expensive
DDA Algorithm (continued)
Still need a lot of floating point arithmetic.
2 ‘round’s and 2 adds per pixel.
Is there a simpler way ?
Can we use only integer arithmetic ?
Easier to implement in hardware.
Advantages of DDA
It calculates the pixel positions faster than
the calculations performed by using the
equation y=mx +b.
Multiplication is eliminated as the x and y
increments are used to determine the
position of the next pixel on a line
Disadvantages of DDA
The rounding and floating point operations
are time consuming.
The round-off error which results in each
successive addition leads to the drift in
pixel position, already calculated
The DDA Algorithm Summary
The DDA algorithm is much faster than
our previous attempt
In particular, there are no longer any
multiplications involved
However, there are still two big issues:
Accumulation of round-off errors can make
the pixelated line drift away from what was
intended
The rounding operations and floating point
arithmetic involved are time consuming
Conclusion
Drawing lines to pixel based displays is
time consuming so we need good ways to
do it
The DDA algorithm is pretty good – but we
can do better
Next time we’ll like at the Bresenham’s
line algorithm and how to draw circles, fill
polygons and anti-aliasing
Bresenham’s Line Drawing
Algorithm
Jack Bresenham addressed these issues with the
Bresenham Line Scan Convert algorithm
This was back in 1965 in the days of pen-plotters
All simple integer calculations
Addition, subtraction, multiplication by 2 (shifts)
Guarantees connected (gap-less) lines where each
point is drawn exactly 1 time
Also known as the midpoint line algorithm
Bresenham’s Algorithm
Uses only integer calculations
Uses distance between ideal y-coordinate
and the upper and lower pixel (assuming
gentle slope)
dupper
dlower
Bresenham’s Line Algorithm
What it comes down to is computing how close the
midpoint (between the two grid points) is to the
actual line
(xp, yp) (xp+1, yp+½)
Bresenham’s Line Algorithm
Define F(m) = F(Mx, My) = d as the discriminant
(derive from the equation of a line Ax + By + C)
(
F ( m )=d m=F x p +1 , y p +
1
2 )
(
F ( m )=d m = A ⋅ ( x p +1 ) + B ⋅ y p +
1
2
+C)
if (dm > 0) choose the upper point (pixel), otherwise
if (dm <0), i.e, negative then choose the lower point (pixel)
Decision parameter dm is referred in book as Pk
w
Bresenham’s Line-Drawing
Algorithm
What Pixels to turn on or off?
(x1,y1)
Consider pixel midpoint M(Mx, My)
M = (x0 + 1, Y0 + ½)
If Pk < 0, M lies above line,
M(Mx,My) shade lower pixel (E pixel)
If Pk > 0, M lies below line,
shade upper pixel(same y as before),
Also caled NE pixel
…
(x0, y0)
General idea how Bresenham
works
Suppose that the line is gently sloping upwards from left
to right.
Start by coloring the left-most pixel.
Then, for the next column (that is, for each x value), we
have to figure out whether we color the same y or y+1.
How do we decide?
When going from one column to the next, add an error value. If
the error value is more than 0.5, we should color y+1 and reset
the error value. Otherwise, color y and accumulate the error
value.
However, it seems like we’re still using floating point
Solution, multiply both sides by 2 so that we use integer
comparisons instead.
Bresenham’s Algorithm
1. Input the two line endpoints and store left endpoint as (x0,y0)
2. Pre-calculate the values dx, dy, 2dy and 2dy - 2dx
3. Color pixel (x0,y0)
4. Let p0 = 2dy – dx
5. At each xk along the line, starting with k=0:
If pk<0, then the next point to plot is (xk + 1,yk),
and pk+1 = pk + 2dy
Otherwise, the next point to plot is (xk + 1, yk + 1),
and pk+1 = pk + 2dy – 2dx
6. Repeat Step-4 dx times
Switch Point 0 and Point 1 if necessary
If negative slope, reflect
If steep slope, flip y and x
Number of Operations in
Bresenham’s Algorithm
Q: In each step, how many floating point operations are there?
A: 0
Q: In each step, how many integer operations are there?
A: 3 or 4
Bresenham’s Algorithm Example
dx = 12 – 2 = 10 2dy = 10
dy = 8 – 3 = 5 2dy – 2dx = -10
Suppose we want to draw p0 = 2dy – dx = 10-10= 0
a line starting at pixel If Pk<0, pk+1 = pk + 2dy else pk+1 = pk + 2dy-2dx
(2,3) and ending at pixel t p P(x) P(y)
(12,8). 0 0 2 3
What are the values of 1 -10 3 4
p0, dx and dy? 2 0 4 4
What are the values of 3 -10 5 5
the variable p at each 4 0 6 5
timestep? 5 -10 7 6
What are the pixels 6 0 8 6
colored, according to 7 -10 9 7
Bresenham’s algorithm?
8 0 10 7
9 -10 11 8
10 0 12 8
10
P(x) P(y) 9
2 3
8
3 4
4 4 7
5 5
6
6 5
7 6 5
8 6
4
9 7 3
10 7
2
11 8
12 8 1
0 0 1 2 3 4 5 6 7 8 9 10 11 12 13
Summary of mid-point algorithm
Choose between 2 pixels at each step
based upon the sign of a decision
variable.
Update the decision variable based
upon which pixel is chosen.
Start point is simply first endpoint (x1,y1).
Need to calculate the initial value for Pk
64
Summary of line drawing so far.
Explicit form of line
Inefficient, difficult to control.
Parametric form of line.
Express line in terms of parameter t
DDA algorithm
Implicit form of line
Only need to test for ‘side’ of line.
Bresenham algorithm.
Can also draw circles.
65
Problems with Bresenham algorithm
Pixels are drawn as a single line
unequal line intensity with change in angle.
Pixel density = 2.n pixels/mm
Can draw lines in darker
colours according to line
direction.
- Better solution : antialiasing !
- (eg. Gupta-Sproull algorithm)
Pixel density = n pixels/mm
66