1 of 55

Computer Graphics

Dr.S.Sivakumar,Principal

C.P.A College, Bodinayakanur

*

1

2 of 55

Circle drawing algorithms

3 of 55

Where do we draw a circle???

Properties of a circle:

  • A circle is defined as a set of points that are all the given distance (xc,yc). This distance relationship is expressed by the pythagorean theorem in Cartesian coordinates as

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

  • We could use this equation to calculate the points on the circle circumference by stepping along x-axis in unit steps from xc-r to xc+r and calculate the corresponding y values at each position as

y = yc +(- ) (r2 – (xc –x )2)1/2

  • This is not the best method:
    • Considerable amount of computation
    • Spacing between plotted pixels is not uniform

4 of 55

Circle Generation

  • Circles can be approximated by a set of straight lines.�

But, how many lines do

we need for an acceptable

representation?

How do we determine end

points of lines?

5 of 55

Polar co-ordinates for a circle

  • We could use polar coordinates r and θ,

x = xc + r cosθ y = yc + r sinθ

  • A fixed angular step size can be used to plot equally spaced points along the circumference
  • A step size of 1/r can be used to set pixel positions to approximately 1 unit apart for a continuous boundary
  • But, note that circle sections in adjacent octants within one quadrant are symmetric with respect to the 45 deg line dividing the to octants
  • Thus we can generate all pixel positions around a circle by calculating just the points within the sector from x=0 to x=y
  • This method is still computationally expensive

6 of 55

7 of 55

A Simple Circle Drawing Algorithm

  • The equation for a circle is:

  • where r is the radius of the circle
  • So, we can write a simple circle drawing algorithm by solving the equation for y at unit x intervals using:

8 of 55

A Simple Circle Drawing Algorithm (cont…)

9 of 55

A Simple Circle Drawing Algorithm (cont…)

  • However, unsurprisingly this is not a brilliant solution!
  • Firstly, the resulting circle has large gaps where the slope approaches the vertical
  • Secondly, the calculations are not very efficient
    • The square (multiply) operations
    • The square root operation – try really hard to avoid these!
  • We need a more efficient, more accurate solution

10 of 55

Eight-Way Symmetry

  • The first thing we can notice to make our circle drawing algorithm more efficient is that circles centred at (0, 0) have eight-way symmetry

(x, y)

(y, x)

(y, -x)

(x, -y)

(-x, -y)

(-y, -x)

(-y, x)

(-x, y)

11 of 55

Mid-Point Circle Algorithm

  • Similarly to the case with lines, there is an incremental algorithm for drawing circles – the mid-point circle algorithm
  • In the mid-point circle algorithm we use eight-way symmetry so only ever calculate the points for the top right eighth of a circle, and then use symmetry to get the rest of the points

The mid-point circle algorithm was developed by Jack Bresenham, who we heard about earlier. Bresenham’s patent for the algorithm can be viewed here.

12 of 55

Mid-Point Circle Algorithm (cont…)

  • Assume that we have �just 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 to �the actual circle
  • So how do we make this choice?

(xk+1, yk)

(xk+1, yk-1)

(xk, yk)

13 of 55

Mid-Point Circle Algorithm (cont…)

  • the equation of the circle slightly to give :

  • The equation evaluates as follows:

  • By evaluating this function at the midpoint between the candidate pixels we can make our decision

14 of 55

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:

  • If pk < 0 the midpoint is inside the circle and and the pixel at yk is closer to the circle
  • Otherwise the midpoint is outside and yk-1 is closer

15 of 55

Midpoint Circle Drawing Algorithm

  • To determine the closest pixel position to the specified circle path at each step.
  • For given radius r and screen center position (xc, yc), calculate pixel positions around a circle path centered at the coodinate origin (0,0).
  • Then, move each calculated position (x, y) to its proper screen position by adding xc to x and yc to y.
  • Along the circle section from x=0 to x=y in the first quadrant, the gradient varies from 0 to -1.

