COMPUTER GRAPHICS
1
/39
Scan Conversion
Scan Conversion
Computer Graphics
COMPUTER GRAPHICS
2
/39
Scan Conversion
OpenGL Pipeline
Model
Transform
View
Transform
Perspective
Transform
Clipping
Perspective
Division
Viewport
Transform
Scan
Conversion
v
1
v2
v3
COMPUTER GRAPHICS
3
/39
Scan Conversion
Determines individual pixel values
Scan Conversion /
Rasterization
COMPUTER GRAPHICS
4
/39
Scan Conversion
Line Drawing
Draw a line on a raster screen between two points
What’s wrong with this statement of problem?
Doesn’t say anything about which points are allowed as
end points
Doesn’t give a clear meaning of “draw”
Doesn’t say what constitutes a “line” in raster form
Doesn’t say how to measure success of proposed
algorithm
Scan Converting Lines
COMPUTER GRAPHICS
5
/39
Scan Conversion
Line Drawing
Problem Statement:
Given two points
P
and
Q
in
XY
plane, both with integer
coordinates, determine which pixels on raster screen
should be on in order to make picture of a unit width line
segment starting at
P
and ending at
Q
Scan Converting Lines
COMPUTER GRAPHICS
6
/39
Scan Conversion
Special Cases:
Horizontal Line:
Draw pixel P and increment x coordinate value by 1 to get next pixel
Vertical Line:
Draw pixel P and increment y coordinate value by 1 to get next pixel
Diagonal Line:
Draw pixel P and increment both x and y coordinate value by 1 to get
next pixel
General Case
What should we do in a general case?
Increment x coordinate by 1 and choose point closest to line.
How do we measure “closest”?
Scan Converting Lines
COMPUTER GRAPHICS
7
/39
Scan Conversion
Why can we use vertical distance as a measure of
which point is closer?
Because vertical distance is proportional to actual distance
We can show this with similar triangles
The true distances to line (in blue) are directly proportional to
vertical distances to line (in black)
Therefore, point with smaller vertical distance to line is closest
Vertical Distance
COMPUTER GRAPHICS
8
/39
Scan Conversion
Strategy 1: Incremental Algorithm
COMPUTER GRAPHICS
9
/39
Scan Conversion
void line (
int
x0,
int
y0,
int
x1,
int
y1) {
int
x, y;
float
dy
= y1
–
y0;
float
dx = x1
–
x0;
float
m =
dy
/ dx; //careful, check dx == 0
y = y0;
for (x = x0; x < x1; ++x) {
WritePixel
(x, Round (y)); //Round is slow…
y = y + m;
}
}
Strategy 1
–
Pseudocode
COMPUTER GRAPHICS
10
/39
Scan Conversion
Questions?
COMPUTER GRAPHICS
11
/39
Scan Conversion
Problem:
Given endpoints (Ax, Ay) and (
Bx
, By) of a line, determine the
best sequence of intervening pixels to represent a line
First, make two simplifying assumptions:
(Ax <
Bx
)
that is, moving from left to write
(0 < m < 1)
that is, slope up and to the right at between (0
and 45 degrees)
We can remove both assumptions later…
Define
Width
W =
Bx
–
Ax
Height
H = By

Ay
Strategy 2
–
Brsenham’s
Algorithm
COMPUTER GRAPHICS
12
/39
Scan Conversion
Based on our assumptions:
W and H are positive
H < W
As x steps in the +1
increments, y can:
Stay the same
Increment by 1
The key is to figure out
which of these cases we
want
Use the “midpoint”
Strategy 2
–
Brsenham’s
Algorithm
Previous
pixel
E pixel
NE
pixel
COMPUTER GRAPHICS
13
/39
Scan Conversion
Strategy 2
–
Brsenham’s
Algorithm
Previous pixel
Choices for
current pixel
Choices for
next pixel
E pixel
NE pixel
Midpoint
M
Q
COMPUTER GRAPHICS
14
/39
Scan Conversion
Line passes between E and NE
Point that is closer to intersection point
must be chosen
Observe on which side of line midpoint
𝑀
lies:
E is closer to line if midpoint
𝑀
lies above line, i.e., line crosses bottom half
NE is closer to line if midpoint
𝑀
lies below line, i.e., line crosses top half
Error (vertical distance between chosen pixel and actual line) is always
≤
.
5
Strategy 2
–
Brsenham’s
Algorithm
E pixel
NE pixel
𝑀
Algorithm chooses NE as next pixel
for line shown
Now we need to find a way to
calculate on which side of the line
the midpoint is
COMPUTER GRAPHICS
15
/39
Scan Conversion
Need a good equation to
represent the line
The equation F(x, y) is doubled
to avoid floats later…
Note that when F(x, y) == 0,
then (x, y) is on the line
If (x, y) is not on the line, we get
a positive or negative value for F
We can think of this as a measure of
error (signed distance) to the line
Strategy 2
–
Brsenham’s
Algorithm
COMPUTER GRAPHICS
16
/39
Scan Conversion
So:
F(x, y) =

