58
INDIAN INSTITUTE OF TECHNOLOGY ROORKEE DEPARTMENT OF CIVIL ENGINEERING CE201: Computer Aided Graphics Week 5, 6, 7 Rajat Rastogi [email protected] [email protected]

CE-201CE-201_Week 5-7_2010

Embed Size (px)

Citation preview

Page 1: CE-201CE-201_Week 5-7_2010

INDIAN INSTITUTE OF TECHNOLOGY ROORKEEDEPARTMENT OF CIVIL ENGINEERING

CE‐201: Computer Aided GraphicsWeek 5, 6, 7

Rajat Rastogi

[email protected]@iitr.ernet.in

Page 2: CE-201CE-201_Week 5-7_2010

Previous LecturePrevious Lecture

• Image File FormatsImage File Formats– Raster Formats

Vector Formats– Vector Formats

– Format Structure

C l S tti– Colour Setting

Page 3: CE-201CE-201_Week 5-7_2010

Lecture OutlineLecture Outline

• Scan ConversionScan Conversion– Point

Line– Line

– Circle

Page 4: CE-201CE-201_Week 5-7_2010

Point ScanPoint Scan

• Point plotting is furnished by converting a singlePoint plotting is furnished by converting a single coordinate position into appropriate operations for the output device in use.

• Random scan uses stored point‐plotting instructions (coordinates) and convert them in deflection voltage that positions the electron beam at the screen locations to be plotted during each refresh cycle.

Page 5: CE-201CE-201_Week 5-7_2010

Point Scan…1Point Scan…1

• Raster scan depends on setting the bit valueRaster scan depends on setting the bit value corresponding the specified screen position within the frame buffer to 1. The electron beam when sweeps across such position in a line emits burst of electrons with intensities corresponding to colour d t b di l d t th t l ticodes to be displayed at that location.

Page 6: CE-201CE-201_Week 5-7_2010

Point Scan…2Point Scan…2

• Let as scan‐convert a point (x, y) to a pixel locationLet as scan convert a point (x, y) to a pixel location (x’, y’)• x’ = Floor(x), and y’ = Floor(y)

• Where Floor is a function that returns largest integer that is less than or equal to the argument.

• In such a case, the origin is placed at the lowest left corner of the pixel grid in the image space

All i h i f ’ ≤ ’ 1 d ’ ≤ ’ 1• All points that satisfy x’ ≤ x < x’+1 and y’ ≤ y < y’ + 1 will be mapped to pixel location (x’, y’) (refer Fig a)

Page 7: CE-201CE-201_Week 5-7_2010

Point Scan…3Point Scan…3

Xiang and Plastock

Page 8: CE-201CE-201_Week 5-7_2010

Point Scan…4Point Scan…4

• If the origin of coordinate system for (x y) is toIf the origin of coordinate system for (x, y) is to be shifted to new location say (x+0.5, y+0.5), thenthen– x’ = Floor(x+0.5), and y’ = Floor(y+0.5)

This will place the origin of the coordinate system– This will place the origin of the coordinate system (x, y) at the centre of pixel (0, 0)

– Now all points that satisfy x’‐0 5 ≤ x < x’+0 5 and– Now, all points that satisfy x ‐0.5 ≤ x < x +0.5 and y’‐0.5 ≤ y < y’+0.5 will be mapped to pixel location (x’, y’) (refer Fig b)( , y ) ( g )

Page 9: CE-201CE-201_Week 5-7_2010

Line Scan‐ConversionLine Scan Conversion

• Line plotting is done by calculating intermediate e p o g s do e by ca cu a g e ed a epositions between the two specified endpoint positions.

• Linearly varying horizontal and vertical deflection voltages are generated proportion to the required changes in x, y directions to produce the smooth line.

A t i ht li t i di l d b l tti th• A straight line segment is displayed by plotting the discrete points between endpoint positions.– These intermediate positions are calculated from the equationThese intermediate positions are calculated from the equation 

of the line

– Colour intensity is loaded into the frame buffer at the corresponding pixel coordinatecorresponding pixel coordinate

