Line Drawing Algorithms
CS-321
Dr. Mark L. Hornick
1
Line Drawing Algorithms
(x1, y1)
(x0, y0)
Which pixels should be set
to represent the line?
CS-321
Dr. Mark L. Hornick
2
Line Equations
(x1, y1)
(x0, y0)
y mx b
y1 y0
m
x1 x0
y y0 m x x0
y m x x0 y0
CS-321
Dr. Mark L. Hornick
3
Simple Algorithm
Start x at origin of line
Evaluate y value
Set nearest pixel (x,y)
Move x one pixel
toward other endpoint
Repeat until done
CS-321
Dr. Mark L. Hornick
y m x x0 y0
4
Implementation
// SimplePixelCalc - elementary pixel calculation using
// variation of y=mx+b
void SimplePixelCalc( int x0, int xn, int y0, int yn )
{
for( int ix=x0; ix<=xn; ix++ )
{
float fy = y0 + float(yn-y0)/float(xn-x0)*ix;
// round to nearest whole integer
int iy = (int)(fy+0.5);
SetPixel(ix, iy, 1); // write pixel value
}
}
How well
does this
work?
How does this work?
CS-321
Dr. Mark L. Hornick
5
Use the lab1 program on the
following coordinates
Example 1
Example 2
(x0, y0) = (0,1)
(x1, y1) = (10,4)
(x0, y0) = (0,8)
(x1, y1) = (9,0)
Example 3
(x0, y0) = (0,1)
(x1, y1) = (2,7)
CS-321
Dr. Mark L. Hornick
6
m=0.3
Example 1 Results?
8
7
6
5
4
3
2
1
0
0
1
2
3
4
5
CS-321
Dr. Mark L. Hornick
6
7
8
9 10 11
7
m=-0.89
Example 2 Results?
8
7
6
5
4
3
2
1
0
0
1
2
3
4
5
CS-321
Dr. Mark L. Hornick
6
7
8
9 10 11
8
m=3
Example 3 Results?
8
7
6
5
4
3
2
1
0
0
1
2
3
4
5
CS-321
Dr. Mark L. Hornick
6
7
8
9 10 11
9
Simple Algorithm
Adjustment for m
If |m|<1
y m x x0 y0
If |m|>1
x (1/ m) y y0 x0
If |m|=1
x=y
CS-321
Dr. Mark L. Hornick
10
Simple Algorithm Summary
Evaluate y = f(x) at each x position
Requires floating multiplication for each
evaluation of y
11
Digital Differential Analyzer
(DDA) Algorithm
Step through either x or y based on slope
Build the line parametrically
If |m|<1
xk+1 = xk + 1
yk+1 = yk + m
If |m|>1
xk+1 = xk + 1/m
yk+1 = yk + 1
If |m|=1
xk+1 = xk + 1
yk+1 = yk + 1
CS-321
Dr. Mark L. Hornick
12
DDA Implementation
void dda( int x0, int xn, int y0, int yn )
{
float dx = float(xn - x0); // total span in x
float dy = float(yn - y0); // total span in y
float y = float(y0);
float x = float(x0);
float Dx, Dy; // incremental steps in x & y
// determine if slope m GT or LT 1
if( dx > dy )
{
Dx = 1;
Dy = dy/dx;// floating division, but only done once per line
}
else
{
Dx = dx/dy;
Dy = 1;
}
int ix, iy; // pixel coords
for( int k=0; k<=((dx>dy)? dx:dy); k++ )
{
ix = int(x + 0.5); // round to nearest pixel coordinate
iy = int(y + 0.5);
x += Dx; // floating point calculations
y += Dy;
}
}
CS-321
Dr. Mark L. Hornick
13
DDA Results?
8
7
6
5
4
3
2
1
0
0
1
2
3
4
5
CS-321
Dr. Mark L. Hornick
6
7
8
9 10 11
14
DDA Algorithm Highlights
Reduction in strength
Replaces multiplication with addition
Still some limitations
Round-off error accumulates
Floating point addition (e.g. not on PPC)
CS-321
Dr. Mark L. Hornick
15
Line-Drawing Algorithms
Simple algorithm
DDA algorithm
Evaluate y = f(x) at each x position
Floating multiplication
Floating addition only
Can we do better?
Tomorrow: Bresenham’s algorithm!
16
© Copyright 2026 Paperzz