Вы находитесь на странице: 1из 21

Example on Bresenham Line Algorithm

(30,18)

Draw a line from (20,10) to (30,18)

dx = 10, dy = 8
initial decision d0 = 2dy dx =
6
2(dy dx) = -4
iAlso
d 2dy
(x =
,y 16,
)
i

0
1
2
3
4
5
6
7
8
9

6
2
-2
14
10
6
2
-2
14
10

i+1

i+1

(21,11)
(22,12)
(23,12)
(24,13)
(25,14)
(26,15)
(27,16)
(28,16)
(29,17)
(30,18)

(20,10)

19
18
17
16
15
14
13
12

Special cases on Bresenhams


Line Algorithm
Special cases can be handled
separately
Horizontal lines (y = 0)
Vertical lines (x = 0)
Diagonal lines (|x| = |y|)

directly into the frame-buffer


without processing them through
the line-plotting algorithms.
2

Circle Generating
Algorithms

Circle Properties:
A set of points that are all at the same distance
(r) from a center point (xc,yc).
Expressed by Pythagorean equation in Cartesian
coordinates as:
(x xc )2 + (y - yc )2 = r2

Any given circle can be drawn on the origin(0,0)


and then moved to its actual position by:
Each calculated x on the circumference moved to
x+xc.
Each calculated y on the circumference is moved to
y+yc.

Circle Generating
Algorithms:
Standard Algorithm
Polar Algorithm
Symmetry Algorithm
Mid-point Algorithm
(xc,
yc )

(x, y)

Standard Algorithm:
Uses the Pythagorean equation to
find
the
points
along
the
circumference.
Steps along x axis by unit steps from
xc-r to xc+r and calculate y for each
step as:
y= yc r2-(xc-yc)2

It involves considerable computations


at each step.

Standard Algorithm:
Spacing between plotted pixels is not uniform.

Spacing can be adjusted by stepping on y


instead of x when the slop is greater than 1, but
such approach increasing computations.

Polar Algorithm:
Uses polar coordinates r and .
Calculate points by fixed regular step size
equations as:
x= xc + r * cos
y= yc + r * sin
Step size that is chosen for depends on the
application.
Larger separations can be connected by line
segments to approximate the circle path.

Polar Algorithm:
In raster displays, step size is 1/r which plots
pixel positions that are approximately one unit
apart.

x,y
r

Symmetry Algorithm:
Reduces computations by considering the
symmetry of circles in the eight octants in
the xy plane.
Generates all pixel positions
around a circle by calculating
only the points within the
sector from x = 0 to x = y.

Bresenhams Circle Algorithm


(CHK )

Consider only
45 90

General Principle
The circle function:

f circle ( x, y ) x 2 y 2 r 2
and
0 if (x,y) is inside the circle boundary

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


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

11

Bresenhams Circle Algorithm


yi

p1

p3

yi - 1

p2

D(si)
D(ti)

r
xi xi + 1
After point p1, do we choose p2 or p3?
12

Bresenhams Circle Algorithm


Define: D(si) = distance of p3 from circle
D(ti) = distance of p2 from circle
i.e. D(si) = (xi + 1)2 + yi2 r2
[always
+ve]
D(ti) = (xi + 1)2 + (yi 1)2 r2 [always -ve]
Decision Parameter pi = D(si) + D(ti)
so if pi < 0 then the circle is closer to p3 (point
above)
if pi 0 then the circle is closer to p2 (point
below)
13

The Algorithm
x0 = 0
y0 = r
p0 = [12 + r2 r2] + [12 + (r-1)2 r2] = 3
2r
if pi < 0 then
yi+1 = yi
pi+1 = pi + 4xi + 6

xi+1 = xi + 1

else if pi 0 then
yi+1 = yi 1
pi+1 = pi + 4(xi yi) + 10
Stop when xi yi and determine
symmetry points in the other octants14

Mid-point Algorithm:
Samples at unit intervals and uses decision parameter to
determine the closest pixel position to the specified circle
path at each step.
Along the circle section from x = 0 to x = y in the first
quadrant, the slope of the curve varies from 0 to -1.
Therefore, we can take unit steps in the positive x
direction over this octant and use a decision parameter to
determine which of the two possible y positions is closer
to the circle path at each step. Positions in the other
seven octants are then obtained by symmetry.

Mid-point Algorithm:
To apply mid-point method, we define a circle
function:

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

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


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

and determine the nearest y position from pk:

Mid-point Algorithm:
If pk < 0, this midpoint is inside the circle and the
pixel on scan line yk is closer to the circle boundary.
Otherwise, the midposition is outside or on the circle
boundary, and we select the pixel on scan line yk 1 .
Successive parameters
incremental calculations:

are

calculated

using

Mid-point Algorithm:
Example:
For a circle radius=10 and center (12,12)
we calculate positions in the first octant only , beginning
with x=0 and ending when x=y
The initial decision parameter value:
P0=1 - r = -9
Drawing on the origin (0,0) and the initial point (0,10)
2x0=0 2y0=20

Mid-point Algorithm:
K P (xk+1,yk+1)
2xk+1
2yk+1
0 -9 (1,10) 2
20
1 -6 (2,10) 4
20
2 -1 (3,10) 6
20
3 6
(4,9)
8
18
4 -3 (5,9)
10
18
5 8
(6,8)
12
16
6 5 (7,7)
14
14
The
first
octant
is
completed
and
the
remained seven octants
can be plotted according
to symmetry.

2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9
8

2 3

4 5 6

7 8

9 10 11 12 13 14 15 16 17 18 19 20

Mid-point Algorithm:
The circle after plotting
all octants and moving it
to its original center
(12,12)

2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9
8

2 3

4 5 6

7 8

9 10 11 12 13 14 15 16 17 18 19 20

Example
r = 10
p0 = 3 2r = -17
Initial point (x0, y0) = (0, 10)
i

pi

xi, yi

-17

(0, 10)

-11

(1, 10)

10
9

-1

(2, 10)

13

(3, 10)

-5

(4, 9)

15

(5, 9)

6
7

(6, 8)
(7,7)

21

Вам также может понравиться