Graphics Primitives Part II: Bresenhams line and circle

Preview:

Citation preview

Graphics Primitives Part II: Bresenhams line and circle

Bresenham’s Line AlgorithmAn 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.

Bresenham Line Algorithm (cont)

The difference between these 2 separations is

the pixel at (xk,yk) is to be displayed, the next point will be chosen from (xk +1, yk) and (xk+1, yK+1)

dlower = y – yk = m(xk + 1) + b – yk

dupper = (yk + 1) – y = yk + 1- m(xk + 1) – b

dlower-dupper = 2m(xk + 1) – 2 yk + 2b – 1

Bresenham’s Line AlgorithmDefine discriminant

Pk = Δx ( dlower-dupper) = 2Δyxk-2 Δxyk + c

The sign of Pk is the same as the sign of dlower-dupper, since Δx > 0.

Parameter c is a constant and has the value 2Δy + Δx(2b-1) (independent of pixel position)

If pk<0, the next point is (xk+1,yk);

Else the next point is (xk+1,yk+1)

Bresenham’s algorithm (cont) Increment thought:At step k + 1, the decision parameter can be evaluated as, pk+1 = 2Δyxk+1 - 2Δxyk+1 + c

Taking the difference of pk+ 1 and pk we get the following.

pk+1 – pk = 2Δy(xk+1- xk)-2Δx(yk+1 – yk)

But, xk+1 = xk +1, so that

pk+1 = pk + 2Δy - 2 Δx(yk+1 – yk)

Where the term yk+1-yk is either 0 or 1, depending on the sign of parameter pk

If pk<0 ,pk+1=pk+2 Δy Else pk+1=pk+(2Δy -2 Δx)

Bresenham’s Line AlgorithmInitial value of p0

The first parameter p0 is directly computed

p0 = 2 Δyx0 - 2 Δxy0 + c = 2 Δyx0 – 2 Δxy0 + Δx (2b-1) + 2Δy

Since (x0,y0) satisfies the line equation , we also have

y0 = Δy/ Δx * x0 + b

Combining the above 2 equations , we will have

p0 = 2Δy – Δx

Bresenham’s Line Algorithmvoid BresenhamLine (int x0,int y0,int xend, int yend,int

color)

{ int dx,dy, incre_p1, incre_p2, p, x, y;

dy=yend-y0, dx=xend-x0;

incre_p1=2*dy, incre_p2=2* (dy-dx);

x=x0, y=y0;

p=2*dy-dx; drawpixel(x, y, color); while (x<x1)

{ if (p<0) {x++, p+=incre_d1; }

else {x++, y++,p+=incre_d2;}

drawpixel (x, y, color); } /* while */ } /* Bresenham */

Circle-Scan conversion algorithmProperties of circle

Symmetry X=r cos (theta) Y=r sin (theta)

bisa tapi tidak efisien

Common algorithmsMidpointBresenham

Midpoint Circle AlgorithmNote 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 byfcircle(x,y) = x2 + y2 – r2

Thus,fcircle(x,y) < 0 if (x,y) is inside the circle boundary

fcircle(x,y) = 0 if (x,y) is on the circle boundary

fcircle(x,y) > 0 if (x,y) is outside the circle boundary

Midpoint Circle Algorithm

Define discriminant If dk < 0 , the next point is p1;

else ,the next point is p2

P=( xp, yp) P1

P2

M

222 )5.0()1(

)5.0,1()(

Ryx

yxFMFd

pp

pp

Midpoint Circle Algorithm

Improve-Increment thought:If dk<0, choose P1(xk+1,yk) as next point .

In order to judge the next point successively , calculate

Else ,choose P2(xk+1,yk-1) as next point . In order to judge the next point successively ,calculate

2 2 21 ( 2, 0.5) ( 2) ( 0.5) 2 3p p p p pd F x y x y R d x

2 2 22 ( 2, 1.5) ( 2) ( 1.5) 2( ) 5p p p p p pd F x y x y R d x y

Midpoint circle algorithm

Initial decision parameter is obtained by evaluating the circle function at the start position (x0,y0) = (0,r)

d0 = fcircle(1, r-0.5) = 1+ (r-0.5)2-r2

OR

0 1.25d r

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);

}}

Improve -Integer calculationsSubstitute e=d-0.25 for d

Untuk e bilangan bulat, jika e negatif d pasti negatif, begitupun sebaliknya

0 0.25 0d e e n e

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);

}}

Assignment: write and describe midpoint algorithm for ellipse

Ellipse-Scan Conversion algorithm

Ellipse equation

SymmetryCommon algorithms

MidpointBresenham

22

1c c

x y

x x y y

r r

Midpoint Ellipse algorithmEllipse function ( (xc,yc)=(0,0) )

2 2 2 2 2 2,ellipse y x x yf x y r x r y r r

0 ( , )

( , ) 0 ( , )

0 ( , )ellipse

if x y is inside

f x y if x y is on

if x y is outside

Consider first quadrant because of symmetry

A Dividing lineRegion 1 :

unit steps at x direction

Region 2: unit steps at y direction

2 21 y xy r x r y 2 2y xr x r y

2 2y xr x r y

Recommended