Professional Documents
Culture Documents
4
RISE
5
=
6
y2-y1 x2-x1
SLOPE =
RUN
DDA (digital differential analyzer) creates good lines but it is too time consuming due to the round function and long operations on real values.
Larry F. Hodges (modified by Amos Johnson) 6
DDA Example
Compute which pixels should be turned on to represent the line from (6,9) to (11,12). Length := Max of (ABS(11-6), ABS(12-9)) = 5 Xinc := 1 Yinc := 0.6 Values computed are: (6,9), (7,9.6), (8,10.2), (9,10.8), (10,11.4), (11,12)
13 12 11 10 9 6 7 8 9 10 11 12 13
(a,b)
-x
(0,0)
+x
-y
The general formula for a line is y = Mx + B where M is the slope of the line and B is the y-intercept. From our assumptions M = b/a and B = 0.
Therefore y = (b/a)x + 0 is f(x,y) = bx ay = 0 (an equation for the line). If (x1,y1) lie on the line with M = b/a and B = 0, then f(x1,y1) = 0.
Larry F. Hodges (modified by Amos Johnson) 9
Having turned on pixel P at (xi, yi), the next pixel is T at (xi+1, yi+1) or S at (xi+1, yi). Choose the pixel closer to the line f(x, y) = bx - ay = 0. The midpoint between pixels S and T is (xi + 1,yi + 1/2). Let e be the difference between the midpoint and where the line actually crosses between S and T. If e is positive the line crosses above the midpoint and is closer to T. If e is negative, the line crosses below the midpoint and is closer to S. To pick the correct point we only need to know the sign of e.
Larry F. Hodges (modified by Amos Johnson) 10
11
Note: The only non-integer value is a/2. If we then multiply by 2 to get d' = 2d, we can do all integer arithmetic using only the operations +, -, and left-shift. The algorithm still works since we only care about the sign, not the value of d.
Larry F. Hodges (modified by Amos Johnson)
12
Note: This algorithm only works for lines with Slopes between 0 and 1
(-a,b) (-a,-b)
(a,b) (a,-b)
14
Fast Circles
Consider only the first octant of a circle of radius r centered on the origin. We begin by plotting point (r,0) and end when x < y.
x +y -r
2 2
=0
x=y
The decision at each step is whether to choose the pixel directly above the current pixel or the pixel which is above and to the left. Assume Pi = (xi, yi) Ti = (xi, yi +1) Si = (xi -1, yi +1)
Larry F. Hodges (modified by Amos Johnson)
is the current pixel. is the pixel directly above is the pixel above and to the left.
15
e
S = (xi -1,yi +1)
Let di = f(xi - 1/2, yi+1) = -2(xi - 1/2)e - e2 Thus, P = (xi ,yi ) If e < 0 then di > 0 so choose point S = (xi - 1, yi + 1). di+1 = f(xi - 1 - 1/2, yi + 1 + 1) = ((xi - 1/2) - 1)2 + ((yi + 1) + 1)2 - r2 = di - 2(xi -1) + 2(yi + 1) + 1 = di + 2(yi+1- xi+1) + 1 If e >= 0 then di <= 0 so choose point T = (xi, yi + 1). di+1 = f(xi - 1/2, yi + 1 + 1) = di + 2yi+1 + 1
Larry F. Hodges (modified by Amos Johnson) 16
17
18
Fast Ellipses
The circle algorithm can be generalized to work for an ellipse but only four way symmetry can be used. F(x,y) = b2x2 + a2y2 -a2b2 = 0
(0,b) (-x, y) (x, y)
(-a,0)
(a,0)
(x, -y)
19
Fast Ellipses
The circle algorithm can be generalized to work for an ellipse but only four way symmetry can be used. F(x,y) = b2x2 + a2y2 -a2b2 = 0
(0,b) (x, y) (-x, y)
y=x
(-a,0)
(a,0)
(x, -y)
(-x, -y) (0,-b)
All the points in one quadrant must be computed. Since Bresenham's algorithm is restricted to only one octant, the computation must occur in two stages. The changeover occurs when the point on the ellipse is reached where the tangent line has a slope of 1. In the first quadrant, this is where the line y = x intersects the ellipses.
Larry F. Hodges (modified by Amos Johnson) 20