2W (y
–
Ay) + 2H (x
–
Ax)
Algorithm:
Loop over x values:
If F(x, y) < 0, then:
(x, y) is above the line, so shade lower pixel (the E pixel)
Keep y the same
If F(x, y) > 0, then:
(x, y) is below the line, so shade upper pixel (the NE pixel)
y
= y + 1
Strategy 2
–
Brsenham’s
Algorithm
COMPUTER GRAPHICS
17
/39
Scan Conversion
Bonus: we can compute F(x, y) incrementally:
Initial midpoint M = (Ax+1, Ay+0.5)
Plugging into F(
Mx
, My) =

2W(y

Ay) + 2H(x

Ax)
We get an initial F = 2H
–
W
The next midpoint will be:
If y stays the same: F(
Mx
, My) += 2H
If y = y+1: F(
Mx
, My) += 2(W
–
H)
Strategy 2
–
Brsenham’s
Algorithm
COMPUTER GRAPHICS
18
/39
Scan Conversion
v
oid
Bresenham
(
IntPoint
a,
IntPoint
b) {
//assume
a.x
<
b.x
, and 0 < H/W < 1
int
y =
a.y
;
int
W =
b.x
–
a.x
;
int
H =
b.y
–
a.y
;
int
F = 2*H
–
W;
//current error term
for (
int
x =
a.x
; x <=
b.x
; x++) {
WritePixel
(x, y);
if (F < 0) {
F += 2H; }
else {
y++;
F += 2(H
–
W); }
}
}
Brsenham’s
Algorithm
–
Psueocode
COMPUTER GRAPHICS
19
/39
Scan Conversion
We had set some restrictions earlier:
0 < m < 1
Ax <
Bx
These can be removed with some additional code:
For lines with Ax >
Bx
, swap the two points
For lines with m > 1, interchange x and y
For lines with m < 0, x++, y = y

1 rather than +1
Horizontal and vertical lines (test for
a.x
==
b.x
)
Brsenham’s
Algorithm
–
Assumptions
COMPUTER GRAPHICS
20
/39
Scan Conversion
Questions?
COMPUTER GRAPHICS
21
/39
Scan Conversion
Version 1:
Increment x
Find y value
WritePixel
(x, y)
Terrible!
Scan Converting Circles
(17, 0)
(0, 17)
COMPUTER GRAPHICS
22
/39
Scan Conversion
Version 1:
Increment x
Find y value
WritePixel
(x, y)
Terrible!
Version 2:
Use polar coordinates
For angle from (0 to 360)
Find x, y
WritePixel
(x, y)
Less bad…
Scan Converting Circles
(17, 0)
(0, 17)
(17, 0)
(0, 17)
COMPUTER GRAPHICS
23
/39
Scan Conversion
Symmetry:
If
(
x
0
+
a
,
y
0
+
b
) is on the circle,
So are
(
x
0
±
a
,
y
0
±
b
) and
(
x
0
±
b
,
y
0
±
a
)
Hence 8