Page 10: CE-201CE-201_Week 5-7_2010

Line Scan‐Conversion…1Line Scan Conversion…1

• Screen locations are referenced with integer values. E.g. Sc ee oca o s a e e e e ced ege a ues g(10.48, 20.51) will be converted to pixel position (10, 20)

• This rounding effect causes lines to be displayed with a stairstep appearance . This is noticeable on low resolution systems.

B tt thi b hi d b dj ti li ht• Better smoothing can be achieved by adjusting light intensities along the line paths. 

Page 11: CE-201CE-201_Week 5-7_2010

Line Scan‐Conversion…2Line Scan Conversion…2

• Pixel column number is basically pixel position across a e co u u be s bas ca y p e pos o ac oss ascan line. These are increasing from left to right.

• Scan line number are increasing from bottom to top.

• Loading a specified colour into frame buffer at a pixel position defined by l ‘ ’ l li ‘ ’ne

No

column ‘x’ along scan line ‘y’

setPixel (x, y)

• Current frame buffer intensity for a

Sca

n Li

n

• Current frame‐buffer intensity for a specified location can be accomplished 

getPixel (x, y)12 g ( , y)

Pixel column No

00 12

1

Page 12: CE-201CE-201_Week 5-7_2010

Line Scan‐Conversion…3Line Scan Conversion…3

• Line is defined by two end points P1, P2 and the equation of y p 1, 2 qline as shown in the Figure

• These can be plotted or scan‐converted using different line l tti l ithplotting algorithms.

• The slope‐intercept equation is not suitable for vertical lines. 

yP2(x2, y2)

Horizontal, vertical and diagonal (|m| = 1) lines can be handled as special cases and can be

y = m.x + b

( ) as special cases and can be mapped to image space straightforward.

b

P1 (x1, y1)

x

Page 13: CE-201CE-201_Week 5-7_2010

Line Drawing AlgorithmsLine Drawing Algorithms 

• Direct use of line equationDirect use of line equation

• DDA algorithm

h ’ i l i h• Bresenham’s Line Algorithm

Page 14: CE-201CE-201_Week 5-7_2010

Direct use of Line equationDirect use of Line equation

• Direct approachDirect approach– The Cartesian slope intercept equation for a 

y2

straight line:

y = m . x + b (1)‘ ’ l f

y1

x x2– ‘m’ represents slope of the line and ‘b’ is intercept on y‐axis, such 

x1 x2

p ythat

(2)12

12

xxm yy

−= − y1

y2

(3)11

12 . xmyb −=

x1 x2

Page 15: CE-201CE-201_Week 5-7_2010

Direct use of Line equation..1Direct use of Line equation..1

• For a small change in ‘x’ • For lines with slopeFor a small change in  x  along the line, the corresponding change 

For lines with slope magnitudes |m|<1, Δx is set proportional to 

in ‘y’ can be given byΔy = m . Δx (4)

small horizontal deflection voltage and th dior Δx = Δy/m (5)

– These equations form the basis for determining

the corresponding vertical deflection is then set proportional tothe basis for determining 

the deflection voltages in analog devices.

then set proportional to Δy as calculated from the equation (4). q ( )

Page 16: CE-201CE-201_Week 5-7_2010

Direct use of Line equation…2Direct use of Line equation…2

• For lines with slope • For lines with slopeFor lines with slope magnitudes |m|>1, Δy is set proportional to a 

For lines with slope magnitudes m=1, Δx = Δy and the horizontal 

small vertical deflection voltage and the 

di

and vertical deflection voltage are equal

corresponding horizontal deflection is then set proportional tothen set proportional to Δx as calculated from the equation (5). q ( )

Page 17: CE-201CE-201_Week 5-7_2010

Direct use of Line equation…3Direct use of Line equation…3

• This means:This means:– First scan convert end points to pixel locations (x1’, y1’) and (x2’, y2’)

– Set ‘m’ and ‘b’ using equations

– For |m|≤ 1, for integer value of x between and excluding x ’ x ’ calculate the corresponding value of y and scanx1 , x2 , calculate the corresponding value of y and scan‐convert (x, y)

