# 1

Chapter 3 Chapter 3

Output Primitives Output Primitives

Circle and Ellipse drawing Circle and Ellipse drawing

algorithms algorithms

What is a Circle? What is a Circle?

• Set of points that are

all at the same

distance r from a

center position (x

c

, y

c

).

• The distance

relationship can be

represented by the

Pythagorean theorem:

– (x - x

c

)

2

+ (y – y

c

)

2

= r

2

r

x

c

y

c

Using this equation to draw Using this equation to draw

a circle a circle

• Step along the x-axis in unit step from

x

c

–r to x

c

+r.

• Calculate y as:

– y = y

c

± √r

2

– (x

c

- x)

2

• Problems:

– Computationally very expensive.

– Spacing between pixels is not uniform.

– Another way to eliminate unequal spacing is

by using polar coordinate equations.

Using polar coordinates Using polar coordinates

• Calculate points along a circular

boundary using polar coordinates r

and θ.

• The equations for calculating x and y

coordinates are:

– x = x

c

+ r cos θ

– y = y

c

+ r sin θ

• Step from 0

0

to 90

0

.

2

Polar Coordinates Polar Coordinates

Symmetry of circles Symmetry of circles

Generate pixel

positions around a

circle by calculating

points within the

sector x = 0 to x = y

+

4

*

¯

· =

=

^

(2, 8)

(y, x)

(-2, 8)

(-y, x)

y

(8, 2)

(x, y)

x

(x, -y)

(8, -2)

(y, -x)

(2, -8)

(-y, -x)

(-2, -8)

(-x, -y)

(-8, -2)

(-8, 2)

(-x, y)

9

9

Midpoint Circle Algorithm Midpoint Circle Algorithm

• Sample at unit intervals and determine

the closest pixel position to the specified

circle path at each step.

• For a given radius r and center (x

c

,y

c

) on

the screen, we

– Calculate pixel positions around the circle path

centered at coordinate origin (0,0).

– The calculated pixel position (x,y) is then

translated as

• x’ = x + x

c

.

• y’ = y + y

c

.

Midpoint Circle Algorithm Midpoint Circle Algorithm

• Along the circle section from x = 0 to

x = y in the first quadrant, the slope

of the curve varies from 0 to -1.

• Take unit step in positive x direction

over this octant and use the decision

parameter to determine which of the

two possible y positions are closer to

the actual circle path at each step.

3

Circle function Circle function

• f

circle

(x,y) = x

2

+ y

2

– r

2

– 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.

– Circle function tests are performed for the

mid positions between pixels near the circle

path at each sampling step ¬ DECISION

PARAMETER

Candidate pixel positions Candidate pixel positions

• Assuming we have

plotted the pixel (x

k

,y

k

),

the next pixel position is:

– (x

k

+1,y

k

) ¬ EAST

– (x

k

+1,y

k

-1) ¬ SOUTH EAST

N

S

E W

SE

Decision parameter Decision parameter

• p

k

= f

circle

(x

k

+1,y

k

-1/2)

• p

k

= (x

k

+1)

2

+ (y

k

-1/2)

2

– r

2

– p

k

< 0, select y

k

– p

k

>= 0, select y

k

-1

4

Next Increments Next Increments

• p

k+1

= f

circle

(x

k+1

+1,y

k+1

-1/2)

• p

k+1

= p

k

+2(x

k

+ 1)+(y

2

k+1

– y

2

k

)–(y

k+1

– y

k

)+1

• If p

k

< 0, y

k+1

= y

k

– p

k+1

= p

k

+ 2(x

k

+ 1) + 1

• [as x

k

+1 = x

k+1

]

– p

k+1

= p

k

+ 2x

k+1

+ 1

• If p

k

>= 0, y

k+1

= y

k-1

– p

k+1

= p

k

+ 2(x

k

+ 1) + 1 – 2(y

k

– 1)

• [as x

k

+1 = x

k+1

]

– p

k+1

= p

k

+ 2x

k+1

+ 1 – 2y

k+1

Initial Decision parameter Initial Decision parameter

• Taking the point (0,r)

• p

0

= 1 + (r-1/2)

2

– r

2

• p

0

= 5/4 – r

• Round the value to make it an

integer

• p

0

= 1 - r

Midpoint Circle Algorithm Midpoint Circle Algorithm

• Input radius r and circle center (x

c

, y

c

), and obtain

the first point on the circumference of a circle

centered at the origin as

– (x

0

, y

0

) = (0, r)

• Calculate the initial value of the decision

parameter as

– p