16 of 55

Midpoint Circle Drawing Algorithm

  • 8 segments of octants for a circle:

17 of 55

Midpoint Circle Drawing Algorithm

  • Circle function:

fcircle (x,y) = x2 + y2 r2

> 0, (x,y) outside the circle

< 0, (x,y) inside the circle

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

{

fcircle (x,y) =

18 of 55

The Mid-Point Circle Algorithm

  • MID-POINT CIRCLE ALGORITHM
  • Input radius r and circle centre (xc, yc), then set the coordinates for the first point on the circumference of a circle centred on the origin as:

  • Calculate the initial value of the decision parameter as:

  • Starting with k = 0 at each position xk, perform the following test. If pk < 0, the next point along the circle centred on (0, 0) is (xk+1, yk) and:

19 of 55

The Mid-Point Circle Algorithm (cont…)

  • Otherwise the next point along the circle is (xk+1, yk-1) and:

  1. Determine symmetry points in the other seven octants

  • Move each calculated pixel position (x, y) onto the circular path centred at (xc, yc) to plot the coordinate values:

  • Repeat steps 3 to 5 until x >= y

20 of 55

Midpoint Circle Drawing Algorithm

Example:

Given a circle radius = 10, determine the circle octant in the first octant from x=0 to x=y.

Solution:

f0 = 5 – r

4

= 5 – 10

4

= -8.75

–9

21 of 55

Midpoint Circle Drawing Algorithm

k

Fk

x

y

2xk+1

2yk+1

0

-9

1

10

2

20

1

-9+2+1=-6

2

10

4

20

2

-6+4+1=-1

3

10

6

20

3

-1+6+1=6

4

9

8

18

4

6+8+1-18=-3

5

9

10

18

5

-3+10+1=8

6

8

12

16

6

8+12+1-16=5

7

7

14

14

Initial (x0, y0) = (1,10)

Decision parameters are: 2x0 = 2, 2y0 = 20

22 of 55

Midpoint Circle Drawing Algorithm

void circleMidpoint(int xCenter, int yCenter, int radius)

{

int x = 0;

Int y = radius;

int f = 1 – radius;

circlePlotPoints(xCenter, yCenter, x, y);

while (x < y) {

x++;

if (f < 0)

f += 2*x + 1;

else {

y--;

f += 2*(x-y)+1;

}

circlePlotPoints(xCenter, yCenter, x, y);

}

}

23 of 55

Midpoint Circle Drawing Algorithm

void circlePlotPoints( int xCenter, int yCenter,

int x, int y)

{

setPixel (xCenter + x, yCenter + y);

setPixel (xCenter – x, yCenter + y);

setPixel (xCenter + x, yCenter – y);

setPixel (xCenter – x, yCenter – y);

setPixel (xCenter + y, yCenter + x);

setPixel (xCenter – y, yCenter + x);

setPixel (xCenter + y, yCenter – x);

setPixel (xCenter – y, yCenter – x);

}

24 of 55

Mid-Point Circle Algorithm Example

  • To see the mid-point circle algorithm in action lets use it to draw a circle centred at (0,0) with radius 10

25 of 55

Mid-Point Circle Algorithm Example (cont…)

9

7

6

5

4

3

2

1

0

8

9

7

6

5

4

3

2

1

0

8

10

10

k

pk

(xk+1,yk+1)

2xk+1

2yk+1

0

1

2

3

4

5

6

26 of 55

Mid-Point Circle Algorithm Exercise

  • Use the mid-point circle algorithm to draw the circle centred at (0,0) with radius 15

27 of 55

Mid-Point Circle Algorithm Summary

  • The key insights in the mid-point circle algorithm 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 circle’s edge we need to choose between two possible y coordinates

28 of 55

  • Fixed angular step size to have equally spaced points

29 of 55

  • Computation can be reduced by considering symmetry of circles:
  • Still too complex,�multiplications,�trigonometric calculations

  • Bresenham's circle generation algorithm involves simple integer operations (comparing squares of pixel separation distances)
  • Midpoint Circle Algorithm avoids squaring and generates the same pixels as Bresenhams’s algorithm.

(x,y)

(x,-y)

(-x,-y)

(-x,y)

(-y,-x)

(y,-x)

(y,x)

(-y,x)

30 of 55

Midpoint Circle Algorithm

  • Consider the second octant.�Increment x , decide on y

�� select which of 2 pixels, (xk+1,yk) or (xk+1,yk-1)� are closer to the circle� by evaluating the circle function at the midpoint.

x

y

31 of 55

32 of 55

where yk+1 is either yk or yk–1 depending on the sign of pk.

if pk < 0 pk+1 = pk+ 2xk + 3

if pk ≥ 0 pk+1 = pk+ 2xk – 2yk + 5

computing p0 at (x0,y0) = (0,r)

if r is integer p0 = 1–r

33 of 55

Midpoint Circle Algorithm

Input: radius r and circle center (xc,yc)

draw(0+xc,r+yc) (add xc and yc before plotting)

pk←1–r; xk0; ykr;

while xk<yk

if pk < 0 choose yk

yk+1yk; pk+1pk+2xk+3

else choose yk –1

yk+1yk–1; pk+1pk + 2xk – 2yk + 5

xk+1xk+1

draw (xk+1+xc,yk+1+yc)

xkxk+1; ykyk+1;

pkpk+1

34 of 55

if pk < 0 choose yk

yk+1yk; pk+1pk+2xk+3

else choose yk –1

yk+1yk–1; pk+1pk + 2xk – 2yk + 5

35 of 55

Bresenham's to Midpoint

  • Bresenham's requires explicit equation
    • Not always convenient (many equations are implicit)
    • Based on implicit equations: Midpoint Algorithm (circle, ellipse, etc.)
    • Implicit equations have the form F(x,y)=0.

36 of 55

Midpoint Circle Algorithm

  • We will first calculate pixel positions for a circle centered around the origin (0,0). Then, each calculated position (x,y) is moved to its proper screen position by adding xc to x and yc to y

  • Note 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 by

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

  • Any point (x,y) on the boundary of the circle satisfies the equation and circle function is zero

37 of 55

38 of 55

Midpoint Circle Algorithm

  • For a point in the interior of the circle, the circle function is negative and for a point outside the circle, the function is positive
  • 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

yk

Yk-1

xk

xk+1

Xk+3

Midpoint

X2+y2-r2=0

Midpoint between candidate pixels at sampling position xk+1 along a circular path

39 of 55

Midpoint Circle Algorithm

  • Assuming we have just plotted the pixel at (xk,yk) , we next need to determine whether the pixel at position (xk + 1, yk-1) is closer to the circle
  • Our decision parameter is the circle function evaluated at the midpoint between these two pixels

pk = fcircle (xk +1, yk-1/2) = (xk +1)2 + (yk -1/2)2 – r2

If pk < 0 , this midpoint is inside the circle and the pixel on the scan line yk is closer to the circle boundary. Otherwise, the

mid position is outside or on the circle boundary, and we select the pixel on the scan line yk-1

40 of 55

Midpoint Circle Algorithm

  • Successive decision parameters are obtained using incremental calculations

Pk+1 = fcircle(xk+1+1, yk+1-1/2)

= [(xk+1)+1]2 + (yk+1 -1/2)2 –r2

OR

Pk+1 = Pk+2(xK+1) + (yK+12 – yk2) – (yk+1- yk)+1

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

  • Increments for obtaining Pk+1:

2xk+1+1 if pk is negative

2xk+1+1-2yk+1 otherwise

41 of 55

Midpoint circle algorithm

  • Note that following can also be done incrementally:

2xk+1 = 2xk +2

2 yk+1 = 2yk – 2

  • At the start position (0,r) , these two terms have the values 2 and 2r-2 respectively
  • Initial decision parameter is obtained by evaluating the circle function at the start position (x0,y0) = (0,r)

p0 = fcircle(1, r-1/2) = 1+ (r-1/2)2-r2

OR

P0 = 5/4 -r

  • If radius r is specified as an integer, we can round p0 to

p0 = 1-r

42 of 55

The actual algorithm

1: Input radius r and circle center (xc,yc) and obtain the first point on the circumference of the circle centered on the origin as

(x0,y0) = (0,r)

2: Calculate the initial value of the decision parameter as

P0 = 5/4 - r

3: At each xk position starting at k = 0 , perform the following 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

43 of 55

The algorithm

Otherwise the next point along the circle is (xk+1, yk-1) and

pk+1 = pk + 2xk+1 +1 -2yk+1

Where 2xk+1 = 2xk+2 and 2yk+1 = 2yk-2

4: Determine symmetry points in the other seven octants

5: Move each calculated pixel position (x,y) onto the circular path centered on (x,yc) and plot the coordinate values

x = x+ xc , y= y+ yc

6: Repeat steps 3 through 5 until x >= y

44 of 55

Circle Algorithms

  • Principle of the midpoint algorithm
    • Reason from octants of a circle centered in (0,0), then find the remaining octants by symmetry, then translate to (xc, yc).
    • The circle function is the decision parameter.
    • Calculate the circle function for the midpoint between two pixels.

45 of 55

Circle Algorithms

  • Midpoint circle drawing algorithm
    1. Input radius r and circle center (xc, yc), then set the coordinates for the first point on the circumference of a circle centered on the origin as (xo, y0) = (0, r).
    2. Calculate the initial value of the decision parameter as p0 = 5/4 – r (1 – r if an integer)
    3. At each xk, from k=0, perform the following test:�if pk<0, next point to plot along the circle centered on (0,0) is (xk + 1, yk) and pk+1 = pk + 2 xk+1 + 1�otherwise, next point to plot is (xk+ 1, yk - 1) � and pk+1 = pk + 2 xk+1 + 1 - 2 yk+1 �where 2 xk+1 = 2 xk + 2, and 2 yk+1 = 2 yk - 2

46 of 55

Circle Algorithms

  • Midpoint circle drawing algorithm
    1. Determine symmetry points in the other seven octants.
    2. Move each calculated pixel position (x, y) onto the circular path centered at (xc, yc) and plot the coordinate values:�x = x + xc, y = y + yc
    3. Repeat steps 3 through 5 until x >= y.

47 of 55

Curve Functions

  • Ellipsis can be drawn similarly using a modified midpoint algorithm.
  • Similar algorithms can be used to display polynomials, exponential functions, trigonometric functions, conics, probability distributions, etc.

48 of 55

Midpoint Ellipse

  • Derivation

49 of 55

50 of 55

51 of 55

52 of 55

Midpoint Ellipse Algorithm

  • Input and ellipse center and obtain the first point on an ellipse centered on the origin as

  • Calculate the initial value of the decision parameter in region 1 as

53 of 55

Midpoint Ellipse..

  • At each position in region 1, starting at k = 0, perform the following test. if , the next point along the ellipse centered on (0,0) is and

  • Otherwise, the next point along the ellipse is and

with

and continue until

54 of 55

Midpoint Ellipse Contd.

  • Calculate the initial value of the decision parameter in region 2 as

where is the last position calculated in region 1

  • At each position in region 2, starting at k=0, perform the following test. if , the next point along the ellipse centered on (0,0) is and

  • Otherwise, the next point along the ellipse is and

  • Using the same incremental calculations for x and y as in region 1. Continue until y=0

55 of 55

Midpoint Ellipse

  • For both regions, determine symmetry points in the other three quadrants
  • Move each calculated pixel position (x, y) onto the elliptical path that is centered on and plot the coordinate values