way symmetry
Reduce the problem to
finding the pixels for 1/8 of
the circle
Version 3
–
Use Symmetry
R
(
x
0
+
a
,
y
0
+
b
)
(
x

x
0
)
2
+ (
y

y
0
)
2
=
R
2
(
x
0
,
y
0
)
COMPUTER GRAPHICS
24
/39
Scan Conversion
Scan top right 1/8 of the
circle with radius R
Circle starts at
(
x
0
,
y
0
+R
)
Let’s use another
incremental algorithm
with decision variable
evaluated at midpoint
Version 3
–
Using Symmetry
(
x
0
,
y
0
)
COMPUTER GRAPHICS
25
/39
Scan Conversion
x = x0;
y = y0 + R;
WritePixel
(x, y);
for (x=x0+1; (x

x0) > (y

y0); x++) {
if (
decision_variable
< 0
) {
//move east
update decision variable; }
else {
//move south east
update decision variable;
y

; }
WritePixel
(x, y);
}
Note, we can replace all occurrences of x0 and y0 with 0 (translate the
coordinate system by (

x0,

y0)
The Incremental Algorithm
–
A Sketch
COMPUTER GRAPHICS
26
/39
Scan Conversion
What we need:
Decision variable
Negative if we move E, positive if we move SE (or vice versa)
Follow the line strategy, use an equation
𝑓
,
=
2
+
2
−
2
=
0
𝑓
,
is zero on circle, negative inside, positive outside
If we are at pixel (x, y), we need to examine (x+1, y) and
(x+1, y+1)
Compute f at the midpoint
The Incremental
Algorithm
COMPUTER GRAPHICS
27
/39
Scan Conversion
Evaluate
𝑓
,
=
2
+
2
−
2
=
0
at the point (x+1, y
–
0.5)
We are asking: “is f(x+1, y

0.5)
positive or negative”?
Note that 0 means it is on the circle
If negative, midpoint is inside
the circle, then we choose E
If positive, midpoint is outside
of the circle, then we choose SE
The Decision Variable
COMPUTER GRAPHICS
28
/39
Scan Conversion
The details of the implementation of the
Brsenham
algorithm for circles can be found in the book.
Just note that because of the curvature on the circle,
the distance computation is a little trickier, especially if
we want to use the same incremental computation as
we did for lines.
There are additional optimizations that can make this
faster
What kind of guarantee do we have from using the
midpoint algorithm (for both line and circle)?
Version 3
–
Use Symmetry
COMPUTER GRAPHICS
29
/39
Scan Conversion
MEC
(
R
)
/* 1/8th of a circle w/ radius R */
{
int
x
=
0
,
y
=
R
;
int
delta_E
=
2
*
x
+
3
;
int
delta_SE
=
2
(
x

y
)
+
5
;
float
decision
=
(
x
+
1
)*(
x
+
1
)
+
(
y
+
0.5
)*(
y
+
0.5
)
–
R
*
R
;
Pixel
(
x
,
y
);
while
(
y
>
x
)
{
if
(
decision
>
0
)
{
/* Move east */
decision
+=
delta_E
;
delta_E
+=
2
;
delta_SE
+=
2
;
/*Update delta*/
}
else
/* Move SE */
{
y


;
decision
+=
delta_SE
;
delta_E
+=
2
;
delta_SE
+=
4
;
/*Update delta
*/
}
x
++;
Pixel
(
x
,
y
);
}
}
Midpoint Eighth Circle Algorithm
COMPUTER GRAPHICS
30
/39
Scan Conversion
Questions?
COMPUTER GRAPHICS
31
/39
Scan Conversion
Aligned Ellipses
Non

integer primitives
General conics
Patterned primitives
Other Scan Conversion
Problems
COMPUTER GRAPHICS
32
/39
Scan Conversion
Patterned line from
P
to
Q
is
not the same as patterned line
from
Q
to
P
:
Patterns can be geometric or
cosmetic
Cosmetic: texture applied after
transformations
Geometric: pattern subject to
transformations
Patterned Lines
COMPUTER GRAPHICS
33
/39
Scan Conversion
Geometric
vs
Cosmetic
+
Geometric
(Perspectivized/Filtered)
Cosmetic
(Real

World
Contact
Paper)
COMPUTER GRAPHICS
34
/39
Scan Conversion
Generic Polygons
COMPUTER GRAPHICS
35
/39
Scan Conversion
Region to be filled is a polygon P, described as a set
of vertices in raster (i.e. pixels), pi = (xi,
yi
), for
i
=1,
2, .., N. This sequence is sorted so that the vertices
are in a pre

determined order.
To fill P, we work through the frame buffer, one
scan line at a time, filling in the appropriate
portions of each line.
The proper portions are determined by finding the
intersections of the scan line with all edges of P.
Filling a Polygon
COMPUTER GRAPHICS
36
/39
Scan Conversion
f
oreach
(scan line L) {
Find intersections of L with all edges of P;
Sort the intersections by increasing x

value;
Fill pixel runs between all pairs of intersections;
}
Pseudocode
For Filling Polygon
COMPUTER GRAPHICS
37
/39
Scan Conversion
Additional details can be found in the text
(calculating intersections, edge lists, etc.)
Which pixels belong to a polygon?
By convention, left and bottom colored, top and
right are not.
Filling Polygons
–
Additional Details
COMPUTER GRAPHICS
38
/39
Scan Conversion
OpenGL Pipeline
Model
Transform
View
Transform
Perspective
Transform
Clipping
Perspective
Division
Viewport
Transform
Scan
Conversion
v
1
v2
v3
COMPUTER GRAPHICS
39
/39
Scan Conversion
Questions?
Comments 0
Log in to post a comment