0

= 5/4 - r

• At each point x

k

position, starting at k = 0,

perform the following test:

– If p

k

< 0, next point centered at (0, 0) is (x

k+1

, y

k

) and

– p

k+1

= p

k

+ 2x

k+1

+ 1

– Otherwise, the next point along the circle is (x

k

+ 1, y

k

–

1) and

– p

k+1

= p

k

+ 2x

k+1

+ 1 – 2y

k+1

– Where 2x

k+1

= 2x

k

+ 2 and 2y

k+1

= 2y

k

- 2

Midpoint Circle Algorithm Midpoint Circle Algorithm

• Determine symmetry points in the other

seven octants.

• Move (x, y) along the circular according to

the symmetry and plot:

– x = x + x

c

&

– y = y + y

c

• Repeat until x >= y

5

Example: r = 10 Example: r = 10

p p

0 0

= 1 = 1- -10 = 10 = - -9, 2x 9, 2x

0 0

= 0, 2y = 0, 2y

0 0

= 0 = 0

14 14 (7, 7) 5 6

16 12 (6, 8) 8 5

18 10 (5, 9) -3 4

18 8 (4, 9) 6 3

20 6 (3, 10) -1 2

20 4 (2, 10) -6 1

20 2 (1, 10) -9 0

2y

k+1

2x

k+1

(x

k+1

, y

k+1

) p

k

k

Selected pixel positions Selected pixel positions

0 1 2 3 4 5 6 7 8 9 10

0

1

2

3

4

5

6

7

8

9

1

0

y = x

What is an ellipse? What is an ellipse?

• Simply stated: an elongated circle.

• Definition: An ellipse is defined as a

set of points such that the sum of

the distance from two fixed points

(foci) is the same for all points.

• If the distances to the two foci from

any poiny P = (x, y) are d

1

& d

2

,

then

– d

1

+ d

2

= constant.

Ellipse with foci F Ellipse with foci F

1 1

and F and F

2 2

x

y

F

1

F

2

P = (x, y)

d

1

d

2

6

Major & Minor Axes Major & Minor Axes

• Major axis is the

straight line

segment extending

from one side of the

ellipse to the other.

• Minor axis spans

the shorter

dimension of the

ellipse, bisecting the

major axis at the

halfway position

between the foci.

x

y

y

c

x

c

r

x

r

y

semi-major and

semi-minor axes

Simple case: Simple case:

• Is when the major and minor axes

are oriented to align with the

coordinate axes.

• r

x

¬ semi-major axis.

• r

y

¬ semi-minor axis.

• Equation of the ellipse:

– (x – x

c

/r

x

)

2

+ (y – y

c

/r

y

)

2

= 1 . . . . . . . eq (1)

Polar coordinates Polar coordinates

• Using r

x

and r

y

and θ, we can

describe an ellipse using the

following equations:

– x = x

c

+ r

x

cos θ

– y = y

c

+ r

y

sin θ

• Unlike a circle (8-way symmetric), an

ellipse is 4-way symmetric.

• We can draw an ellipse in one

quadrant and then use 4-way

symmetry to draw points in the rest

of the quadrants.

4 4- -way symmetry way symmetry

7

Midpoint ellipse algorithm Midpoint ellipse algorithm

• Given parameters r

x

, r

y

and (x

c

, y

c

),

we determine points (x, y) for an

ellipse in a standard position

centered at the origin.

• Once we have done that, we can do

two things:

– Add x

c

and y

c

to translate the origin to

(x

c

, y

c

).

– If the ellipse was not aligned with the x-

y coordinates, then we can rotate about

the center to re-orient the major and

minor axes.

Midpoint algorithm (contd) Midpoint algorithm (contd)

• The algorithm is applied throughout

the 1

st

quadrant in two stages.

• The algorithm is devised for ellipses

with r

x

< r

y

, although it can be

generalized for all ellipses.

• What do we do?

– Divide the quadrant in 2 regions.

– Unit increment in x and once we have

reached the region boundary, we switch

and unit increment in y.

Ellipse function with Ellipse function with ( (x x

c c

,y ,y

c c

) = ) =

(0, 0) (0, 0)

• Using eq (1), we define the ellipse

function as:

– f

ellipse

(x, y) = r

y

2

x

2

+ r

x

2

y

2

– r

x

2

r

y

2

• Which has the following properties:

– f

ellipse

(x, y) < 0, if (x, y) is inside the

boundary

– f

ellipse

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

boundary

– f

ellipse

(x, y) > 0, if (x, y) is outside the

boundary