– For |m|> 1, for integer value of x between and excluding | | g gy1’, y2’, calculate the corresponding value of x and scan‐convert (x, y)

Page 18: CE-201CE-201_Week 5-7_2010

Direct use of Line equation…4Direct use of Line equation…4

• Example:Example:– End points are (0, 0) and (6, 18). Compute each value of y as x steps from 0 to 6 and plot.

• For equation y = m.x + b, compute slope as– m = Δy/Δx

• Using value of ‘m’ compute ‘b’

• Place x = 0 to 6 and compute corresponding value of ‘y’

• Plot 

Page 19: CE-201CE-201_Week 5-7_2010

Digital Differential Analyzer (DDA)Digital Differential Analyzer (DDA)

• DDA is an incremental scan‐conversion line algorithmDDA is an incremental scan conversion line algorithm based on calculating either Δx or Δy as mentioned in line primitives.

• Suppose at step ‘i’ the point is (xi, yi) on the line.

• For next point (xi+1, yi+1)p i+1 yi+1– yi+1 = yi + Δy, where Δy = m Δx– xi+1 = xi + Δx, where Δx = Δy/m– The process continues until x reaches x2’ (for |m|≤ 1 case) or y reaches y2’ (for |m|> 1 case)

Page 20: CE-201CE-201_Week 5-7_2010

Digital Differential Analyzer…1Digital Differential Analyzer…1

• Consider a line with positive slope. Let the slope beConsider a line with positive slope. Let the slope be less than or equal to 1.  For unit interval Δx=1, 

yk+1 = yk + m, where k = 1, 2, …., end point (6)

– The value of yk+1 should be rounded to the nearest integer

• For slope greater than 1, increasing Δy by 1xk+1 = xk + 1/m, where k = 1, 2, …., end point (7)

Page 21: CE-201CE-201_Week 5-7_2010

Digital Differential Analyzer…2Digital Differential Analyzer…2

• The assumption here is that lines are processed fromThe assumption here is that lines are processed from left to right. If it is processed from right to left then

yk+1 = yk – m (8)

xk+1 = xk – 1/m (9)y

x

Page 22: CE-201CE-201_Week 5-7_2010

Digital Differential Analyzer…3Digital Differential Analyzer…3

• For negative slope of line as shown in figure:For negative slope of line as shown in figure:• If moving from left to right:

– Set Δx = +1 and use equation (8) for m<1 to calculate y values

– Set Δ y = ‐1 and use equation (7) for m>1 to calculate x values

• If moving from right to left:– If m < 1 then set Δx = ‐1 and obtain y

y– If m < 1 then set Δx = ‐1 and obtain y values from equation (6)

– If m > 1 then set Δy = +1 and obtain x values from equation (9)values from equation (9)

x

Page 23: CE-201CE-201_Week 5-7_2010

Digital Differential Analyzer…4Digital Differential Analyzer…4

• This algorithm is faster in calculating pixel positionsThis algorithm is faster in calculating pixel positions

• It eliminates multiplication by making use of raster characteristics 

• But rounding off in successive additions and its accumulation may cause drifting of the pixel position y g p paway from the true line

• This rounding off procedure is also time consuming

• By converting m or 1/m into integer or fractional part, the time consumed can be reduced.

Page 24: CE-201CE-201_Week 5-7_2010

Bresenham’s Line AlgorithmBresenham s Line Algorithm

• This raster‐line generating algorithm is more accurate s as e e ge e a g a go s o e accu a eand efficient

• It uses only incremental integer calculations 

• It can be adapted to display circles and other curves.

• The method is based on identifying the nearest pixel position closer to the line path at each sample step.

13 53

11

12

51

52

53

10

10 11 12 13

50

50 51 52 53

Fig-1 Fig-2

Page 25: CE-201CE-201_Week 5-7_2010

Bresenham’s Line Algorithm…1Bresenham s Line Algorithm…1

• In Fig 1 the line segment  13g e e seg estarts from scan line 11 and pixel position ( l ) 10 Th 11

12

13

