Circle Drawing Parametric equation radius R , center at origin is a - - PowerPoint PPT Presentation

circle drawing
SMART_READER_LITE
LIVE PREVIEW

Circle Drawing Parametric equation radius R , center at origin is a - - PowerPoint PPT Presentation

Circle Drawing Parametric equation radius R , center at origin is a parameter in [0..2) x = R*cos y = R*sin Algorithm based on the parametric equation: def drawCircleParam(radius, color): step = ??? for alpha in [0 : 2*M_PI] by


slide-1
SLIDE 1

Circle Drawing

Parametric equation – radius R, center at origin x = R*cos α α is a parameter in [0..2π) y = R*sin α Algorithm based on the parametric equation:

def drawCircleParam(radius, color): step = ??? for alpha in [0 : 2*M_PI] by step: x = radius*cos(alpha) y = radius*sin(alpha) setPixel(x, y, color)

What step to choose for the angle – small for big radius, big for small radius? Uses double and expensive operations cos,sin

slide-2
SLIDE 2

Results Parametric Eq.

slide-3
SLIDE 3

Circle Drawing

Implicit equation – radius R, center at origin x2 + y2 - R2 = 0 Explicit equation – solve for y: y = ±√ (R2 - x2) Algorithm based on the explicit equation:

def drawCircleExpl(radius, color): for x in [-radius : radius]: y = sqrt(radius*radius - x*x) setPixel(x, y, color) setPixel(x, -y, color)

Draws points densely around origin, leaves gaps farther away Uses double and expensive operation sqrt

slide-4
SLIDE 4

Results Explicit Eq.

slide-5
SLIDE 5

Midpoint Algorithm for Circles

Use similar idea as in the line drawing algorithm midpoint determines which pixel to intensify Again will use the explicit equation: F(x, y) = x2 + y2 - R2 F(x, y) = 0 point is on the circle F(x, y) < 0 point inside circle F(x, y) > 0 point outside circle Will also use the fact that there is 8-point symmetry:

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

slide-6
SLIDE 6

Midpoint Algorithm for Circles

Use similar idea as in the line drawing algorithm midpoint determines which pixel to intensify M MSE M ME midpoint M outside circle – intensify pixel below M, consider point MSE next midpoint M inside circle – intensify pixel above M, consider point ME next

slide-7
SLIDE 7

Circle Drawing (Midpoint)

If we know the value at F(M) can we calculate efficiently F(ME) and F(MSE) Calculating the value F(ME) Calculating the value F(MSE) M ME M MSE Coordinates: M(xm,ym) ME(xm+1,ym) F(ME) = (xm+1)2 + ym

2 - R2 = ?

Coordinates: M(xm,ym) ME(xm+1,ym-1) F(MSE) = (xm+1)2 + (ym-1)2 - R2 = ?

slide-8
SLIDE 8

Circle Drawing (Midpoint)

If we know the value at F(M) can we calculate efficiently F(ME) and F(MSE) Calculating the value F(ME) Coordinates: M(xm,ym) ME(xm+1,ym) F(ME) = (xm+1)2 + ym

2 - R2 =

= xm

2 + 2xm + 1 + ym 2 - R2 =

= xm

2 + ym 2 - R2 + 2xm + 1 =

= F(M) + 2xm + 1 Calculating the value F(MSE) Coordinates: M(xm,ym) ME(xm+1,ym-1) F(MSE) = (xm+1)2 + (ym-1)2 - R2 = = xm

2 + 2xm + 1 + ym 2 - 2ym + 1 - R2 =

= xm

2 + ym 2 - R2 + 2xm - 2ym + 2 =

= F(M) + 2xm - 2ym + 2 M ME M MSE

slide-9
SLIDE 9

Circle Drawing (Midpoint)

Need initial value of F(M) to start the algorithm

M P0

M P0 Coordinates of M = ? F(M) = ?

slide-10
SLIDE 10

Circle Drawing (Midpoint)

Need initial value of F(M) to start the algorithm

M P0

