cs 450: computer graphics drawing ellipses and other curves spring 2015 dr. michael j. reale

25
CS 450: COMPUTER GRAPHICS DRAWING ELLIPSES AND OTHER CURVES SPRING 2015 DR. MICHAEL J. REALE

Upload: javier-theall

Post on 14-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 450: COMPUTER GRAPHICS DRAWING ELLIPSES AND OTHER CURVES SPRING 2015 DR. MICHAEL J. REALE

CS 450: COMPUTER GRAPHICS

DRAWING ELLIPSES ANDOTHER CURVES

SPRING 2015

DR. MICHAEL J. REALE

Page 2: CS 450: COMPUTER GRAPHICS DRAWING ELLIPSES AND OTHER CURVES SPRING 2015 DR. MICHAEL J. REALE

OVERVIEW

• When dealing with other kinds of curves, we can draw it in one of the following ways:

• Use explicit form (i.e., y = f(x)) or a parametric form (i.e., y = f(s) and x = g(s)) approximate curve with line segments

• Parametric form better for this easy to get evenly-spaced points along curve path

• Can connect points OR use best-fitting line (linear regression or least squares approach)

• Use implicit form (i.e., f(x,y) = 0) and derive incremental midpoint method

• In this slide deck, we’ll cover:

• Ellipses with the midpoint method

• Conic sections (and broadly discuss how to draw them)

• Polynomials and spline curves (which we’ll cover in greater detail later)

Page 3: CS 450: COMPUTER GRAPHICS DRAWING ELLIPSES AND OTHER CURVES SPRING 2015 DR. MICHAEL J. REALE

ELLIPSES

Page 4: CS 450: COMPUTER GRAPHICS DRAWING ELLIPSES AND OTHER CURVES SPRING 2015 DR. MICHAEL J. REALE

INTRODUCTION

• Ellipses are basically elongated circles

• Formal definition: given two points F1 (x1 , y1 ) and F2 (x2 , y2 ) (the foci of the ellipses), a point (x,y) is on the ellipse if the sum of the distances from F1 and F2 is some constant c.

• Conceptually, this is like having a string whose ends are connected at F1 and F2 , while you use a pencil to trace out the ellipse.

• If we expand this out, we get:

• The general ellipse equation:

cdd 21

cyyxxyyxx 22

22

21

