cg out put primitives

Upload: seethaiah-vootla

Post on 03-Jun-2018

234 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 Cg Out Put Primitives

    1/73

    Computer Graphics

    Chapter 3Graphics Output Primitives

    Andreas Savva

  • 8/11/2019 Cg Out Put Primitives

    2/73

    2

    Basic Raster Algorithms

    for 2D Primitives

    Description of picturesSpecified by a set of intensitiesfor the pixel positions.

    Describe it as a set of complex objects, such as trees,furniture and walls, positioned at specific coordinate

    locations within the scene.Graphics programming packages provide functions todescribe a scene in terms of basic geometric structuresreferred to as output primitives.

    Points

    Straight lines

    Circles

    Splines curves and surfaces

    Polygon color areas

    Character strings

    Etc.

  • 8/11/2019 Cg Out Put Primitives

    3/73

    3

    Implementing Application programs

    Description of objects in terms of primitives and

    attributes and converts them to the pixels on the

    screen.Primitiveswhat is to be generated

    Attributeshow primitives are to be generated

  • 8/11/2019 Cg Out Put Primitives

    4/73

    4

    Points

    The electron beam is turned on to illuminate the phosphor

    at the selected location (x, y) where

    0 x maxx

    0 y maxy

    setpixel(x, y, intensity)loads an intensity value into theframe-buffer at (x, y).

    getpixel(x, y)retrieves the current frame-buffer

    intensity setting at position (x, y).

    (0,0)

    (maxx,maxy)

    CRT

  • 8/11/2019 Cg Out Put Primitives

    5/73

    5

    Lines

    Analog devises, such as a random-scan display or avector plotter, display a straight line smoothly from one

    endpoint to another. Linearly varying horizontal and

    vertical deflection voltages are generated that are

    proportional to the required changes in the x and y

    directions to produce the smooth line.

  • 8/11/2019 Cg Out Put Primitives

    6/73

    6

    Digital devicesdisplay a straight line by plotting discrete

    coordinate points along the line path which are calculated

    from the equation of the line.

    Screen locations are referenced with integer values, so plotted

    positions may only approximate actual line positions between two

    specific endpoints.

    A computed line position of (10.48, 20.51) will be converted to

    pixel position (10, 21). This rounding of coordinate values to

    integers causes lines to be displayed with a stairstepappearance(the jaggies).

    Particularly noticeable on systems with low resolution.

    To smooth raster lines, pixel intensities along the line paths must

    be adjusted.

  • 8/11/2019 Cg Out Put Primitives

    7/73

    7

    Line Drawing Algorithms

    Cartesian equation:

    y = mx + c

    wheremslope

    cy-intercept

    x

    y

    xx

    yym

    12

    12

    x1

    y1

    x2

    y2

  • 8/11/2019 Cg Out Put Primitives

    8/73

    8

    Slope

    if |m| = 1

    = 45

    45

    45

    +ve -ve

    if |m| 1

    -45< < 45

    if |m| 1

    45< < 90 or

    -90< < -45

  • 8/11/2019 Cg Out Put Primitives

    9/73

    9

    8

    7

    6

    5

    4

    3

    2 1

    0

    0 1 2 3 4 5 6 7 8

    y = xm = 1c = 0 y

    x

    x y

    0 0

    1 12 2

    3 3

    4 4

    5 5

    6 6

    7 7

    8 8

    |m| = 1

  • 8/11/2019 Cg Out Put Primitives

    10/73

    10

    8

    7

    6

    5

    4

    3

    2

    1

    0

    0 1 2 3 4 5 6 7 8

    y = x+ 1m =

    c = 1y

    x

    x y round(y)

    0 1 1

    1 1.5 2

    2 2 2

    3 2.5 3

    4 3 3

    5 3.5 4

    6 4 4

    7 4.5 5

    8 5 5

    |m| 1

  • 8/11/2019 Cg Out Put Primitives

    11/73

    11

    |m| 1

    8

    7

    6

    5

    4

    3

    21

    0

    0 1 2 3 4 5 6 7 8

    y = 3x- 2m = 3

    c = -2y

    x

    x y round(y)

    0 -2 -2

    1 1 1

    2 4 4

    3 7 7

    4 10 10

    5 13 13

    6 16 16

    7 19 19

    8 22 22

    outside

  • 8/11/2019 Cg Out Put Primitives

    12/73

    12

    The Digital Differential Analyzer

    (DDA) Algorithm

    mx

    y

    x y0 1

    1 4

    2 7

    3 10

    4 135 16

    means that for a unit (1) change inxthere is

    m-changeiny.

    i.e. y= 3x+ 1 m= 3

    my

    x 1

    means that for a unit (1) change inythere is

    1/mchangeinx.

    Do not usey= 3x + 1

    to calculatey.

    Use m

  • 8/11/2019 Cg Out Put Primitives

    13/73

    13

    The DDA Method

    Uses differential equation of the line : m

    If slope |m| 1then incrementxin steps of 1 pixeland find correspondingy-values.

    If slope |m| 1then incrementyin steps of 1 pixel

    and find correspondingx-values.

    step through inx step through iny

  • 8/11/2019 Cg Out Put Primitives

    14/73

    14

    The DDA Method

    Desired line

    (xi,round(yi))

    (xi,yi)

    (xi+1,round(yi+m))

    (xi+1,yi+m)

  • 8/11/2019 Cg Out Put Primitives

    15/73

    15

    if slope m0

    if |m| 1

    xi+1=xi+ 1

    yi+1=yi+ m

    if |m| 1

    yi+1=yi+ 1

    xi+1=xi+ 1/m

    Left

    Right

    Left

    Right

  • 8/11/2019 Cg Out Put Primitives

    16/73

    16

    Proceeding from right-endpoint to left-endpoint

    if slope m0

    if |m| 1

    xi+1=xi- 1

    yi+1=yi- m

    if |m| 1

    yi+1=yi- 1

    xi+1=xi- 1/m

    Left

    Right

    Left

    Right

  • 8/11/2019 Cg Out Put Primitives

    17/73

    17

    if slope m< 0

    if |m| 1

    xi+1=xi+ 1

    yi+1=yi+ m

    if |m| 1

    yi+1=yi- 1

    xi+1=xi- 1/m

    Left

    Right

    Left

    Right

  • 8/11/2019 Cg Out Put Primitives

    18/73

    18

    Proceeding from right-endpoint to left-endpoint

    if slope m0

    if |m| 1

    xi+1=xi- 1

    yi+1=yi- m

    if |m| 1

    yi+1=yi+ 1

    xi+1=xi+ 1/m

    Left

    Right

    Left

    Right

  • 8/11/2019 Cg Out Put Primitives

    19/73

    19

    Example (DDA)

    31

    1

    131 1

    10

    1

    ii

    ii

    yyxx

    m

    xy

    x y round(y)

    0 1 11 4/3 1

    2 5/3 2

    3 2 2

    4 7/3 25 8/3 3

    6 3 3

    7 10/3 3

    8 11/3 4

    87

    6

    5

    4

    3

    2

    1

    0

    0 1 2 3 4 5 6 7 8

    y

    x

  • 8/11/2019 Cg Out Put Primitives

    20/73

    20

    Example (DDA)

    )(1

    1

    83

    31

    1

    1

    ii

    ii

    xxyy

    m

    xy

    y x round(x)

    8 0 07 1/3 0

    6 2/3 1

    5 1 1

    4 4/3 13 5/3 2

    2 2 2

    1 7/3 2

    0 8/3 3

    8 7

    6

    5

    4

    3

    2

    1

    0

    0 1 2 3 4 5 6 7 8

    y

    x

  • 8/11/2019 Cg Out Put Primitives

    21/73

    21

    voidLineDDA(intx0, inty0, intx1, inty1){

    intdx = x1x0, dy = y1y0, steps;

    if(abs(dx)>abs(dy)) steps = abs(dx);

    elsesteps = abs(dy);// one of these will be 1 or -1doublexIncrement = (double)dx / (double)steps;doubleyIncrement = (double)dy / (double)steps;

    doublex = x0;

    doubley = y0;setPixel(round(x), round(y));

    for(inti=0; i

  • 8/11/2019 Cg Out Put Primitives

    22/73

    22

    Example

    Draw a line from point (2,1) to (12,6)

    Draw a line from point (1,6) to (11,0)

    7

    6

    5

    4

    3

    2

    1

    0

    0 1 2 3 4 5 6 7 8 9 10 11 12

  • 8/11/2019 Cg Out Put Primitives

    23/73

    23

    Bresenham Line AlgorithmA more efficient approach

    Basis of the algorithm:

    From start position decide Aor Bnext

    A

    B

    Start position

  • 8/11/2019 Cg Out Put Primitives

    24/73

    24

    Bresenham Line Algorithm

    For a given value ofxone pixel lies at distance tiabove the line, andone pixel lies at distancesibelow the line

    True line

    siti

  • 8/11/2019 Cg Out Put Primitives

    25/73

    25

    Bresenham Line Algorithm

    Decision parameter

    di= (si- ti)

    If di0, then closest pixel is below true line (sismaller)

    If di0, then closest pixel is above true line (tismaller)

    We must calculate the new values for dias we move

    along the line.

  • 8/11/2019 Cg Out Put Primitives

    26/73

    26

    3dy2dy

    dy

    Example:

    )2or5.0(i.e.0.5(slope)gradientLet dxdydx

    dy

    Start pixel at (x0,y1)

    4dy

    At x1:s1= dy t1= dx- dy

    d1= (si- ti) = dy- (dx- dy) = 2dy- dx

    but 2dydxdi0ystays the same

    hence next pixel is at (x1,y1)

    At x2:s2= 2dy t2= dx- 2dy

    d2= (s2t2) = 2dy- (dx- 2dy) = 4dy- dx

    Supposed2 0yis incremented

    hence next pixel is at (x2,y2)

    At x3:s3= 3dy - dx t2= 2dx- 3dy

    d3= (s2t3) = 6dy- 3dx0

    soystays the same

    hence next pixel is at (x3,y2)

    x1 x2 x3 x4 x5x0y0

    y1

    y2

    y3

    y5

  • 8/11/2019 Cg Out Put Primitives

    27/73

    27

    In General For a line with gradient 1d0= 2dydx

    if di0 then yi+1=yi

    di+1= di+ 2dyif di0 then yi+1=yi+ 1

    di+1= di+ 2(dydx)

    xi+1=xi+ 1

    For a line with gradient 1d0= 2dxdy

    if di0 then xi+1=xidi+1= di+ 2dx

    if di0 then xi+1=xi+ 1di+1= di+ 2(dxdy)

    yi+1=yi+ 1

    Note: For |m| 1 the constants 2dyand 2(dy-dx) can be calculated once,

    so the arithmetic will involve only integer addition and subtraction.

    E l

  • 8/11/2019 Cg Out Put Primitives

    28/73

    28

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

    19

    18

    17

    16

    15

    14

    13

    12

    11

    10

    20 21 22 23 24 25 26 27 28 29 30 31 32

    (20,10)

    (30,18)dx= 10

    dy= 8

    initial decision d0= 2dydx= 6Also 2dy= 16, 2(dydx) = -4

    i di (xi+1,yi+1)

    0 6 (21,11)

    1 2 (22,12)

    2 -2 (23,12)

    3 14 (24,13)

    4 10 (25,14)

    5 6 (26,15)

    6 2 (27,16)7 -2 (28,16)

    8 14 (29,17)

    9 10 (30,18)

    id Li B (i t 0 i t 0 i t 1 i t 1) // li f | | 1

  • 8/11/2019 Cg Out Put Primitives

    29/73

    29

    voidLineBres(intx0, inty0, intx1, inty1) // line for |m| < 1{

    intdx = abs(x1x0), dy = abs(y1y0);intd = 2 * dydx, twoDy = 2 * dy, twoDyMinusDx = 2 * (dydx);intx, y;

    if(x0 > x1) { // determines which point to use as start, which as endx = x1;y = y1;x1 = x0;

    }else{

    x = x0;y = y0;}setPixel(x,y);

    while (x < x1) {x++;if (d < 0) d += twoDy;else{

    y++;d += twoDyMinusDx;

    }setPixel(x, y);

    }}

  • 8/11/2019 Cg Out Put Primitives

    30/73

    30

    Special cases

    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.

  • 8/11/2019 Cg Out Put Primitives

    31/73

    31

    Parallel Line Algorithms

    Take advantage of multiple processors.

    Given npprocessors, subdivide the line path into npBresenhamsegments.

    For a line with slope 0 m1 and leftpoint (x0,y0) the distance tothe right endpoint (left endpoint for next segment) is

    where

    x= width of the line

    xp

    is computed using integer division

    Numbering the segments, and the processors, as 0, 1, 2, , np-1,

    starting x-coordinate for the kthpartition is

    xk=x0+ kxp

    p

    pp

    nnxx 1

  • 8/11/2019 Cg Out Put Primitives

    32/73

    32

    Parallel Line Algorithms (continue)

    i.e. x= 15 , np= 4 processors

    Startingx-values atx0,x0+ 4,x0+ 8,x0+ 12

    yp= mxpAt the kthsegment, the starting y-coordinates is

    yk=y0+ round(kyp)Also, the initial decision parameter for Bresenhamsalgorithm at the start of the kthsubinterval is:

    pk= (kxp)(2y)round(kyp)(2x) + 2yx

    44

    18

    4

    1415

    px

    A i li i f i h li

  • 8/11/2019 Cg Out Put Primitives

    33/73

    33

    Anti-aliasing for straight lines

    Lines generated can have jagged or stair-step appearance,

    one aspect of phenomenon called aliasing, caused by factthat pixels are integer coordinate points.

    Use anti-aliasing routines to smooth out display of a line

    by adjusting pixels intensities along the line path

  • 8/11/2019 Cg Out Put Primitives

    34/73

    34

    Another raster effect

    Line B

    Line A

    Both lines plotted with the same number of pixels, but

    the diagonal line is longer than the horizontal line

    Visual effect is diagonal line appears less thick.

    If the intensity of each pixel isI, then the intensity per unit

    length of line A isI, whereas for line B it is onlyI/2; this

    discrepancy is easily detected by the viewer.

  • 8/11/2019 Cg Out Put Primitives

    35/73

    35

    Circle Generating Algorithms

    Circles and ellipses are common components in

    many pictures.

    Circle generation routines are often included inpackages.

  • 8/11/2019 Cg Out Put Primitives

    36/73

    36

    Circle Equations

    Polar formx= rCos

    y= rSin (r = radius of circle)

    P=(rCos, rSin)

    rSin)

    rCos)x

    y

    r

  • 8/11/2019 Cg Out Put Primitives

    37/73

    37

    Drawing a circle

    Disadvantages

    To find a complete circle varies from 0 to

    360

    The calculation of trigonometric functions

    is very slow.

    = 0while (< 360)

    x= rCosy = rSinsetPixel(x,y)= + 1

    end while

  • 8/11/2019 Cg Out Put Primitives

    38/73

    38

    Cartesian form

    Use Pythagoras theorem

    x2+ y2= r2

    x

    ry

    y

    x x

    y

    r

    2 2,P x r x

    Ci l l ith

  • 8/11/2019 Cg Out Put Primitives

    39/73

    39

    Circle algorithms Step throughx-axis to determiney-values

    Disadvantages:Not all pixel filled in Square root function is very slow

  • 8/11/2019 Cg Out Put Primitives

    40/73

    40

    Circle Algorithms

    Use 8-fold symmetry and only compute pixelpositions for the 45sector.

    45

    (x,y)

    (y,x)

    (-x,y)

    (y, -x)

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

    (-y,x)

    (-y, -x)

  • 8/11/2019 Cg Out Put Primitives

    41/73

    41

    Bresenhams Circle Algorithm

    General Principle

    The circle function:

    and

    Consider only

    45 90

    2 2 2( , )circlef x y x y r

    if (x,y) is inside the circle boundary

    if (x,y) is on the circle boundary

    if (x,y) is outside the circle boundary

    0( , ) 0

    0

    circlef x y

  • 8/11/2019 Cg Out Put Primitives

    42/73

    42

    Bresenhams Circle Algorithm

    p1 p3

    p2

    D(si)D(ti)

    After pointp1, do we choosep2orp3?

    yi

    yi- 1

    xi xi+ 1

    r

    B h Ci l Al i h

  • 8/11/2019 Cg Out Put Primitives

    43/73

    43

    Bresenhams Circle Algorithm

    Define:D(si) = distance ofp3from circleD(ti) = distance ofp2from circle

    i.e. D(si) = (x

    i+ 1)2+y

    i

    2r2 [always +ve]

    D(ti) = (xi+ 1)2+ (yi1)

    2r2 [always -ve]

    Decision Parameter pi= D(si) + D(ti)

    so ifpi< 0 then the circle is closer top3(point above)

    ifpi 0 then the circle is closer top2(point below)

    The Al ith

  • 8/11/2019 Cg Out Put Primitives

    44/73

    44

    The Algorithmx0= 0

    y0

    = r

    p0= [12+ r2r2] + [12+ (r-1)2r2] = 32r

    i f pi< 0 then

    yi+1= yipi+1= pi+ 4xi+ 6

    else if pi0 then

    yi+1= yi1pi+1= pi+ 4(xiyi)+ 10

    Stop when xi yiand determine symmetry

    points in the other octants

    xi+1 = xi+ 1

    Example

  • 8/11/2019 Cg Out Put Primitives

    45/73

    45

    Example

    10

    9

    8

    7

    6

    5

    4

    3 2

    1

    0

    0 1 2 3 4 5 6 7 8 9 10

    i pi xi, yi

    0 -17 (0, 10)

    1 -11 (1, 10)

    2 -1 (2, 10)

    3 13 (3, 10)

    4 -5 (4, 9)5 15 (5, 9)

    6 9 (6, 8)

    7 (7,7)

    r = 10

    p0= 32r = -17

    Initial point (x0,y0) = (0, 10)

  • 8/11/2019 Cg Out Put Primitives

    46/73

    46

    Exercises

    Draw the circle with r= 12 using the

    Bresenham algorithm.

    Draw the circle with r= 14 and center at

    (15, 10).

  • 8/11/2019 Cg Out Put Primitives

    47/73

    47

    Decision Parameters

    Prove that ifpi< 0 andyi+1=yithen

    pi+1=pi+ 4xi+ 6

    Prove that ifpi0 andyi+1=yi1 then

    pi+1

    =pi

    + 4(xi

    yi

    ) + 10

  • 8/11/2019 Cg Out Put Primitives

    48/73

    48

    Advantages of Bresenham circle

    Only involves integer addition, subtraction

    and multiplication

    There is no need for squares, square rootsand trigonometric functions

    Mid i Ci l Al i h

  • 8/11/2019 Cg Out Put Primitives

    49/73

    49

    Midpoint Circle Algorithm

    yi yi-1

    xi xi+1 xi+2

    Midpoint

    x2+y2r2= 0

    Assuming that we have just plotted the pixels at (xi , yi).Which is next? (xi+1, yi)OR (xi+1, yi1).

    - The one that is closer to the circle.

    Mid i Ci l Al i h

  • 8/11/2019 Cg Out Put Primitives

    50/73

    50

    Midpoint Circle Algorithm

    The decision parameter is the circle at the midpoint

    between the pixelsyiandyi1.

    Ifpi< 0, the midpoint is inside the circle and the pixel

    yiis closer to the circle boundary. Ifpi0, the midpoint is outside the circle and the pixel

    yi- 1 is closer to the circle boundary.

    12

    2 2 212

    ( 1, )

    ( 1) ( )

    i circle i i

    i i

    p f x y

    x y r

    D i i P t

  • 8/11/2019 Cg Out Put Primitives

    51/73

    51

    Decision Parameters

    Decision Parameters are obtained using

    incremental calculations

    OR

    whereyi+1is eitheryioryi-1 depending on the sign ofpi

    11 1 1 2

    2 2 21

    1 2

    ( 1, )

    ( 2) ( )

    i circle i i

    i i

    p f x y

    x y r

    2 2 2

    1 1 12( 1) ( ) ( ) 1i i i i i i ip p x y y y y

    Note:

    xi+1=xi+1

    Th Al ith1 Initial values: point(0 r)

  • 8/11/2019 Cg Out Put Primitives

    52/73

    52

    The Algorithm1. Initial values:- point(0,r)x0= 0

    y0= r

    2. Initial decision parameter

    3. At eachxiposition, starting at i = 0, perform the

    following test: ifpi< 0, the next point is (xi+ 1,yi) and

    pi+1=pi+ 2xi+1+ 1

    Ifpi 0, the next point is (xi+1,yi-1) and

    pi+1=pi+ 2xi+1+ 12yi+1

    where 2xi+1

    = 2xi+ 2 and 2y

    i+1= 2y

    i2

    4. Determine symmetry points in the other octants

    5. Move pixel positions (x,y) onto the circular path centered

    on (xc, yc) and plot the coordinates:x=x+xc,y=y+yc

    6. Repeat 35 untilxy

    move circle origin at (0,0) byx=xxcandy=yyc

    2 2 51 10 2 2 4

    (1, ) 1 ( )circlep f r r r r

    Example

  • 8/11/2019 Cg Out Put Primitives

    53/73

    53

    Example

    10

    9

    8

    7

    6

    5

    4

    3

    2

    1

    0

    0 1 2 3 4 5 6 7 8 9 10

    i pi xi+1, yi+1 2xi+1

    2yi+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)

    r = 10

    p0= 1r = -9(if ris integer roundp0= 5/4rto integer)

    Initial point (x0,y0) = (0, 10)

  • 8/11/2019 Cg Out Put Primitives

    54/73

    54

    Exercises

    Draw the circle with r= 12 using the

    Midpoint-circle algorithm

    Draw the circle with r= 14 and center at

    (15, 10).

  • 8/11/2019 Cg Out Put Primitives

    55/73

    55

    Exercises

    Prove that ifpi< 0 andyi+1=yithen

    pi+1=pi+ 2xi+1+ 1

    Prove that ifpi0 andyi+1=yi-1 then

    pi+1=pi+ 2xi+1+ 12yi+1

    Midpoint function

  • 8/11/2019 Cg Out Put Primitives

    56/73

    56

    Midpoint functionvoid plotpoints(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);

    }

    void circle(int r){

    int x = 0, y = r;plotpoints(x,y);int p = 1 r;while (x

  • 8/11/2019 Cg Out Put Primitives

    57/73

    57

    Ellipse-Generating Algorithms

    EllipseA modified circle whose radius varies from amaximum value in one direction (major axis) to a minimumvalue in the perpendicular direction (minor axis).

    P=(x,y)F1

    F2

    d1

    d2

    The sum of the two distances d1and d2, between the fixedpositionsF1andF2(called thefociof the ellipse) to anypointPon the ellipse, is the same value, i.e.

    d1+ d2= constant

    Ellipse Properties

  • 8/11/2019 Cg Out Put Primitives

    58/73

    58

    Ellipse Properties

    Expressing distances d1and d2in terms of the focal

    coordinatesF1= (x1,x2) andF2= (x2,y2), we have:

    Cartesian coordinates:

    Polar coordinates:

    2 2 2 2

    1 1 2 2( ) ( ) ( ) ( ) constantx x y y x x y y

    ry rx

    22

    1c c

    x y

    x x y yr r

    cos

    sin

    c x

    c y

    x x r

    y y r

  • 8/11/2019 Cg Out Put Primitives

    59/73

    59

    Ellipse Algorithms

    Symmetry between quadrants

    Not symmetric between the two octants of a quadrant

    Thus, we must calculate pixel positions along the

    elliptical arc through one quadrant and then we obtain

    positions in the remaining 3 quadrants by symmetry

    (x,y)(-x,y)

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

    rx

    ry

    Ellipse Algorithms

  • 8/11/2019 Cg Out Put Primitives

    60/73

    60

    Ellipse Algorithms

    Decision parameter:

    2 2 2 2 2 2( , )ellipse y x x y

    f x y r x r y r r

    1

    Slope = -1

    rx

    ry 2

    0 if ( , ) is inside the ellipse

    ( , ) 0 if ( , ) is on the ellipse0 if ( , ) is outside the ellipse

    ellipse

    x y

    f x y x yx y

    2

    2

    2

    2

    y

    x

    r xdySlope

    dx r y

    Ellipse Algorithms Slope = 1

  • 8/11/2019 Cg Out Put Primitives

    61/73

    61

    Ellipse Algorithms

    Starting at (0, ry) we take unit steps in thexdirection until

    we reach the boundary between region 1and region 2.

    Then we take unit steps in theydirection over theremainder of the curve in the first quadrant.

    At the boundary

    therefore, we move out of region 1whenever

    1

    Slope = -1

    rx

    ry 2

    2 2

    1 2 2y xdy

    r x r ydx

    2 22 2y xr x r y

    Midpoint Ellipse Algorithm

  • 8/11/2019 Cg Out Put Primitives

    62/73

    62

    Midpoint Ellipse Algorithm

    yi yi-1

    xi xi+1 xi+2

    Midpoint

    Assuming that we have just plotted the pixels at (xi , yi).

    The next position is determined by:

    12

    2 2 2 2 2 212

    1 ( 1, )

    ( 1) ( )

    i ellipse i i

    y i x i x y

    p f x y

    r x r y r r

    Ifp1i< 0 the midpoint is inside the ellipse yiis closer

    Ifp1i0 the midpoint is outside the ellipse yi1is closer

    D i i P (R i 1)

  • 8/11/2019 Cg Out Put Primitives

    63/73

    63

    Decision Parameter (Region 1)

    At the next position [xi+1+ 1 =xi+ 2]

    OR

    whereyi+1=yi

    or yi+1=yi1

    11 1 1 2

    2 2 2 2 2 21

    1 2

    1 ( 1, )

    ( 2) ( )

    i ellipse i i

    y i x i x y

    p f x y

    r x r y r r

    2 2 2 2 2 21 11 1 2 2

    1 1 2 ( 1) ( ) ( )i i y i y x i ip p r x r r y y

    Decision Parameter (Region 1)

  • 8/11/2019 Cg Out Put Primitives

    64/73

    64

    Decision Parameter (Region 1)Decision parameters are incremented by:

    Use only addition and subtraction by obtaining

    At initial position (0, ry)

    2 2

    1

    2 2 2

    1 1

    2 if 1 0

    2 2 if 1 0

    y i y i

    y i y x i i

    r x r p

    increment r x r r y p

    2 2

    2 and 2y xr x r y

    2

    2 2

    2 2 2 2 21 10 2 2

    2 2 21

    4

    2 0

    2 2

    1 (1, ) ( )

    y

    x x y

    ellipse y y x y x y

    y x y x

    r x

    r y r r

    p f r r r r r r

    r r r r

    Region 2

  • 8/11/2019 Cg Out Put Primitives

    65/73

    65

    Region 2Overregion 2, step in the negative y direction and midpoint istaken between horizontal pixels at each step.

    yi yi-1

    xi xi+1 xi+2

    Midpoint

    Decision parameter:

    12

    2 2 2 2 2 212

    2 ( , 1)

    ( ) ( 1)

    i ellipse i i

    y i x i x y

    p f x y

    r x r y r r

    Ifp2i> 0 the midpoint is outside the ellipse xiis closer

    Ifp2i0 the midpoint is inside the ellipse xi+ 1is closer

    D i i P t (R i 2)

  • 8/11/2019 Cg Out Put Primitives

    66/73

    66

    Decision Parameter (Region 2)

    At the next position [yi+11 =yi2]

    OR

    wherexi+1=xi

    or xi+1=xi+ 1

    11 1 12

    2 2 2 2 2 21

    1 2

    2 ( , 1)

    ( ) ( 2)

    i ellipse i i

    y i x i x y

    p f x y

    r x r y r r

    2 2 2 2 21 11 1 2 2

    2 2 2 ( 1) ( ) ( )i i x i x y i ip p r y r r x x

    Decision Parameter (Region 2)

  • 8/11/2019 Cg Out Put Primitives

    67/73

    67

    Decision Parameter (Region 2)

    Decision parameters are incremented by:

    At initial position (x0, y0) is taken at the last

    position selected in region 1

    2 2

    1

    2 2 2

    1 1

    2 if 2 0

    2 2 if 2 0

    x i x i

    y i x i x i

    r y r pincrement

    r x r y r p

    10 0 02

    2 2 2 2 2 210 02

    2 ( , 1)

    ( ) ( 1)

    ellipse

    y x x y

    p f x y

    r x r y r r

    Midpoint Ellipse Algorithm

  • 8/11/2019 Cg Out Put Primitives

    68/73

    68

    p p g

    1. Input rx, ry, and ellipse center (xc,yc), and obtain the first

    point on an ellipse centered on the origin as

    (x0,y0) = (0, ry)

    2. Calculate the initial parameter in region 1as

    3. At eachxiposition, starting at i= 0, ifp1i< 0, the next

    point along the ellipse centered on (0, 0) is (xi + 1,yi) and

    otherwise, the next point is (xi+ 1,yi1) and

    and continue until

    2 2 210 4

    1 y x y xp r r r r

    2 2

    1 11 1 2i i y i yp p r x r

    2 2 2

    1 1 11 1 2 2i i y i x i yp p r x r y r

    2 2

    2 2y xr x r y

    Midpoint Ellipse Algorithm

  • 8/11/2019 Cg Out Put Primitives

    69/73

    69

    p p g4. (x0,y0)is the last position calculated in region 1. Calculate the

    initial parameter in region 2as

    5. At eachyiposition, starting at i= 0, ifp2i> 0, the next point

    along the ellipse centered on (0, 0) is (xi,yi1)and

    otherwise, the next point is (xi+ 1,yi1)and

    Use the same incremental calculations as in region 1. Continue

    untily= 0.

    6. For both regions determine symmetry points in the other threequadrants.

    7. Move each calculated pixel position (x, y) onto the elliptical

    path centered on (xc,yc) and plot the coordinate values

    x= x+ xc , y= y+ yc

    2 2 2 2 2 21

    0 0 022 ( ) ( 1)y x x yp r x r y r r

    2 2

    1 12 2 2i i x i xp p r y r

    2 2 2

    1 1 12 2 2 2i i y i x i xp p r x r y r

    Example

  • 8/11/2019 Cg Out Put Primitives

    70/73

    70

    p

    i pi xi+1, yi+1 2ry2xi+1 2rx

    2yi+1

    0 -332 (1, 6) 72 768

    1 -224 (2, 6) 144 768

    2 -44 (3, 6) 216 768

    3 208 (4, 5) 288 640

    4 -108 (5, 5) 360 640

    5 288 (6, 4) 432 512

    6 244 (7, 3) 504 384

    rx= 8 , ry= 6

    2ry2x = 0 (with increment 2ry

    2= 72)

    2rx2y = 2rx

    2ry (with increment -2rx2= -128)

    Region 1

    (x0, y0) = (0, 6)

    2 2 210 4

    1 332y x y xp r r r r

    Move out of region 1since

    2ry2

    x> 2rx2

    y

    Example

  • 8/11/2019 Cg Out Put Primitives

    71/73

    71

    p

    6

    5

    4 3

    2

    1

    0

    0 1 2 3 4 5 6 7 8

    i pi xi+1, yi+1 2ry2xi+1 2rx

    2yi+1

    0 -151 (8, 2) 576 256

    1 233 (8, 1) 576 128

    2 745 (8, 0) - -

    Region 2

    (x0, y0) = (7, 3) (Last position inregion 1)

    10 22 (7 ,2) 151ellipsep f

    Stop aty= 0

  • 8/11/2019 Cg Out Put Primitives

    72/73

    72

    Exercises

    Draw the ellipse with rx= 6, ry= 8.

    Draw the ellipse with rx= 10, ry= 14.

    Draw the ellipse with rx= 14, ry= 10 andcenter at (15, 10).

    Midpoint Ellipse Function

  • 8/11/2019 Cg Out Put Primitives

    73/73

    p pvoid ellipse(int Rx, int Ry){

    int Rx2 = Rx * Rx, Ry2 = Ry * Ry;int twoRx2 = 2 * Rx2, twoRy2 = Ry2 * Ry2;int p, x = 0, y = Ry;

    int px = 0, py = twoRx2 * y;

    ellisePlotPoints(xcenter, ycenter, x, y);// Region 1p = round(Ry2 (Rx2 * Ry) + (0.25 * Rx2));while (px < py) {

    x++;px += twoRy2;if (p < 0) p += Ry2 + px;

    else {y--;py -= twoRx2;p += Ry2 + px py;

    }ellisePlotPoints(xcenter, ycenter, x, y);

    }// Region 2p = round(Ry2 * (x+0.5) * (x+0.5) + Rx2 * (y-1)*(y-1) Rx2 * Ry2;while (y > 0) {

    y--;py -= twoRx2;if (p > 0) p += Rx2 py;else {

    x++;px += twoRy2;p += Rx2 py + px;

    }