digitized by - Internet Archive...digitized by . digitized by . digitized by
linedrawingBresenhamCirclesAndPolygons by kumbhkar
Transcript of linedrawingBresenhamCirclesAndPolygons by kumbhkar
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
1/47
1of39
Contents
In todays lecture well have a look at: Bresenhams line drawing algorithm
Line drawing algorithm comparisons
Circle drawing algorithms A simple technique
The mid-point circle algorithm
Polygon fill algorithms
Summary of raster drawing algorithms
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
2/47
2of39
The Bresenham Line Algorithm
The Bresenham algorithm isanother incremental scanconversion algorithm
The big advantage of thisalgorithm is that it uses onlyinteger calculations
J a c k B r e s e n h a mworked for 27 years atIBM before enteringacademia. Bresenhamdeveloped his famousalgorithms at IBM int h e e a r l y 1 9 6 0 s
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
3/47
3of39
The Big Idea
Move across thex axis in unit intervals andat each step choose between two differentycoordinates
2 3 4 5
2
4
3
5 For example, fromposition (2, 3) wehave to choosebetween (3, 3) and(3, 4)
We would like thepoint that is closer to
the original line
(xk
,yk
)
(xk+1,yk)
(xk+1,yk+1)
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
4/47
4of39
They coordinate on the mathematical line at
xk+1 is:
Deriving The Bresenham Line Algorithm
At sample positionxk+1 the verticalseparations from the
mathematical line arelabelled dupperand dlower
bxmy k )1(
y
yk
yk+1
xk+1
dlower
dupper
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
5/47
5of39
So, dupperand dlowerare given as follows:
and:
We can use these to make a simple decisionabout which pixel is closer to the mathematical
line
Deriving The Bresenham Line Algorithm(cont)
klower yyd
kk ybxm
)1(
yyd kupper )1(
bxmy kk )1(1
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
6/47
6of39
This simple decision is based on the differencebetween the two pixel positions:
Lets substitute m with y/xwhere x and
y are the differences between the end-points:
Deriving The Bresenham Line Algorithm(cont)
122)1(2 byxmdd kkupperlower
)122)1(2()(
byxx
yxddx kkupperlower
)12(222 bxyyxxy kk
cyxxy kk 22
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
7/47
7of39
So, a decision parameterpk for the kth stepalong a line is given by:
The sign of the decision parameterpk is the
same as that of dlower
dupper
Ifpk is negative, then we choose the lowerpixel, otherwise we choose the upper pixel
Deriving The Bresenham Line Algorithm(cont)
cyxxy
ddxp
kk
upperlowerk
22
)(
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
8/47
8of39
Remember coordinate changes occur alongthex axis in unit steps so we can doeverything with integer calculations
At step k+1 the decision parameter is givenas:
Subtractingpk from this we get:
Deriving The Bresenham Line Algorithm(cont)
cyxxyp kkk 111 22
)(2)(2 111 kkkkkk yyxxxypp
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
9/47
9of39
But,xk+1 is the same asxk+1 so:
whereyk+1 - yk is either 0 or 1 depending onthe sign ofpkThe first decision parameter p0 is evaluated
at (x0, y0) is given as:
Deriving The Bresenham Line Algorithm(cont)
)(22 11 kkkk yyxypp
xyp 20
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
10/47
10of39
The Bresenham Line Algorithm
BRESENHAMS LINE DRAWING ALGORITHM(for |m| < 1.0)
1. Input the two line end-points, storing the left end-point
in (x0, y0)
2. Plot the point (x0, y0)
3. Calculate the constants x, y, 2y, and (2y - 2x)and get the first value for the decision parameter as:
4. At eachxkalong the line, starting at k = 0, perform the
following test. Ifpk< 0, the next point to plot is
(xk+1, yk) and:
xyp 20
ypp kk 21
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
11/47
11of39
The Bresenham Line Algorithm (cont)
ACHTUNG! The algorithm and derivationabove assumes slopes are less than 1. forother slopes we need to adjust the algorithmslightly
Otherwise, the next point to plot is (xk+1, yk+1) and:
5. Repeat step 4 (x 1) times
xypp kk 221
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
12/47
12of39
Bresenham Example
Lets have a go at thisLets plot the line from (20, 10) to (30, 18)
First off calculate all of the constants:
x: 10
y: 8
2y: 16
2y - 2x: -4
Calculate the initial decision parameterp0:
p0= 2yx = 6
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
13/47
13of39
Bresenham Example (cont)
17
16
15
14
13
12
11
10
18
292726252423222120 28 30
k pk (xk+1,yk+1)
0
1
2
3
4
56
7
8
9
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
14/47
14of39
Bresenham Exercise
Go through the steps of the Bresenham linedrawing algorithm for a line going from(21,12) to (29,16)
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
15/47
15of39
Bresenham Exercise (cont)
17
16
15
14
13
12
11
10
18
292726252423222120 28 30
k pk (xk+1,yk+1)
0
1
2
3
4
56
7
8
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
16/47
16of39
Bresenham Line Algorithm Summary
The Bresenham line algorithm has thefollowing advantages:
An fast incremental algorithm
Uses only integer calculationsComparing this to the DDA algorithm, DDAhas the following problems:
Accumulation of round-off errors can makethe pixelated line drift away from what wasintended
The rounding operations and floating point
arithmetic involved are time consuming
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
17/47
17of39
A Simple Circle Drawing Algorithm
The equation for a circle is:
wherer
is the radius of the circle
So, we can write a simple circle drawing
algorithm by solving the equation fory at
unitx intervals using:
222ryx
22 xry
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
18/47
18of39
A Simple Circle Drawing Algorithm(cont)
20020 220 y
20120 221 y
20220 222 y
61920 2219 y
02020 2220 y
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
19/47
19of39
A Simple Circle Drawing Algorithm(cont)
However, unsurprisingly this is not a brilliantsolution!
Firstly, the resulting circle has large gapswhere the slope approaches the vertical
Secondly, the calculations are not veryefficient
The square (multiply) operations
The square root operation try really hard toavoid these!
We need a more efficient, more accurate
solution
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
20/47
20of39
Eight-Way Symmetry
The first thing we can notice to make our circledrawing algorithm more efficient is that circlescentred at (0, 0) have eight-way symmetry
(x, y)
(y, x)
(y, -x)
(x, -y)(-x, -y)
(-y, -x)
(-y, x)
(-x, y)
2
R
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
21/47
21of39
Mid-Point Circle Algorithm
Similarly to the case with lines,there is an incrementalalgorithm for drawing circles
the mid-point circle algorithmIn the mid-point circle algorithmwe use eight-way symmetry so
only ever calculate the pointsfor the top right eighth of acircle, and then use symmetryto get the rest of the points
The mid-point circle
a l g o r i t h m w a sdeveloped by JackBresenham, who weheard about earlier.Bresenhams patent
for the algorithm canb e v i e w e d h e r e .
http://patft.uspto.gov/netacgi/nph-Parser?u=%2Fnetahtml%2Fsrchnum.htm&Sect1=PTO1&Sect2=HITOFF&p=1&r=1&l=50&f=G&d=PALL&s1=4371933.PN.&OS=PN/4371933&RS=PN/4371933http://patft.uspto.gov/netacgi/nph-Parser?u=%2Fnetahtml%2Fsrchnum.htm&Sect1=PTO1&Sect2=HITOFF&p=1&r=1&l=50&f=G&d=PALL&s1=4371933.PN.&OS=PN/4371933&RS=PN/4371933 -
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
22/47
22of39
Mid-Point Circle Algorithm (cont)
(xk+1, yk)
(xk+1, yk-1)
(xk, yk)Assume that we havejust plotted point (xk, yk)
The next point is a
choice between (xk+1, yk)and (xk+1, yk-1)
We would like to choose
the point that is nearest tothe actual circle
So how do we make this choice?
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
23/47
23of39
Mid-Point Circle Algorithm (cont)
Lets re-jig the equation of the circle slightlyto give us:
The equation evaluates as follows:
By evaluating this function at the midpointbetween the candidate pixels we can make
our decision
222),( ryxyxfcirc
,0
,0
,0
),( yxfcirc
boundarycircletheinsideis),(if yx
boundarycircleon theis),(if yx
boundarycircletheoutsideis),(if yx
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
24/47
24of39
Mid-Point Circle Algorithm (cont)
Assuming we have just plotted the pixel at(xk,yk) so we need to choose between(xk+1,yk) and (xk+1,yk-1)
Our decision variable can be defined as:
Ifpk< 0 the midpoint is inside the circle andand the pixel atyk is closer to the circle
Otherwise the midpoint is outside andyk-1 is
closer
222 )2
1()1(
)2
1,1(
ryx
yxfp
kk
kkcirck
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
25/47
25of39
Mid-Point Circle Algorithm (cont)
To ensure things are as efficient as possiblewe can do all of our calculationsincrementally
First consider:
or:
whereyk+1 is eitherykoryk-1 depending on
the sign ofpk
2212111
21]1)1[(
21,1
ryx
yxfp
kk
kkcirck
1)()()1(2 122
11 kkkkkkk yyyyxpp
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
26/47
26of39
Mid-Point Circle Algorithm (cont)
The first decision variable is given as:
Then ifpk< 0 then the next decision variable
is given as:
Ifpk> 0 then the decision variable is:
r
rr
rfp circ
45
)
2
1(1
)2
1,1(
22
0
12 11 kkk xpp
1212 11 kkkk yxpp
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
27/47
27of39
The Mid-Point Circle Algorithm
MID-POINT CIRCLE ALGORITHM Input radius rand circle centre (xc, yc), then set the
coordinates for the first point on the circumference of acircle centred on the origin as:
Calculate the initial value of the decision parameter as:
Starting with k = 0 at each positionxk, perform thefollowing test. Ifpk< 0, the next point along the circlecentred on (0, 0) is (xk+1, yk) and:
),0(),( 00 ryx
rp 4
50
1211
kkk
xpp
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
28/47
28of39
The Mid-Point Circle Algorithm (cont)
Otherwise the next point along the circle is (xk+1, yk-1)and:
4. Determine symmetry points in the other seven octants5. Move each calculated pixel position (x, y) onto the
circular path centred at (xc, yc) to plot the coordinate
values:
6. Repeat steps 3 to 5 untilx >= y
111 212 kkkk yxpp
cxxx cyyy
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
29/47
29of39
Mid-Point Circle Algorithm Example
To see the mid-point circle algorithm inaction lets use it to draw a circle centred at(0,0) with radius 10
Mid P i t Ci l Al ith E l
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
30/47
30of39
Mid-Point Circle Algorithm Example(cont)
9
76
5
4
3
2
1
0
8
976543210 8 10
10 k pk (xk+1,yk+1) 2xk+1 2yk+1
0
1
2
3
4
5
6
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
31/47
31of39
Mid-Point Circle Algorithm Exercise
Use the mid-point circle algorithm to drawthe circle centred at (0,0) with radius 15
Mid P i t Ci l Al ith E l
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
32/47
32of39
Mid-Point Circle Algorithm Example(cont)
k pk (xk+1,yk+1) 2xk+1 2yk+1
0
1
2
3
4
5
6
78
9
10
11
12
9
7
6
54
3
2
1
0
8
976543210 8 10
10
131211 14
15
13
12
14
11
16
15 16
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
33/47
33of39
Mid-Point Circle Algorithm Summary
The key insights in the mid-point circlealgorithm are:
Eight-way symmetry can hugely reduce the
work in drawing a circle Moving in unit steps along the x axis at each
point along the circles edge we need to
choose between two possible y coordinates
34
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
34/47
34of39
Filling Polygons
So we can figure out how to draw lines andcircles
How do we go about drawing polygons?
We use an incremental algorithm known asthe scan-line algorithm
35
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
35/47
35of39
Scan-Line Polygon Fill Algorithm
2
4
6
8
10 Scan Line
02 4 6 8 10 12 14 16
36
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
36/47
36of39
Scan-Line Polygon Fill Algorithm
The basic scan-line algorithm is as follows: Find the intersections of the scan line with all
edges of the polygon
Sort the intersections by increasing xcoordinate
Fill in all pixels between pairs of intersectionsthat lie interior to the polygon
37 Scan Line Polygon Fill Algorithm
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
37/47
37of39
Scan-Line Polygon Fill Algorithm(cont)
38
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
38/47
38of39
Line Drawing Summary
Over the last couple of lectures we havelooked at the idea of scan converting lines
The key thing to remember is this has to be
FASTFor lines we have either DDA or Bresenham
For circles the mid-point algorithm
39
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
39/47
39of39
Anti-Aliasing
40
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
40/47
40of39
Summary Of Drawing Algorithms
41
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
41/47
41of39
Mid-Point Circle Algorithm (cont)
6
2 3 41
5
4
3
42
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
42/47
42of39
Mid-Point Circle Algorithm (cont)
M
6
2 3 41
5
4
3
43
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
43/47
43of39
Mid-Point Circle Algorithm (cont)
M
6
2 3 41
5
4
3
44
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
44/47
44of39
Blank Grid
45
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
45/47
45of39
Blank Grid
46
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
46/47
46of39
Blank Grid
9
7
6
5
4
3
2
1
0
8
976543210 8 10
10
47
-
7/31/2019 linedrawingBresenhamCirclesAndPolygons by kumbhkar
47/47
47of39
Blank Grid