21 )()()()(

http://commons.wikimedia.org/wiki/File:Drawing_an_ellipse_via_two_tacks_a_loop_and_a_pen.jpg#mediaviewer/File:Drawing_an_ellipse_via_two_tacks_a_loop_and_a_pen.jpg

022 FEyDxCxyByAx

Page 5: CS 450: COMPUTER GRAPHICS DRAWING ELLIPSES AND OTHER CURVES SPRING 2015 DR. MICHAEL J. REALE

AXIS-ALIGNED ELLIPSES

• Ellipses have two axes:

• Major axis = line passing through both foci

• Minor axis = line perpendicular to major axis

• The semimajor and semiminor axes are like radii aligned with the major and minor axes, respectively

• If the ellipse axes are aligned with the coordinate axes (i.e., x and y), the equation we can use is a lot simpler:

• …where

• rx = semimajor/minor axis aligned with x axis

• ry = semimajor/minor axis aligned with y axis

1

22

y

c

x

c

r

yy

r

xx

Page 6: CS 450: COMPUTER GRAPHICS DRAWING ELLIPSES AND OTHER CURVES SPRING 2015 DR. MICHAEL J. REALE

MIDPOINT ELLIPSE ALGORITHM

• We’re going to assume our ellipse is aligned with the x and y axes

• Start at (0,ry )

• Increment in x and choose y UNTIL abs(slope) > 1.0

• Then, decrement in y and choose x

• Use symmetry to copy points to other 3 quadrants

Page 7: CS 450: COMPUTER GRAPHICS DRAWING ELLIPSES AND OTHER CURVES SPRING 2015 DR. MICHAEL J. REALE

DERIVING OUR ELLIPSE FORMULA

• We’re going to assume our ellipses is centered at (0,0)

• This won’t affect which pixels we choose

)by d(Multiplie 0),(

)by d(Multiplie 0

11100

01

2222222

222

222

2

2

2

22222

22

yyxxy

xx

y

x

yxyxyx

y

c

x

c

rrryrxryxF

rrr

yrx

r

y

r

x

r

y

r

x

r

y

r

x

r

yy

r

xx

Page 8: CS 450: COMPUTER GRAPHICS DRAWING ELLIPSES AND OTHER CURVES SPRING 2015 DR. MICHAEL J. REALE

OUR ELLIPSE FORMULA

0),( 222222 yxxy rryrxryxF

boundary ellipse theOUTSIDE is y)(x, if 0,

boundary ellipse theON is y)(x, if 0,

boundary ellipse theINSIDE is y)(x, if 0,

),( yxF

…which has the following properties:

Page 9: CS 450: COMPUTER GRAPHICS DRAWING ELLIPSES AND OTHER CURVES SPRING 2015 DR. MICHAEL J. REALE

GETTING THE SLOPE

• We’ll start by incrementing x, but we need to check the slope to make sure it isn’t <= -1.0

• To get the slope, we’ll do some implicit differentiation

yr

xr

dx

dy

yr

xdxrdy

xdxrydyr

ydyrxdxr

rrDyrDxrD

DrryrxrD

x

y

x

y

yx

xy

yxxy

yxxy

2

2

2

2

22

22

222222

222222

2

2

2

2

22

022

0)()()(

)0()(

Page 10: CS 450: COMPUTER GRAPHICS DRAWING ELLIPSES AND OTHER CURVES SPRING 2015 DR. MICHAEL J. REALE

TESTING THE SLOPE

• When the slope = -1.0 (at boundary of R1 and R2), then:

• When the slope < -1.0 (entering R2):

yrxr

yr

xr

dx

dy

xy

x

y

22

2

2

22

12

2

yrxr

yr

xr

xy

x

y

22

2

2

22

12

2

Page 11: CS 450: COMPUTER GRAPHICS DRAWING ELLIPSES AND OTHER CURVES SPRING 2015 DR. MICHAEL J. REALE

DECISION VARIABLE: REGION 1

• Our decision variable for region 1 is going to be based on where (xk + 1, yk – ½) is relative to the “true”

ellipse:

• Similar to the midpoint circle algorithm:

• If p1k < 0 inside ellipse choose yk

• Otherwise outside ellipse choose yk - 1

222

222

2

1)1(

2

1,11

yxkxky

kkk

rryrxr

yxFp

Page 12: CS 450: COMPUTER GRAPHICS DRAWING ELLIPSES AND OTHER CURVES SPRING 2015 DR. MICHAEL J. REALE

INCREMENTING THE DECISION VARIBLE: REGION 1• To figure out our increment, we look at the value of

p1k+1:

22

122

12

22

1222

22

122222

2222

22

1222

22222

22

1222

2222

222

12

22

2222

222

1222

222

1222

111

2

1

2

121

2

1

2

1)1(21

2

1

2

1)1(2

2

1)1(

2

1

2

1

2

1)122()1(

2

1

2

1

2

1)3212(

2

1

2

1

2

1)44(

2

1)2(

2

1)11((

2

1,11

kkxykyk

kkxykyk

kkxykyyxkxky

kkxyxkxkyky

kkxyxkxkkky

yxkxkxkxkky

yxkxky

yxkxkykkk

yyrrxrp

yyrrxrp

yyrrxrrryrxr

yyrrryrxrxr

yyrrryrxxxr

rryryryrxxr

rryrxr

rryrxryxFp

Page 13: CS 450: COMPUTER GRAPHICS DRAWING ELLIPSES AND OTHER CURVES SPRING 2015 DR. MICHAEL J. REALE

INCREMENTING THE DECISION VARIBLE: REGION 1

• If p1k < 0 chose yk+1 = yk

• Otherwise chose yk+1 = yk - 1

22

122

12

1 2

1

2

1211 kkxykykk yyrrxrpp

21

21 211 ykykk rxrpp

122

12

1 2211 kxykykk yrrxrpp

02

1

2

122

kk yy

1

222222

2)1(222

4

1

4

93

2

1

2

3

2

1

2

11

kkk

kkkkkkkk

yyy

yyyyyyyy

Page 14: CS 450: COMPUTER GRAPHICS DRAWING ELLIPSES AND OTHER CURVES SPRING 2015 DR. MICHAEL J. REALE

WHEN DO WE SWITCH TO REGION 2?

• We have to check when this expression becomes true:

• At the starting point (0,ry), these values are:

• So, every iteration:

• Add 2ry2 to px

• If we choose (y – 1), subtract 2rx2 from py

yrxr xy22 22

yxxy

yx

rryrp

xrp22

2

22

02

Page 15: CS 450: COMPUTER GRAPHICS DRAWING ELLIPSES AND OTHER CURVES SPRING 2015 DR. MICHAEL J. REALE

INITIAL DECISION VARIABLE: REGION 1

222

2222222

22222

222

222

0

4

14

1

4

1

2

1)10(

2

1,11

xyxy

yxxyxyxy

yxyyxy

yxyxy

y

rrrr

rrrrrrrr

rrrrrr

rrrrr

rFp

Page 16: CS 450: COMPUTER GRAPHICS DRAWING ELLIPSES AND OTHER CURVES SPRING 2015 DR. MICHAEL J. REALE

DECISION VARIABLE: REGION 2

• Decrementing in y

• To get next decision variable:

• If p2k > 0 outside choose xk increment p2k:

• Otherwise inside choose (xk + 1) increment p2k:

22222

2 12

11,

2

12 yxkxkykkk rryrxryxFp

22

1222

1 2

1

2

1)1(222 kkyxkxkk xxrryrpp

21

21 222 xkxkk ryrpp

1

22

2222

212

224

82

4

1

4

93

2

1

2

3

2

1

2

11

kk

kkkkkk

kkkk

xx

xxxxxx

xxxx

21

21

21 2222 ykxkxkk rxryrpp

Page 17: CS 450: COMPUTER GRAPHICS DRAWING ELLIPSES AND OTHER CURVES SPRING 2015 DR. MICHAEL J. REALE

INITIAL DECISION VARIBLE: REGION 2

• We’ll use the last point we had before switching to region 2:

• An alternative would be to start at (rx, 0) and go counterclockwise; we would then INCREMENT y and choose between x and (x – 1)

2220

22

02

000

12

1

1,2

12

yxxy rryrxr

yxFp

Page 18: CS 450: COMPUTER GRAPHICS DRAWING ELLIPSES AND OTHER CURVES SPRING 2015 DR. MICHAEL J. REALE

NONSTANDARD ELLIPSES

• We have two ways to deal with non-standard ellipses (i.e., ellipses that do not line up with the coordinate axes):

• 1) Adapt formulas to work over ENTIRE ellipse path

• 2) Get the pixels for the standard ellipse, but then transform the points (i.e., rotation matrix)

