Title: Graphics Lecture Note
1Part II Line Drawing
2Scan Conversion for Lines
- Lets assume 1-pixel-thick line.
- If slope 1, one pixel in a column.
- If slope 1, one pixel in a row.
- Lets take slope 1 case only.
- If slope 1, we can reverse the roles of x and
y. - Without loss of generality, we will focus on the
case 0
lines can be handled separately.
y
?
x
3A Brute Force Algorithm
LineDrawing1(x1,y1,x2,y2) // a line specified
by two integer end points m (y2 - y1) / (x2 -
x1) // slope m for the line equation B y1
- mx1 // compute the y-intercept B using
m for (xx1 x SetPixel(x, round(y))
(x2,y2)
ymxB
(x1,y1)
B
Example
(5,3)
(4,2.33)
(9,4)
3
y(1/3)x1
(3,2)
(4,2)
2
2
(5,2.66)
(3,2)
1
3
4
5
m and B computation
1st iteration
3rd iteration
2nd iteration
4A Brute Force Algorithm (contd)
- LineDrawing1 is inefficient because, for each
iteration, we need - ? floating-point multiplication
- ? floating-point addition, and
- ? round operation.
- LineDrawing1(x1,y1,x2,y2)
- m (y2 - y1) / (x2 - x1)
- B y1 - mx1
- for (xx1 x
- y mx B
- SetPixel(x, round(y))
-
- Lets try to drop ? multiplication.
? addition
? multiplication
? round operation
5An Improved Algorithm
- Note that, when x is increased by 1, y is
increased by m. So, we dont have to use the line
equation y mx B to update y value. - LineDrawing2(x1,y1,x2,y2)
- y y1
- m (y2 - y1) / (x2 - x1)
- for (xx1 x
- SetPixel(x, round(y))
- y y m
- We now need ? floating-point addition and ? round
operation. - Not bad, but any better algorithm?
- Yes, there is an integer-addition-only version.
Its Bresenhams algorithm.
6Bresenhams Line Drawing Algorithm
- In the process of rasterization from (x1,y1) to
(x2,y2), suppose that a pixel (xk, yk) is drawn. - Obviously, the x-position of the next pixel is
xk1. - 0either yk or yk1.
- Which one to choose? E(xk1, yk) or NE(xk1,
yk1)?
NE
E
xk
xk1
7Bresenhams Line Drawing Algorithm (contd)
- Obviously, which one to choose between E and NE
depends on which pixel is closer to the ideal
line. -
- If d1 d2, choose NE. Otherwise choose E. (If d1
d2, we simply choose E. Of course, we could
choose NE. It doesnt matter.) - Let the ideal lines y-coordinate at xk1 be y
m(xk1) B. Then, - In the sense that the sign of d1-2 determines
between E and NE, d1-2 is called the decision
variable.
NE
yk1
d2
(xk1, y)
d1
yk
E
(xk, yk)
d1 y yk m(xk1) B yk d2 yk1 - y
yk1 - m(xk1) - B d1-2 d1 - d2 2m(xk1) 2B
2yk 1
8Bresenhams Line Drawing Algorithm (contd)
- Lets replace the slope m by dy/dx(y2-y1)/(x2-x1)
. - Important is the sign of the decision variable
d1-2, not its value. So multiply d1-2 by dx( 0)
to make a new version of the variable D. - D and d1-2 play the same role!!!
- If D 0, choose NE.
- Otherwise, choose E.
- In D, all except xk and yk are constants. So,
rewrite D as follows - Observe that, after choosing the pixel (xk, yk),
we compute D for the next step by inserting (xk,
yk) into Ds formula.
d1-2 2m(xk1) 2B 2yk 1 2(dy/dx)(xk1)
2B 2yk 1
D 2dy(xk1) 2Bdx 2ykdx dx 2xkdy
2ykdx 2dy 2Bdx dx
If D 0, choose NE
(xk, yk)
Otherwise, choose E
D 2xkdy 2ykdx C, where C 2dy 2Bdx dx
9Bresenhams Line Drawing Algorithm (contd)
- Suppose that we chose a pixel (xk, yk) at x xk.
- For the next step x xk1, we should compute the
decision variable by inserting (xk, yk) into Ds
formula. Lets call it Dcurrent. - Dcurrent is used to determine between E and NE.
(In this example, just assume that E happens to
be chosen.) - After choosing between E and NE at x xk1, we
also have to compute the new decision variable
Dnew for the next step x xk2. - Recall that D is computed using the just-chosen
pixels coordinate. The value of Dnew depends on
whether we choose E or NE. - Importantly, Dnew can be incrementally computed
from Dcurrent.
Dcurrent
Dold
Dnew
(1) Choose a pixel using Dold computed at x
xk-1
(3) At x xk1, choose a pixel using Dcurrent
yk1
NE
(4) Compute Dnew for x xk2, and keep going
(2) Compute Dcurrent for x xk1
yk
E
xk
xk1
xk2
xk-1
10Bresenhams Line Drawing Algorithm (contd)
- Recall D 2xkdy 2ykdx C.
- At xk1, between E and NE, suppose we choose
E(xk1, yk). - Then, Dnew 2(xk1)dy 2ykdx C.
- Dcurrent 2xkdy 2ykdx C. So, Dnew Dcurrent
2dy. - In contrast, suppose we choose NE(xk1, yk1).
- Then, Dnew 2(xk1)dy 2(yk1)dx C. Dnew
Dcurrent 2dy2dx. - In summary, if Dcurrent 0, choose NE and let
Dnew be Dcurrent 2dy2dx Otherwise, choose E
and let Dnew be Dcurrent 2dy. - It is very important to note that both 2dy and
2dy-2dx are integers. So, if Dcurrent is
guaranteed to be an integer, we are doing just
integer additions all over the repeated steps. - Whether Dcurrent is an integer depends on Dold,
etc. Ultimately, if the first variable (named
Dstart) is an integer, the world is perfect!!
11Bresenhams Line Drawing Algorithm (contd)
- It is obvious that the first pixel to be drawn is
(x1,y1). - After choosing (x1,y1), compute the first
decision variable as follows - Note that y1 mx1B (dy/dx)x1B.
- Both dx and dy are integers, and so is Dstart.
- So, Bresenhams line drawing algorithm works
using integer additions only. What a gorgeous
algorithm!!!
Dstart 2x1dy 2y1dx 2dy 2Bdx dx
12Pseudo Code of Bresenhams Algorithm
- LineDrawing3(x1,y1,x2,y2)
- x x1
- y y1
- dx (x2 - x1)
- dy (y2 - y1)
- D (2dy - dx)
- delta1 2dy
- delta2 2dy - 2dx
- SetPixel(x, y)
- for (xx11 x
- if (D0)
- y y 1
- D D delta2
- else
- D D delta1
- SetPixel(x, y)
Why dont you simulate LineDrawing1 and
LineDrawing3 and compare the CPU times?
13Sampling, Aliasing, and Anti-aliasing
- A line consists of ? points. Such a continuous
line is sampled with a 2D array of finite number
of discrete points. Therefore, jaggies or
staircasing is inevitable. - Information distortion due to undersampling is
called aliasing, which is a ubiquitous problem in
signal processing and computer graphics. - We need anti-aliasing. Intuitively, anti-aliasing
is a technique for reducing jaggies. - The most popular anti-aliasing technique is
supersampling. (Recall that aliasing is caused by
undersampling.)
14Supersampling for Line Drawing
- Supersampling sampling an object at a higher
resolution, and combining the results to
reconstruct the object at a lower resolution. - We have 4-level intensities as the max of
subpixels of a pixel is 3.
divide each pixel Into e.g. 9 sub-pixels,
and apply Brensenhams with the sub-pixels
count the number of intensified sub-pixels
1
3
3
pixel intensity is set in proportion to the
number of sub-pixels
3
2
3
15Antialiased Line Example
- The stairstep effect is smoothed by displaying a
somewhat blurred line path in the vicinity of the
stairsteps (between horizontal lines). - Anti-aliasing is needed not only for lines, but
also for polygon edges, textures, etc. - Anti-aliasing is more complicated for
polygons/textures than for lines.