Beruflich Dokumente
Kultur Dokumente
Algorithms
Larry F. Hodges
(modified by Amos Johnson)
P2 = (x2,y2)
3
2
1
P1 = (x1,y1)
1
SLOPE
=
RISE
RUN
y2-y1
x2-x1
B = [-(y2-y1)/(x2-x1)]y1 + y1
Larry F. Hodges
(modified by Amos Johnson)
Larry F. Hodges
(modified by Amos Johnson)
Larry F. Hodges
(modified by Amos Johnson)
For I := 0 To Length Do
Begin
Plot(Round(X), Round(Y));
X := X + Xinc;
Y := Y + Yinc
End {For}
End; {DDA}
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)
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
Larry F. Hodges
(modified by Amos Johnson)
10
11 12 13
-x
(0,0)
(a,b)
+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)
T = (xi + 1, yi + 1)
Assume:
Distance between pixels centers = 1
P = (xi,yi )
(xi +1,yi +
1/2)
S = (xi + 1, yi )
Having turned on pixel P at (x i, 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 (x i + 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
= f(xi +
11
x := 0;
y := 0;
d := b - a/2;
For i := 0 to a do
Plot(x,y);
If d >= 0 Then
x := x + 1;
y := y + 1;
d := d + b a
Else
x := x + 1;
d := d + b
End
End
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
Larry F. Hodges
(modified by Amos Johnson)
(-b,a)
(b,a)
(-a,b)
(a,b)
(-a,-b)
(a,-b)
End;
(-b,-a)
Larry F. Hodges
(modified by Amos Johnson)
(b,-a)
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.
2
x +y -r
=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)
Larry F. Hodges
(modified by Amos Johnson)
15
(xi -1/2, yi + 1)
T = (xi ,yi
+1)
f(xi - 1/2 + e, yi + 1)
= (xi - 1/2 + e)2 + (yi + 1)2 - r2
= (xi- 1/2)2 + (yi+1)2 - r2 + 2(xi-1/2)e + e2
e
S = (xi -1,yi +1)
P = (xi ,yi )
= f(xi - 1/2, yi + 1 + 1)
= di + 2yi+1 + 1
Larry F. Hodges
(modified by Amos Johnson)
16
= di + -2xi+1 + 2yi+1 + 1
Larry F. Hodges
(modified by Amos Johnson)
17
Larry F. Hodges
(modified by Amos Johnson)
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)
(-x, -y)
(0,-b)
Larry F. Hodges
(modified by Amos Johnson)
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)
y=x
(-x, y)
(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
21