M P0 Coordinates of M = (1, R – ½) F(M) = 12 + (R – ½)2 - R2 = 5/4 - R

slide-11
SLIDE 11

The Algorithm

Inefficient version as first attempt: always computes F(M) = d = xm2 + ym2 - R2

def drawCircle(R, c): xp, yp = 0, R xm, ym = 1, R-0.5 circlePoints(xp, yp, c) while x > y: d = xm2 + ym2 - R2 # F(M) if d < 0: # going E xp, yp = xp+1, yp xm, ym = xm+1, ym else: # going SE xp, yp = xp+1, yp+1 xm, ym = xm+1, ym+1 circlePoints(x, y, c)

slide-12
SLIDE 12

The Algorithm

Inefficient version as first attempt: always computes F(M) = d = xm2 + ym2 - R2

def drawCircle(R, c): xp, yp = 0, R xm, ym = 1, R-0.5 circlePoints(xp, yp, c) while x > y: d = xm2 + ym2 - R2 # F(M) if d < 0: # going E xp, yp = xp+1, yp xm, ym = xm+1, ym else: # going SE xp, yp = xp+1, yp+1 xm, ym = xm+1, ym+1 circlePoints(x, y, c) def drawCircle(R, c): xp, yp = 0, R xm, ym = 1, R-0.5 d = 5/4 – R # F(M0) circlePoints(xp, yp, c) while x > y: d = xm2 + ym2 - R2 # F(M) if d < 0: # going E d += 2*xm + 1 xp, yp = xp+1, yp xm, ym = xm+1, ym else: # going SE d = 2*xm – 2*ym + 2 xp, yp = xp+1, yp+1 xm, ym = xm+1, ym+1 circlePoints(x, y, c)

Better version: incrementally updates F(M)

slide-13
SLIDE 13

The Algorithm

Since xp,yp and xm,ym move together in the same way can derive xm,ym from xp,yp

def drawCircle(R, c): xp, yp = 0, R xm, ym = 1, R-0.5 d = 5/4 – R # F(M0) circlePoints(xp, yp, c) while x > y: if d < 0: # going E d += 2*xm + 1 = 2*(xp+1) + 1 xp, yp = xp+1, yp xm, ym = xm+1, ym else: # going SE d = 2*xm – 2*ym + 2 = 2*(xp+1) – 2*(yp-.5) + 2 xp, yp = xp+1, yp+1 xm, ym = xm+1, ym+1 circlePoints(x, y, c)

xm = xp + 1 ym = yp – 0.5

slide-14
SLIDE 14

The Algorithm

Getting rid of the fraction 5/4

def drawCircle(R, c): xp, yp = 0, R d = 5/4 – R circlePoints(xp, yp, c) while x > y: if d < 0: # going E d += 2*xp + 3 xp, yp = xp+1, yp else: # going SE d = 2*xp – 2*yp + 5 xp, yp = xp+1, yp+1 circlePoints(x, y, c)

slide-15
SLIDE 15

The Algorithm

Getting rid of the fraction 5/4

def drawCircle(R, c): xp, yp = 0, R d = 5/4 – R – 1/4 = 1 – R # start d ¼ less than what it should be, # which makes d an integer value circlePoints(xp, yp, c); # # then the condition becomes: while x > y: # # if d < 0: # if d < -1/4: # going E # d += 2*xp + 3 # but since d is an integer and always xp, yp = xp+1, yp # updates by integers, it stays integer, else: # so to be < -1/4, it must be < 0 # going SE # d = 2*xp – 2*yp + 5 # thus, the conditions is unchanged xp, yp = xp+1, yp+1 circlePoints(x, y, c)

slide-16
SLIDE 16

Antialiasing for Circles

Use similar idea as in the line drawing algorithm intensify the selected vertex and its neighbors based on distance to circle Exact distance DP is harder to compute efficiently. Use the approximation F(P) ~ 2*DP*R Keep track of F(P) – same as F(M) i.e. same increments, different initial value Compute F(PN), F(PS) as we did for F(ME), F(MSE) and compute DP,DPN, DPS M PN P PS