Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphics Primitives Part II: Bresenhams line and circle.

Similar presentations


Presentation on theme: "Graphics Primitives Part II: Bresenhams line and circle."— Presentation transcript:

1 Graphics Primitives Part II: Bresenhams line and circle

2 Bresenhams Line Algorithm An accurate, efficient raster line drawing algorithm developed by Bresenham, scan converts lines using only incremental integer calculations that can be adapted to display circles and other curves.

3 Bresenham Line Algorithm (cont) The difference between these 2 separations is the pixel at (x k,y k ) is to be displayed, the next point will be chosen from (x k +1, y k ) and (x k +1, y K +1) d lower = y – y k = m(x k + 1) + b – y k d upper = (y k + 1) – y = y k + 1- m(x k + 1) – b d lower -d upper = 2m(x k + 1) – 2 y k + 2b – 1

4 Bresenham s Line Algorithm Define discriminant P k = Δx ( d lower -d upper ) = 2Δyx k -2 Δxy k + c The sign of P k is the same as the sign of d lower -d upper, since Δx > 0. Parameter c is a constant and has the value 2Δy + Δx(2b-1) (independent of pixel position) If p k <0, the next point is (x k +1,y k ); Else the next point is (x k +1,y k +1)

5 Bresenham s algorithm (cont) Increment thought: At step k + 1, the decision parameter can be evaluated as, p k+1 = 2Δyx k+1 - 2Δxy k+1 + c Taking the difference of p k+ 1 and p k we get the following. p k+1 – p k = 2Δy(x k+1 - x k )-2Δx(y k+1 – y k ) But, x k+1 = x k +1, so that p k+1 = p k + 2Δy - 2 Δx(y k+1 – y k ) Where the term y k+1 -y k is either 0 or 1, depending on the sign of parameter p k If pk<0,p k+1 =p k +2 Δy Else p k+1 =p k +(2Δy -2 Δx)

6 Bresenham s Line Algorithm Initial value of p 0 The first parameter p 0 is directly computed p 0 = 2 Δyx 0 - 2 Δxy 0 + c = 2 Δyx 0 – 2 Δxy 0 + Δx (2b-1) + 2Δy Since (x 0,y 0 ) satisfies the line equation, we also have y 0 = Δy/ Δx * x 0 + b Combining the above 2 equations, we will have p 0 = 2Δy – Δx

7 Bresenham s Line Algorithm void BresenhamLine (int x 0,int y 0,int x end, int y end,int color) { int dx,dy, incre_p 1, incre_p 2, p, x, y; dy=y end- y 0, dx=x end -x 0 ; incre_p 1 =2*dy, incre_p 2 =2* (dy-dx); x=x 0, y=y 0 ; p=2*dy-dx; drawpixel(x, y, color); while (x<x 1 ) { if (p<0) {x++, p+=incre_d 1 ; } else {x++, y++,p+=incre_d 2 ;} drawpixel (x, y, color); } /* while */ } /* Bresenham */

8 Circle-Scan conversion algorithm Properties of circle Symmetry X=r cos (theta) Y=r sin (theta) bisa tapi tidak efisien Common algorithms Midpoint Bresenham

9 Midpoint Circle Algorithm Note that along the circle section from x=0 to x=y in the first octant, the slope of the curve varies from 0 to -1 Circle function around the origin is given by f circle (x,y) = x 2 + y 2 – r 2 Thus, f circle (x,y) < 0 if (x,y) is inside the circle boundary f circle (x,y) = 0 if (x,y) is on the circle boundary f circle (x,y) > 0 if (x,y) is outside the circle boundary

10 Midpoint Circle Algorithm Define discriminant If d k < 0, the next point is p1; else,the next point is p2

11 Midpoint Circle Algorithm Improve-Increment thought: If d k <0, choose P 1 (x k +1,y k ) as next point. In order to judge the next point successively calculate Else,choose P 2 (x k +1,y k -1) as next point. In order to judge the next point successively calculate

12 Midpoint circle algorithm Initial decision parameter is obtained by evaluating the circle function at the start position (x0,y0) = (0,r) d 0 = f circle (1, r-0.5) = 1+ (r-0.5) 2 -r 2 OR

13 MidPointCircle(int r int color) {int x,y; float d; x=0; y=r; d=1.25-r; circlepoints (x,y,color); //draw (x,y) and other symmetric points while(x<=y) {if(d<0) d+=2*x+3; else { d+=2*(x-y)+5; y--;} x++; circlepoints (x,y,color); }

14 Improve -Integer calculations Substitute e=d-0.25 for d Untuk e bilangan bulat, jika e negatif d pasti negatif, begitupun sebaliknya

15 MidPointCircle(int r int color) {int x,y; float d; x=0; y=r; d=1-r; circlepoints (x,y,color); //draw (x,y) and other symmetric points while(x<=y) {if(d<0) d+=2*x+3; else { d+=2*(x-y)+5; y--;} x++; circlepoints (x,y,color); }

16 Assignment: write and describe midpoint algorithm for ellipse

17 Ellipse-Scan Conversion algorithm Ellipse equation Symmetry Common algorithms Midpoint Bresenham

18 Midpoint Ellipse algorithm Ellipse function (x c,y c )=(0,0)

19 Consider first quadrant because of symmetry A Dividing line Region 1 : unit steps at x direction Region 2: unit steps at y direction


Download ppt "Graphics Primitives Part II: Bresenhams line and circle."

Similar presentations


Ads by Google