(column) 10. The next point along the line may be at pixel position (11,

Fig-1 10

11

10 11 12 13be at pixel position (11, 11) or (11, 12)

• Similarly, in Fig 2 the line 52

53

segment starts from (50, 52) and the next pixel position may be (51 52) Fig-250

51

52

position may be (51, 52) or (51, 51)

Fig 250 51 52 53

Page 26: CE-201CE-201_Week 5-7_2010

Bresenham’s Line Algorithm…2Bresenham s Line Algorithm…2

• Bresenham’s line algorithm tests the sign of anBresenham s line algorithm tests the sign of an integer parameter and the one whose value is proportional to the difference between the separations of the two pixel positions from the actual line path is selected. 

• Let the line starts from left‐end point (xo, yo) of a given line. Now,the pixel whose scan line y value

yk+2

yk+3

the pixel whose scan‐line y value is closest to the line path is plotted. yk

yk+1

xk xk+1 xk+2 xk+3

Fig-3

k k+1 k+2 k+3

Page 27: CE-201CE-201_Week 5-7_2010

Bresenham’s Line Algorithm…3Bresenham s Line Algorithm…3

• Let the pixel displayed is (xk, yk). The next pixel toLet the pixel displayed is (xk, yk). The next pixel to plot will be in column xk+1 and may be represented as (xk+1, yk) or (xk+1, yk+1) yk 3

• The difference between estimated value of y and new y value along the yk+1

yk+2

yk+3

d2

y = m.x + b

line is to be computed.For first point yest =m . (xk+1) + b   (10) 

iff d (11)

yk

yk 1

xk xk+1 xk+2 xk+3

d1

d2

Difference  d1 = yest – yk (11)

For second point d2 = (yk+1) – yest (12)

Difference between the two separations = d – d

Fig-3

Difference between the two separations = d1 – d2

Page 28: CE-201CE-201_Week 5-7_2010

Bresenham’s Line Algorithm…4Bresenham s Line Algorithm…4

• Difference (d1 – d2) = 2m.(xk+1) – 2 yk + 2b – 1 (13)Difference (d1 d2)   2m.(xk+1)  2 yk + 2b  1 (13)

• Let m = Δy/Δx denoting vertical and horizontal separations. Let px is defined as yk 3p px

px = Δ x (d1 – d2)

px = 2 Δy . xk ‐ 2 Δx . yk + c (14) yk+1

yk+2

yk+3

d2

y = m.x + b

Where, c = 2 Δy + Δx (2b – 1) (15)

• Decision parameter px is –ve i.e. d1<d2 yk

yk 1

xk xk+1 xk+2 xk+3

d1

d2

– Pixel at yk is closer to line than that at yk+1

– Lower point will be plotted

Fig-3

– Vice‐Versa

Page 29: CE-201CE-201_Week 5-7_2010

Bresenham’s Line Algorithm…5Bresenham s Line Algorithm…5

• The change in coordinate along the line takes placeThe change in coordinate along the line takes place in unit steps. The decision parameter at next step will be:

pk+1 = 2Δy . xk+1 ‐ 2 Δx . yk+1 + cAnd pk+1 – pk = 2 Δy (xk+1 – xk) ‐ 2 Δx (yk+1 – yk) (16)But xk+1 = xk+1, so that 

pk+1 = pk + 2 Δy ‐ 2 Δx (yk+1 – yk) (17)( ) is either 0 or 1 depending pon the sign of p(yk+1 – yk) is either 0 or 1 depending upon the sign of pk.

The initial value of parameter will be 

p = 2 Δy ‐ Δx (18)po = 2 Δy  Δx (18)

Page 30: CE-201CE-201_Week 5-7_2010

Bresenham’s Line Algorithm…6Bresenham s Line Algorithm…6

• Algorithm for |m|<1: (steps)Algorithm for |m|<1: (steps)– Input and store (xo, yo). Plot it (load on buffer).

– Calculate Δx, Δy, 2 Δy ‐ 2 Δx– Compute decision parameter, po (= 2 Δy ‐ Δx)– Assuming pk < 0, the next point will be (xk+1, yk) and 

pk+1 = pk + 2ΔyOtherwise, plot point (xk +1, yk +1) and 

2 Δ 2 Δpk+1 = pk + 2 Δy ‐ 2 Δx – Repeat step Δx times

Page 31: CE-201CE-201_Week 5-7_2010

Bresenham’s Line Algorithm…7Bresenham s Line Algorithm…7

• E.g. Plot a line with end points (20, 10) and (30, 18).E.g. Plot a line with end points (20, 10) and (30, 18). The slope of line is 0.8.– Calculate Δx (=10), Δy (=8)– Compute decision parameter, po (= 2 Δy ‐ Δx = 6)– Compute 2 Δy (=16), 2 Δy ‐ 2 Δx (= ‐4) – Assuming pk < 0, the next point will be (xk+1, yk) and 

pk+1 = pk + 2ΔyOth i l t i t ( 1 1) dOtherwise, plot point (xk +1, yk +1) and 

pk+1 = pk + 2 Δy ‐ 2 Δx – Repeat step Δx timesRepeat step Δx times

Page 32: CE-201CE-201_Week 5-7_2010

Bresenham’s Line Algorithm…8Bresenham s Line Algorithm…8

• It is generalized to lines with arbitrary slopes.s ge e a ed o es a b a y s opes

• Considers symmetry between octants and quadrants of the xy plane.

• Algorithm allows moving along pixel position from either end of the line.

• For positive slope and start from left end point, both the increments are positive. It is vice‐versa for start from right end pointright end point.

• For negative slope, one increment will be positive and other negative depending upon the start point.g p g p p

Page 33: CE-201CE-201_Week 5-7_2010

Bresenham’s Line Algorithm…9Bresenham s Line Algorithm…9

• When the two vertical separations from the line path are e e o e ca sepa a o s o e e pa a eequal (d1 = d2), we choose upper (or lower) of the two candidate pixels.

• Special cases like Δy = 0 (horizontal line); Δx = 0 (vertical line) and |Δx| = |Δy| (diagonal line) can be loaded directly into the frame buffer without processing themdirectly into the frame buffer without processing them through the line –plotting algorithm.

Page 34: CE-201CE-201_Week 5-7_2010

Circle Generating AlgorithmsCircle Generating Algorithms

• Properties of circle:Properties of circle:– A circle is defined as the set of points that are all at a givendistance r from a center position (xc,yc)

– This distance relationship is expressed by the Pythagoreantheorem in Cartesian coordinates as

(x x )2 + (y y )2 = r2 (22)(x – xc)2 + (y – yc)2 = r2 (22)

– For position of points on a circle• Move from (xc – r) to (xc + r) in unit steps r( c ) ( c ) p

• Calculate corresponding y values as:

y = yc ± √(r2 – (xc – x)2 (23) xc

yc

Page 35: CE-201CE-201_Week 5-7_2010

Circle Generating Algorithms…1Circle Generating Algorithms…1

• This is not the best method for generating a circle.g g• One problem with this approach is that it involvesconsiderable computation at each step.

• Moreover, the spacing between plotted pixelpositions is not uniformTh i b dj t d b i t h i d• The spacing can be adjusted by interchanging x and ywhenever the absolute value ofslope of the curve is greater than 1.p g

• This is computationally morecumbersome.

Page 36: CE-201CE-201_Week 5-7_2010

Circle Generating Algorithms…2Circle Generating Algorithms…2

• Another approach is to use polar coordinates r and θ,o e app oac s o use po a coo d a es a d θ,yielding

x = xc + r Cos θ (24)

y = yc + r Sin θ (25)

• Larger angular separations along the circumference canbe connected with straight line segments to approximatethe circular path.

• For a more continues boundary on a raster display the• For a more continues boundary on a raster display, thestep size is set at 1/r. This plots pixel positions that areapproximately one unit apart.pp y p

Page 37: CE-201CE-201_Week 5-7_2010

Circle Generating Algorithms…3Circle Generating Algorithms…3

• Computation can be reduced either byComputation can be reduced either by– Considering symmetry w.r.t. y‐axis for segment in 1st and2nd quadrant, and then considering symmetry w.r.t. x‐axis

d hfor segment in 3rd and 4th quadrant or

– Considering the symmetry between adjacent octantswithin a quadrant w r t 45o linewithin a quadrant w.r.t 45 line

– Therefore calculations are requiredfor points between x=0 and x=y 450

(y, x)

(-x, y)

(-y, x)

(x, y)

and then these are mapped to resteight sectors.

(-x, -y) (x, -y)

(-y, -x) (y, -x)

Page 38: CE-201CE-201_Week 5-7_2010

Circle Generating Algorithm…4Circle Generating Algorithm…4

Page 39: CE-201CE-201_Week 5-7_2010

Circle Generating Algorithms…5Circle Generating Algorithms…5

• All these procedures require a good deal ofAll these procedures require a good deal ofcomputation time

• More efficient circle algorithms are based ongincremental calculations as in the Bresenham linealgorithm

• Bresenham’s line algorithm is adapted to circlegeneration by setting up decision parameters forfinding the closest pixel to the circumference at eachfinding the closest pixel to the circumference at eachsampling step

• It avoids square‐root calculations by comparing theq y p gsquares of the pixel separation distances

Page 40: CE-201CE-201_Week 5-7_2010

Midpoint Circle AlgorithmMidpoint Circle Algorithm 

• The closest pixel position to the specified circle pathThe closest pixel position to the specified circle pathis determined by moving unit distance at each step

• Let radius is r and screen center position is (xc, yc)p ( c, yc)

• Set up algorithm to calculate pixel positions around acircle path centered at the coordinate origin (0, 0)p g

• To move each calculated position (x, y) to its properscreen position add xc to x and yc to y

• Along the circle section from x = 0 to x = y in the firstquadrant the slope of the curve varies from 0 to ‐1

Page 41: CE-201CE-201_Week 5-7_2010

Midpoint Circle Algorithm…1Midpoint Circle Algorithm…1

• To apply the midpoint method, circle function isTo apply the midpoint method, circle function isdefined as:

fcircle (x, y) = x2 + y2 – r2 (26)

Such that

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

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

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

Thi b h d i i f id i• This becomes the decision parameter for midpoint algorithm

Page 42: CE-201CE-201_Week 5-7_2010

Midpoint Circle Algorithm…2Midpoint Circle Algorithm…2

• Let us consider the midpoint between the twoLet us consider the midpoint between the twocandidate pixels at sampling position xk+1.

• After plotting of the pixel at (xk, yk), next top g p ( k, yk),determine is whether the pixel at position (xk+1, yk)or the one at position (xk+1, yk‐1) is closer to thealong a circular path.

circle.

• Decision parameter will be: x2+y2-r2=0yk

Midpointyk-1⎟⎠⎞

⎜⎝⎛ −+=

21,1 kkcirclek yxfp

( ) 22

2 1 ⎞⎛(27) xk xk + 1 xk + 2( ) 2

k2

k r21y1x −⎟⎠⎞

⎜⎝⎛ −++=

Page 43: CE-201CE-201_Week 5-7_2010

Midpoint Circle Algorithm…3Midpoint Circle Algorithm…3

• If pk<0, this midpoint is inside the circle and the pixelIf pk<0, this midpoint is inside the circle and the pixelon scan line yk is closer to the circle boundary.

• Otherwise, the mid position is outside or on the, pcircle boundary, and we select the pixel on scanlineyk‐1.

• Successive decision parameters are obtained usingincremental calculations. For pixel location (xk+1+ 1)

(28)

• yk+1 is either yk or yk‐1 depending on the sign of pk

1)yy()yy()1x(2pp k1k2k

21kkk1k +−−−+++= +++

Page 44: CE-201CE-201_Week 5-7_2010

Midpoint Circle Algorithm…4Midpoint Circle Algorithm…4

• Increments for obtaining pk+1 are either 2xk+1 +1 (if pkIncrements for obtaining pk+1 are either 2xk+1 +1 (if pkis negative) or (2xk+1 + 1 ‐ 2yk+1)

• Evaluation of the terms 2xk+1 and 2yk+1 can also bek+1 yk+1done incrementally as

2xk+1 = 2xk + 2; 2yk+1 = 2yk ‐ 2

• At the start position (0, r) these two terms have thevalues 0 and 2r, respectively.

• Each successive value is obtained by adding 2 to theprevious value of 2x and subtracting 2 from the

i l f 2previous value of 2y.

Page 45: CE-201CE-201_Week 5-7_2010

Midpoint Circle Algorithm…5Midpoint Circle Algorithm…5

• The initial decision parameter is obtained byThe initial decision parameter is obtained byevaluating the circle function at the start position(x0, y0) = (0, r) as:

rfp cricle

⎞⎛

⎟⎠⎞

⎜⎝⎛ −=

1

21 ,1

2

0

(29)rp

rr

−=

−⎟⎠⎞

⎜⎝⎛ −+=

45

211

0

2

( )

• If the radius r is specified as an integer, we can simplyround to p0 = 1 – r (for r an integer).

p40

or p0 ( g )

Page 46: CE-201CE-201_Week 5-7_2010

StepsSteps 

• Input radius r, circle center (xc, yc), and obtain the first pointc con the circumference of a circle centered at the origin as (x0,y0) = (0, r)

• Calculate the initial value of the decision parameter asCalculate the initial value of the decision parameter asp0 = 5/4 – r

• At each xk position, starting at k = 0, perform the followingtest: if p < 0 the next point along the circle centered on (0 0)test: if pk < 0, the next point along the circle centered on (0,0)is (xk+1 , yk) and pk+1 = pk + 2xk+1 + 1

• Otherwise, the next point along the circle is (xk+1, yk‐1) and 2 1 2pk+1 = pk + 2xk+1 + 1 ‐ 2yk+1 

Where 2xk+1 = 2x k  + 2 and 2yk+1 = 2y k  ‐ 2• Determine symmetry points in the seven other octantsDetermine symmetry points in the seven other octants• Move pixel position (calculated) to screen position by adding

Page 47: CE-201CE-201_Week 5-7_2010

ExampleExample

• Plot the generated pixel positions for a circle with

y = xy

Plot the generated pixel positions for a circle with radius r = 10 using midpoint circle algorithm.– po = 1 – r = ‐9

8

9

10

y

– Initial point (0, 10)

– Decision parameters

4

5

6

72xo = 0;  2yo = 20

1

2

3

4

x0 1 2 3 4 5 6 7 8 9 10

0

Page 48: CE-201CE-201_Week 5-7_2010

BRESENHAM’S ALGORITHMBRESENHAM S ALGORITHM

• Assume that (xi, yi) are the coordinates of the last scan‐ssu e a ( i, yi) a e e coo d a es o e as scaconverted pixel upon entering step i.

• Let the distance from the origin to pixel T squared minusthe distance to the true circle squared = D(t).

• Then let the distance from the origin to pixel S squaredi th di t t th t i l d D( )minus the distance to the true circle squared = D(s).

• As the coordinates of T are (xi + 1, yi) and those of S are(x + 1 y ‐1) the following expression can be developed:(xi + 1, yi ‐1), the following expression can be developed:

Page 49: CE-201CE-201_Week 5-7_2010

BRESENHAM’S ALGORITHM…1(xi + 1, yi)T(xi, yi)y

BRESENHAM S ALGORITHM…1

(xi, + 1, yi - 1)S

y

r

( ) ( )22 11 −++ ii yx

x

Choosing Pixels in Bresenham’s Circle Algorithm

Page 50: CE-201CE-201_Week 5-7_2010

BRESENHAM’S ALGORITHM…2BRESENHAM S ALGORITHM…2

D(t) = (xi + 1)2 + yi2 ‐ r2( ) ( i ) yiD(s) = (xi+1)2 + (yi – 1)2 – r2

• This function D provides a relative measurement of the• This function D provides a relative measurement of thedistance from the center of a pixel to the true circle.

• Since D(t) will always be positive (T is outside the true( ) y p (circle) and D(s) will always be negative (S is inside thetrue circle), a decision variable, di may be defined asfollows:follows:

di = D(t) + D(s)Therefore

di = 2(xi + 1)2 + yi2 + (yi – 1)2 – 2r2

Page 51: CE-201CE-201_Week 5-7_2010

BRESENHAM’S ALGORITHM…3BRESENHAM S ALGORITHM…3

• When di<0, |D(t)|<|D(s)|, then select pixel T.e di 0, | ( )| | (s)|, e se ec p e

• When di ≥ 0, |D(t) ≥ |D(s)|, then select pixel S.

• The decision variable di+1 for the next step:i p

di+1 = (2xi+1+1)2 + y2i+ 1 + (yi+1‐1)2 – 2 r2

Hence

di+1‐ di = 2(xi+1+ 1)2 + y2i+1+ (yi+1 ‐ 1)2

– 2(xi+ 1)2 – y2i – (yi – 1)2

Since xi+1= xi + 1 so,

d d 4 2( 2 2 ) 2( ) 6di+1 = di + 4xi + 2(y2i+1– y2i) – 2(yi+1 ‐ yi) + 6

Page 52: CE-201CE-201_Week 5-7_2010

BRESENHAM’S ALGORITHM…4BRESENHAM S ALGORITHM…4

• If T is the chosen pixel (di< 0) then yi+1=yi and sop ( i ) yi+1 yidi+1 = di+ 4xi+ 6

• If S is the chosen pixel (di ≥ 0) then yi+1 = yi‐1 and sodi+1 = di + 4(xi–yi) + 10

• Set (0, r) to be the starting pixel coordinates andcompute the base case value d :compute the base case value di :

d1 = 2 (0 + 1)2 + r2 + (r – 1)2 – 2r2d1 2 (0 + 1) + r + (r 1) 2r= 3‐ 2r

Page 53: CE-201CE-201_Week 5-7_2010

ExampleExample

• Given a radius of 10 units.e a ad us o 0 u s

• Compute di = 2(xi+1)2 + yi2+(yi‐1)2‐2r2.

• Set the value as (0,r) as the start point ( , ) p

d0 = 2(0+1)2 + 102 + 10‐1)2 ‐ 2x1020

= 2+100+81‐200

= 183‐200

= ‐17.

Page 54: CE-201CE-201_Week 5-7_2010

Example…1Example…1

• As d0 < 0, yi+1=yi 1As d0 < 0,  yi+1 yi‐1.• So next point (1,10)

d1 = d0 + 4xi + 6.d1 d0 4xi  6.

= ‐ 17+ 4 + 6

= ‐7

• So next point is (2, 10). 

d2 = d1 + 4x2+62 1

= ‐7+8+6.

=+7.7.

Page 55: CE-201CE-201_Week 5-7_2010

Example…2Example…2

• So next point (3, 9). So e po (3, 9)

d3 = di+4(xi‐yi)+10

= 7+4(3‐9)+10( )

= 7‐24+10

= ‐7.

• So next point (4, 9) 

d4 = ‐7+16+6 = 15.

• So next point (5, 8).

d5 = ‐15+4(5‐8)+10.

Page 56: CE-201CE-201_Week 5-7_2010

Example…3Example…3

• So next point (6,8).So e po (6,8)

d6 = ‐17+4x6+6

= +13.

So next point is (7,7)

Page 57: CE-201CE-201_Week 5-7_2010

Comparison

Iteration Mid point BresenhamIteration Mid point Bresenham

0 (0,10) (0,10)

1 (1 10) (1 10)1 (1,10) (1,10)

2 (2,10) (2,10)

3 (3 10) (3 9)3 (3,10) (3,9)

4 (4,9) (4,9)

5 (5,9) (5,8)

6 (6,8) (6,8)

7 (7,7) (7,7)

Page 58: CE-201CE-201_Week 5-7_2010

ReadingsReadings

• Hearn and Baker 104 – 122Hearn and Baker, 104  122

• Xiang and Plastock,