Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphics Primitives: line. Pixel Position 0 1 2 3 4 5 6.

Similar presentations


Presentation on theme: "Graphics Primitives: line. Pixel Position 0 1 2 3 4 5 6."— Presentation transcript:

1 Graphics Primitives: line

2 Pixel Position

3 Line-Scan Conversion Algorithms Line scan conversion: determine the nearest pixel position (integer) along the line between the two endpoints and store the color for each position in the frame buffer. Three common algorithms DDA Midpoint Bresenham

4

5 Line Equations The Cartesian slope-intercept equation ((x 0,y 0 )(x end,y end )) Y=mx+b

6 Naive Idea void NaiveLine(int x 0,int y 0,int x end,int y end,int color) int x; float y, m, b; m=(y end -y 0 )/(x end -x 0 ); b = y 0 – m*x 0 ; for (x=x 0 ; x x end ; x++) drawpixel (x, int(y+0.5), color); y=m*x+b; Costly floating point computations !! Multiplications, additions, roundings

7 DDA (Digital Differential Analyzer) ALGORITHMDigital Differential Analyzer The digital differential analyzer (DDA) samples the line at unit intervals in one coordinate corresponding integer values nearest the line path of the other coordinate. The following is the basic scan-conversion(DDA) algorithm for line drawing (sample at unit x intervals ) for x from x0 to xend Compute y=mx+b Draw_fn(x, round(y)) How to improve???

8 Reuse Previous Value Increment For Start from x=x 0 and y=y 0, every position (x,y) can be computed by incrementation, x by 1 and y by m.

9 void DDALine(int x 0,int y 0,int x end,int y end,int color) int x; float dx, dy, y, m; d x = x end -x 0, d y =y end -y 0 ; m=dy/dx; y=y 0 ; for (x=x 0 ; x x end ; x++) drawpixel (x, int(y+0.5), color); y=y+m; No more multiplications, but still have fp additions, roundings

10 Example draw segment x y int(y+0.5) round

11 DDA Illustration (x i, Round(y j )) (x i +1, y j +m) (x i, y j ) (x i +1, Round(y j +m)) Desired Line x1x2 y2 y1

12 The above DDALine only suit the condition that x end >x 0 and 0 < m < 1 (octant #1) How about the other cases? |m| > 1 x end < x 0 ?

13 Octant#2: x end >x 0 and 1 < m < Octant#3: x end x 0 and - < m < -1 Octant#8: x end >x 0 and -1 < m < 0 m=1 m=-1 m=m= m=0 #1#1 #2#2 #3#3 #4#4 #5#5 #6#6#7#7 #8#8

14 Octant#2 and #3: reverse the role of x as iterator by y and increment x by 1/m Octant#4: reverse the end points octant#8 Octant#5: reverse the end points octant#1 Octant#6: reverse the end points octant#2 Octant#7: reverse the end points octant#3 Octant#8: Just same DDA algorithm for octant#1

15 Major deficiency in the above approach : Uses floats Has rounding operations the accumulation of error

16 Midpoint Algorithm Basic thought( 0 < m < 1) M PTPT PBPB P=(x,y) Q P i (x i,y i ) M(x i+1,y i+0.5 ) According the position of M and Q, choose the next point P t or P b

17 Line equation ( (x 0,y 0 ), (x end,y end ) ) For any point (x, y): F(x,y) = 0 (x,y) is on the line F(x,y) > 0 (x,y) is above the line F(x,y) < 0 (x,y) is beneath the line

18 Discriminant Function d P2P2 P1P1 M P=(x,y) Q P i (x i,y i ) d < 0, M is beneath Q, P2 is next point; d > 0, M is above Q, P1 is the next point; d = 0, P1 or P2 is right, commonly P1 The function is made by using the midpoint M:

19 Is it (computing the d) costly? No! We can use the idea in the DDA: use previous d value to compute next one.

20 Incrementation thought If d 0 then choose the next point: P 1 (x p +1, y p ), In order to judge the next point successively calculate increment of d is a If d<0 then choose the next point: P 2 (x p +1, y p +1) In order to judge the next point successively,calculate increment of d is a+b

21 Initial value of d In each of iteration Else if d < 0 If d >= 0

22 Improve again: integer calculations?

23 Substitute 2d for d Initial value of d In each of iteration Implementation issue: the quantities (2a) and (2a+2b) can be precomputed to be two constants. If d >= 0 Else if d<0

24 Example Drawing a line from (0,0) to (5.2) ixiyid

25 void MidpointLine (int x 0,int y 0,int x end, int y end,int color) { int a, b, incre_d 1, incre_d 2, d, x, y; a=y 0 -y end, b=x end -x 0, d=2*a+b; incre_d 1 =2*a, incre_d 2 =2* (a+b); x=x 0, y=y 0 ; drawpixel(x, y, color); while (x


Download ppt "Graphics Primitives: line. Pixel Position 0 1 2 3 4 5 6."

Similar presentations


Ads by Google