Page 19: CS 450: COMPUTER GRAPHICS DRAWING ELLIPSES AND OTHER CURVES SPRING 2015 DR. MICHAEL J. REALE

MIDPOINT ELLIPSE ALGORITHM: REGION 1

• 1. Input rx, ry, and ellipse center (xc, yc); get first point for ellipse centered on origin:

• 2. Get initial decision variable for region 1:

• 3. While (2ry2x < 2rx

2y)

• If p1k < 0 choose (xk + 1, yk)

• Otherwise choose (xk + 1, yk – 1)

),0(),( 00 yryx

2220 4

11 xyxy rrrrp

21

21 211 ykykk rxrpp

122

12

1 2211 kxykykk yrrxrpp

Page 20: CS 450: COMPUTER GRAPHICS DRAWING ELLIPSES AND OTHER CURVES SPRING 2015 DR. MICHAEL J. REALE

MIDPOINT ELLIPSE ALGORITHM: REGION 2

• 4. Get initial value of decision variable for region 2 from last position (x0, y0) calculated in region 1:

• 5. While (y != 0)

• If p2k > 0 choose (xk, yk – 1)

• Otherwise choose (xk + 1, yk – 1)

• 6. For both regions, define symmetry points: (x,y), (-x,y), (x,-y), and (-x,-y)

• 7. For each position (x,y), plot:

2220

22

02

0 12

12 yxxy rryrxrp

21

21 222 xkxkk ryrpp

21

21

21 2222 ykxkxkk rxryrpp

c

c

yyy

xxx

Page 21: CS 450: COMPUTER GRAPHICS DRAWING ELLIPSES AND OTHER CURVES SPRING 2015 DR. MICHAEL J. REALE

CONIC SECTIONS

Page 22: CS 450: COMPUTER GRAPHICS DRAWING ELLIPSES AND OTHER CURVES SPRING 2015 DR. MICHAEL J. REALE

CONIC SECTIONS

• A conic section (or conic) can be described with this second-degreeequation:

• Basically, it’s any (nondegenerate) curve generated by the intersection of a plane with a top and/or bottom of a double cone

• “Degenerate” conics would be things like points and straight lines

• The values of A,B,C,D,E, and F determine what kind of curve we want

• If we look at (B2 – 4AC):

022 FEyDxCxyByAx

http://mathworld.wolfram.com/ConicSection.html

hyperbola ,0

parabola ,0

circle)(or ellipse ,0

42 ACB

Page 23: CS 450: COMPUTER GRAPHICS DRAWING ELLIPSES AND OTHER CURVES SPRING 2015 DR. MICHAEL J. REALE

DRAWING CONIC SECTIONS

• Figure out where the curve is symmetrical

• Circles symmetrical across 8 octants

• Ellipses/Hyperbolas symmetrical across 4 quadrants

• Parabolas symmetrical across vertical axis

• Compute decision variable formulas for areas where slope is less than 1.0 and greater than 1.0

• Go through midpoint algorithm, checking when to switch because of slope

Page 24: CS 450: COMPUTER GRAPHICS DRAWING ELLIPSES AND OTHER CURVES SPRING 2015 DR. MICHAEL J. REALE

POLYNOMIALS AND SPLINE CURVES

Page 25: CS 450: COMPUTER GRAPHICS DRAWING ELLIPSES AND OTHER CURVES SPRING 2015 DR. MICHAEL J. REALE

POLYNOMIALS AND SPLINE CURVES

• The usual way to handle these is to break them in parametric form

• For example, given a third-order (cubic) polynomial:

• The parameter u varies from 0 to 1 (start and end of the curve)

• If you have a continuous curve formed with polynomial pieces spline curve (or just splines)

• We’ll talk about these later

• Usually you would draw these by approximating them with line segments (with small increments of u)

33

2210

33

2210

uauauaay

uauauaax

yyyy

xxxx