bgt
description
Transcript of bgt
7/21/2019 bgt
http://slidepdf.com/reader/full/bgt5695d0581a28ab9b029214bf 1/12
LECTURE: 07
DDA (DIGITAL DIFFERENTIAL
ANALYZER ALGORITHM)
7/21/2019 bgt
http://slidepdf.com/reader/full/bgt5695d0581a28ab9b029214bf 2/12
LINE DRAWING ALGORITHM-
STEPS
• Specifies (x,y) values of end pixels/points
• Need algorithm to figure out which intermediate
pixels are on line path
• Pixel (x,y) values constrained to integer valuesthough actual computed intermediate line values
may be floats
• Rounding may be required. E.g. computed point
(10.48, 20.51) rounded to (10, 21)• Rounded pixel value is off actual line path (jaggy!!)
• Sloped lines end up having jaggies
• Vertical, horizontal lines, no jaggies
7/21/2019 bgt
http://slidepdf.com/reader/full/bgt5695d0581a28ab9b029214bf 3/12
LINE DRAWING ALGORITHM
0 1 2 3 4 5 6 7 8 9 10 11 12
87
654321
Line: (3,2) -> (9,6)
?Which intermediatepie!" t# t$rn #n?
7/21/2019 bgt
http://slidepdf.com/reader/full/bgt5695d0581a28ab9b029214bf 4/12
Slope-intercept line equationy = mx + b
Given two end points (x0,y0), (x1, y1), how to compute m
and b?
(0,%0)
(1,%1)
d
d%
01
01
x x
y y
dx
dym
−
−
==
0*0 xm yb −=
LINE DRAWING ALGORITHM
4
7/21/2019 bgt
http://slidepdf.com/reader/full/bgt5695d0581a28ab9b029214bf 5/12
LINE DRAWING ALGORITHM
Numerical example of finding slope m:
(Ax, Ay) = (23, 41), (Bx, By) = (125, 96)
5392.0102
55
23125
4196==
−
−=
−
−=
Ax Bx
Ay Bym
5
7/21/2019 bgt
http://slidepdf.com/reader/full/bgt5695d0581a28ab9b029214bf 6/12
DIGITAL DIFFERENTIAL ANALYZER (DDA): LINE
DRAWING ALGORITHM
(0,%0)
(1,%1)
d
d%
Walk through the line, starting at (x0,y0)Constrain x, y increments to values in [0,1] rangeCase a: x is incrementing faster (m < 1) Step in x=1 increments, compute and round y
Case b: y is incrementing faster (m > 1) Step in y=1 increments, compute and round x
7/21/2019 bgt
http://slidepdf.com/reader/full/bgt5695d0581a28ab9b029214bf 7/12
DDA ALGORITHM
Y_inc
X_inc
7
7/21/2019 bgt
http://slidepdf.com/reader/full/bgt5695d0581a28ab9b029214bf 8/12
DDA LINE DRAWING ALGORITHM (CASE A: M < 1)
(0, %0)
& 0 ' 1 % & %0 ' 1 m
!!$minate pie! (, r#$nd(%))
& ' 1 % & % ' 1 m
!!$minate pie! (, r#$nd(%))
*
+nti! && 1
(1,%1)
& 0 % & %0
!!$minate pie! (, r#$nd(%))
m y y k k +=+1
7/21/2019 bgt
http://slidepdf.com/reader/full/bgt5695d0581a28ab9b029214bf 9/12
DDA LINE DRAWING ALGORITHM
(CASE B: M > 1)
% & %0 ' 1 & 0 ' 1 1m
!!$minate pie! (r#$nd(), %)
% & % ' 1 & ' 1 m
!!$minate pie! (r#$nd(), %)
*
+nti! % && %1
& 0 % & %0
!!$minate pie! (r#$nd(), %)(1,%1)
(0,%0)
m x x k k
1
1 +=
+
7/21/2019 bgt
http://slidepdf.com/reader/full/bgt5695d0581a28ab9b029214bf 10/12
DDA PSEUDO-CODE
// assume that slope is gentle
DDA(float x0, float x1, float y0, float y1)
{
float x, y;
float xinc, yinc;
int numsteps;
numsteps = Round(x1) – Round(x0);
xinc = (x1 – x0) / numsteps;
yinc = (y1 – y0) / numsteps;
x = x0;
y = y0;
ColorPixel(Round(x),Round(y));
for (int i=0; i<numsteps; i++) {
x += xinc;
y += yinc;
ColorPixel(Round(x),Round(y));
}}
7/21/2019 bgt
http://slidepdf.com/reader/full/bgt5695d0581a28ab9b029214bf 11/12
DDA EXAMPLE
Suppose we want todraw a line startingat pixel (2,3) andending at pixel (12,8).
What are the valuesof the variables x andy at each timestep?
What are the pixelscolored, according tothe DDA algorithm?
numsteps = 12 – 2 = 10xinc = 10/10 = 1.0
yinc = 5/10 = 0.5
11
t x y R(x) R(y)
0 2 3 2 3
1 3 3.5 3 42 4 4 4 4
3 5 4.5 5 5
4 6 5 6 5
5 7 5.5 7 6
6 8 6 8 6
7 9 6.5 9 7
8 10 7 10 7
9 11 7.5 11 8
10 12 8 12 8
7/21/2019 bgt
http://slidepdf.com/reader/full/bgt5695d0581a28ab9b029214bf 12/12
LINE DRAWING ALGORITHM
DRAWBACKS
DDA is the simplest line drawing algorithmNot very efficient
Round operation is expensive
12