scan conversion 1
TRANSCRIPT
-
7/30/2019 Scan Conversion 1
1/18
I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS
Andries van Dam September 23, 2008 Scan Conversion 1 #/18
Scan Conversion 1(pages 72 -78 of textbook)
-
7/30/2019 Scan Conversion 1
2/18
I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS
Andries van Dam September 23, 2008 Scan Conversion 1 #/18
Line Drawing Draw a line on a raster screen between two
points
Whats wrong with statement of problem?
doesnt say anything about which points areallowed as endpoints
doesnt give a clear meaning of draw
doesnt say what constitutes a line in rasterworld
doesnt say how to measure success ofproposed algorithms
Problem Statement
Given two points Pand Q in XY plane, bothwith integer coordinates, determine whichpixels on raster screen should be on in orderto make picture of a unit-width line segmentstarting at Pand ending at Q
Scan Converting Lines
-
7/30/2019 Scan Conversion 1
3/18
I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS
Andries van Dam September 23, 2008 Scan Conversion 1 #/18
Special case: Horizontal Line:
Draw pixel Pand incrementxcoordinatevalue by 1 to get next pixel.
Vertical Line:
Draw pixel Pand increment ycoordinatevalue by 1 to get next pixel.
Diagonal Line:
Draw pixel Pand increment bothxand y
coordinate by 1 to get next pixel.
What should we do in general case?
Increment x coordinate by 1 andchoose point closest to line.
But how do we measure closest?
Finding next pixel:
-
7/30/2019 Scan Conversion 1
4/18
I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS
Andries van Dam September 23, 2008 Scan Conversion 1 #/18
Why can we use vertical distance as measureof which point is closer?
because vertical distance is proportional toactual distance
how do we show this?
with similar triangles
By similar triangles we can see that true
distances to line (in blue) are directlyproportional to vertical distances to line (inblack) for each point
Therefore, point with smaller vertical distanceto line is closest to line
Vertical Distance
(x1, y1)
(x2, y2)
-
7/30/2019 Scan Conversion 1
5/18
I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS
Andries van Dam September 23, 2008 Scan Conversion 1 #/18
Basic Algorithm
Find equation of line that connects twopoints Pand Q
Starting with leftmost point P, incrementxiby 1 to calculate yi= m*xi+ B
where m = slope, B = yintercept
Draw pixel at (xi, Round(yi)) where
Round (yi) = Floor (0.5 + yi)
Incremental Algorithm:
Each iteration requires a floating-pointmultiplication
Modify algorithm to use deltas (yi+1 yi) = m*(xi+1 - xi) + B B
yi+1 = yi + m*(xi+1 - xi)
Ifx= 1, then yi+1 = yi+ m At each step, we make incremental
calculations based on preceding step tofind next yvalue
Strategy 1 - Incremental
Algorithm (1/2)
-
7/30/2019 Scan Conversion 1
6/18
I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS
Andries van Dam September 23, 2008 Scan Conversion 1 #/18
Strategy 1 - Incremental
Algorithm (2/2)
),( ii yx ))(,1( myRoundx ii
))(,( ii yRoundx ),1( myx ii
-
7/30/2019 Scan Conversion 1
7/18
I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS
Andries van Dam September 23, 2008 Scan Conversion 1 #/18
Example Code
// Incremental Line Algorithm
// Assume x0 < x1
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;
y = y0;
for (x = x0; x < x1; x++) {
WritePixel(x, Round(y));
y = y + m;
}
}
-
7/30/2019 Scan Conversion 1
8/18
I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS
Andries van Dam September 23, 2008 Scan Conversion 1 #/18
Problem with Incremental
Algorithm:
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;
y = y0;
for (x = x0; x < x1; x++) {
WritePixel(x, Round(y));
y = y + m;
}
}
Rounding takes time
Since slope is fractional, needspecial case for vertical lines
-
7/30/2019 Scan Conversion 1
9/18
I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS
Andries van Dam September 23, 2008 Scan Conversion 1 #/18
Assume that lines slope is shallowand positive (0 < slope < 1); otherslopes can be handled by suitable
reflections about principle axes
Call lower left endpoint (x0, y0) andupper right endpoint (x1, y1)
Assume that we have just selected
pixel Pat (xp, yp)
Next, we must choose between pixelto right (E pixel), or one right andone up (NE pixel)
Let Q be intersection point of linebeing scan-converted and verticallinex=xp+1
Strategy 2 Midpoint Line
Algorithm (1/3)
-
7/30/2019 Scan Conversion 1
10/18
I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS
Andries van Dam September 23, 2008 Scan Conversion 1 #/18
Strategy 2 Midpoint Line
Algorithm (2/3)
),(pp
yxP 1
pxx
Previouspixel
Choices forcurrentpixel
Choicesfor next
pixel
E pixel
NE pixel
Midpoint M
Q
-
7/30/2019 Scan Conversion 1
11/18
I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS
Andries van Dam September 23, 2008 Scan Conversion 1 #/18
Line passes between E and NE
Point that is closer to intersection point Qmust be chosen
Observe on which side of line midpoint M
lies:
E is closer to line if midpoint Mlies aboveline, i.e., line crosses bottom half
NE is closer to line if midpoint Mlies belowline, i.e., line crosses top half
Error (vertical distance between chosenpixel and actual line) is always
-
7/30/2019 Scan Conversion 1
12/18
I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS
Andries van Dam September 23, 2008 Scan Conversion 1 #/18
Line equation as function f(x):
Line equation as implicit function:
for coefficients a, b, c, where a, b 0
from above,
Properties (proof by case analysis):
f(xm, ym) = 0 when any point Mis on line
f(xm, ym) < 0 when any point Mis above line f(xm, ym) > 0 when any point Mis below line
Our decision will be based on value offunction at midpoint Mat (xp + 1, yp + )
Line
Bxdx
dyy
Bmxy
0),( cbyaxyxf
dxBcdxbdya
dxBdxyxdy
dxBxdydxy
,,
0
-
7/30/2019 Scan Conversion 1
13/18
I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS
Andries van Dam September 23, 2008 Scan Conversion 1 #/18
Decision Variable d:
We only need sign off(xp+ 1, yp + )
to see where line lies, and then pick
nearest pixel
d= f(xp + 1, yp + )- ifd> 0 choose pixel NE
- ifd< 0 choose pixel E
- ifd= 0 choose either one consistently
How do we incrementally update d?
On basis of picking E or NE, figure out
location ofMfor that pixel, and
corresponding value dfor next grid line
We can derive dfor the next pixel
based on our current decision
Decision Variable
-
7/30/2019 Scan Conversion 1
14/18
I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS
Andries van Dam September 23, 2008 Scan Conversion 1 #/18
Increment Mby one inxdirection
dnew= f(xp + 2, yp + )
= a(xp + 2) + b(yp + ) + c
dold = a(xp + 1)+ b(yp + )+ c
dnew- dold is the incremental difference E
dnew= dold+ a
E= a = dy(2 slides back)
We can compute value of decision variableat next step incrementally without
computing F(M) directly
dnew= dold+ E= dold+ dy
E can be thought of as correction orupdate factor to take doldto dnew
It is referred to as forward difference
If E was chosen:
-
7/30/2019 Scan Conversion 1
15/18
I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS
Andries van Dam September 23, 2008 Scan Conversion 1 #/18
Increment Mby one in bothxand ydirections
dnew= F(xp + 2, yp + 3/2)
= a(xp + 2)+ b(yp + 3/2)+ c
NE = dnewdolddnew= dold+ a + b
NE = a + b = dydx
Thus, incrementally,
dnew= dold+ NE = dold+ dydx
If NE was chosen:
-
7/30/2019 Scan Conversion 1
16/18
I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS
Andries van Dam September 23, 2008 Scan Conversion 1 #/18
At each step, algorithm choosesbetween 2 pixels based on sign ofdecision variable calculated inprevious iteration.
It then updates decision variableby adding either E or NE to oldvalue depending on choice of pixel.Simple additions only!
First pixel is first endpoint (x0, y0),so we can directly calculate initialvalue ofdfor choosing between Eand NE.
Summary (1/2)
-
7/30/2019 Scan Conversion 1
17/18
I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS
Andries van Dam September 23, 2008 Scan Conversion 1 #/18
First midpoint for first d = dstartis at(x0 + 1, y0 + )
f(x0 + 1, y0 + )= a(x0 + 1) + b(y0 + ) + c
= a *x0 + b * y0 + c+ a + b/2= f(x0, y0) + a + b/2
But (x0, y0) is point on line and f(x0, y0) = 0
Therefore, dstart= a + b/2 = dydx/2
use dstartto choose second pixel, etc.
To eliminate fraction in dstart:
redefine fby multiplying it by 2; f(x,y) =2(ax+ by+ c)
this multiplies each constant and decisionvariable by 2, but does not change sign
Bresenhams line algorithm is same but doesntgeneralize as nicely to circles and ellipses
Summary (2/2)
-
7/30/2019 Scan Conversion 1
18/18
I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS
Andries van Dam September 23, 2008 Scan Conversion 1 #/18
Example Code
void MidpointLine(int x0, int y0,
int x1, int y1) {
int dx = x1 - x0;
int dy = y1 - y0;
int d = 2 * dy - dx;
int incrE = 2 * dy;
int incrNE = 2 * (dy - dx);
int x = x0;
int y = y0;
writePixel(x, y);
while (x < x1) {if (d