107
Computer Graphics Output Primitives

Out Put Primitives

Embed Size (px)

DESCRIPTION

OUT PUT PRIMITIVES

Citation preview

  • Computer Graphics

    Output Primitives

  • Terminology

    Raster: A rectangular array of points or dots.

    Pixel: Picture element.Smallest accessible element in picture

    Assume rectangular or circular shape

    One dot or picture element of the raster

    Scan line: A row of pixelsVideo raster devices display an image by sequentially drawing out the pixels of the scan lines that form the raster.

  • Resolution

    Image Resolution

    Pixel spacing i.e. distance from one pixel to next pixel

    Total no of pixel along entire height and width of the image

    800X600 means 480000 pixels in image area

    Screen Resolution

    Maximum number of points that can be displayed without

    overlap on a CRT monitor

    The number of distinguishable rows and columns in the

    device.

    Measured in:

    Absolute values (1K x 1K) or,

    Density values (300 dpi [=dots per inch])

    Terminology

  • Aspect Ratio: Ratio between physical dimensions of a pixel(not necessarily 1)no of X pixels to the no of Y pixels

    Frame aspect ratio (FAR) = horizontal/vertical sizeTV 4:3HDTV 16:9Page 8.5:11 ~ 3/4

    Terminology

  • Terminology

    Screen Space:

    A discrete Cartesian coordinate system of the

    screen pixels

    Object Space:

    The Cartesian coordinate system of the

    universe, in which the objects (to be displayed) are

    embedded

    Dynamic Range:

    The ratio between the minimal (not zero!) and

    the maximal light intensity a display pixel can emit

  • Output Primitives:

    Basic geometric structures used to describe

    scenes.

    Can be grouped into more complex structures.

    Each one is specified with input coordinate data

    and other information about the way that object

    is to be displayed.

    Examples: point, line and circle each one with

    specified coordinates.

    Construct the vector picture.

  • Point Drawing

    y

    x

    Converting a single

    coordinate position

    furnished by an application

    program into appropriate

    operation for the output

    device in use.

    In raster system:

    Black-white: setting the bit

    value corresponding to a

    specified screen position

    within the frame buffer to 1.

    RGB: loading the frame buffer with the color codes for the intensities that are

    to be displayed at the screen pixel positions.

  • Line Drawing Algorithms

    x

    1

    x

    2

    y1

    y2 A straight line is

    specified by two

    endpoint positions.

    Line drawing is done

    by:

    Calculating

    intermediate

    positions between

    the endpoints.

    Directing the output device to fill in the

    calculated positions as in the case of plotting

    single points.

  • Line Drawing:

    Plotted positions may be only approximations to

    the actual line positions between endpoints.

    A computed position (10.48, 20.51) is converted

    to pixel (10,21).

    This rounding causes the lines to be displayed

    with a stair step appearance.

    Stair steps are noticeable in low resolution

    systems, it can be improved by:

    Displaying lines on high resolution systems.

    Adjusting intensities along line path.

  • Line Drawing:

    On raster systems, lines are

    plotted with pixels, and step

    sizes in the horizontal and

    vertical directions are

    constrained by pixel

    separations.

    Scan conversion process

    samples a line at discrete

    positions and determine the

    nearest pixel to the line at

    each sampled position.

    Y2

    y1

    Y2

    y1

    X1 x2

    X1 x2

    Sampling along x axis

    Sampling along y axis

  • Line Requirements Must compute integer coordinates of pixels which lie on or near

    a line or circle.

    Pixel level algorithms are invoked hundreds or thousands of

    times when an image is created or modified must be fast!

    Lines must create visually satisfactory images.

    Lines should appear straight

    Lines should terminate accurately

    Lines should have constant density

    Line algorithm should always be defined.

  • Simple Line

    Based on slope-intercept

    algorithm from algebra:

    y = mx + b

    Simple approach:

    increment x, solve for y

    Floating point arithmetic

    required

  • Does it Work?

    It seems to work okay for lines with

    a slope of 1 or less,

    but doesnt work well for lines with

    slope greater than 1 lines become

    more discontinuous in appearance

    and we must add more than 1 pixel

    per column to make it work.

    Solution? - use symmetry.

  • OUTPUT:

  • Modification

    OR, increment along x-axis if dy

  • Vector Generation

    There are two vector generation algorithm:

    DDA = Digital Differential Analyser

    Samples the line at unit intervals in one

    coordinate and determine corresponding

    integer values nearest the line path for the

    other coordinate.

    Bresenhams Algorithm

    Scan converts lines using only incremental

    integer calculations.

  • Line Drawing Algorithms:

    DDA

    A scan-conversion line algorithm based on

    calculating either y or x using line pointscalculating equations.

    In each case, choosing the sample axis

    depends on the slop value.

    The unit step for the selected axis is 1.

    The other axis is calculated depending on the

    first axis and the slop m.

    The next slides will show the different cases of

    the slop:

  • Line Drawing Algorithms:

    DDA

    Case 1:the slop is Positive and less than 1

    Sample at unit x interval (x=1) andcompute each successive y value as :

    y k+1= yk+ m

    K takes integer values starting from 1,at the

    first point, and increasing by 1 on each step

    until reaching the final endpoint.

    The calculated y must be rounded to the

    nearest integer.

  • Lines Drawing Algorithms: DDA

    Example: Describe the line segment which starts at(3,3) and ends at (23,7).

    m= (7-3)/(23-3)

    =4/20

    =0.2

    x=1y=0.2

    x y actual point pixel position

    3 3 (3,3) (3,3)

    4 3.2 (4,3.2) (4,3)

    5 3.4 (5,3.4) (5,3)

    6 3.6 (6,3.6) (6,4)

    7 3.8 (7,3.8) (7,4)

    8 4 (8,4) (8,4)

    9 4.2 (9,4.2) (9,4)

    10 4.4 (10,4.4) (10,4)

    11 4.6 (11,4.6) (11,5)

    12 4.8 (12,4.8) (12,5)

    13 5 (13,5) (13,5)

    14 5.2 (14,5.2) (14,5)

    15 5.4 (15,5.4) (15,5)

    16 5.6 (16,5.6) (16,6)

    17 5.8 (17,5.8) (17,6)

    .. .. .. ..

    .. .. .. ..

    22 6.8 (22,6.8) (22,7)

    23 7 (23,7) (23,7)

  • Line Drawing Algorithms:

    DDA

    Case 2:the slop is Positive and greater than 1

    Sample at unit y interval (y=1) andcompute each successive y value as :

    x k+1= xk+ (1/m)

    K takes integer values starting from 1,at the

    first point, and increasing by 1 on each step

    until reaching the final endpoint.

    The calculated x must be rounded to the

    nearest integer.

  • Line Drawing Algorithms:

    Example: Describe the line segment which starts at (3,3)

    and ends at (7,23).

    m= (23-3)/(7-3)

    = 20/4

    = 5

    x =1/m=1/5

    =0.2

    y =1

    y x actual point pixel

    position

    3 3 (3,3) (3,3)

    4 3.2 (3.2,4) (3,4)

    5 3.4 (3.4,5) (3,5)

    6 3.6 (3.6,6) (4,6)

    7 3.8 (3.8,7) (4,7)

    8 4 (4,8) (4,8)

    9 4.2 (4.2,9) (4,9)

    10 4.4 (4.4,10) (4,10)

    11 4.6 (4.6,11) (5,11)

    12 4.8 (4.8,12) (5,12)

    13 5 (5,13) (5,13)

    14 5.2 (5.2,14) (5,14)

    15 5.4 (5.4,15) (5,15)

    16 5.6 (5.6,16) (6,16)

    17 5.8 (5.8,17) (6,17)

    .. .. .. ..

    .. .. .. ..

    22 6.8 (6.8,22) (7,22)

    23 7 (7,23) (7,23)

  • Line Drawing Algorithms: DDA

    Case 3:the slop is negative and its absolute value is less

    than 1

    Follow the same way in case 1.

    Case 4:the slop is negative and its absolute value is

    greater than 1

    Follow the same way in case 2.

    In the previous 4 cases, we start from the left to the

    right. If the state is reversed then:

    If the absolute slop is less than 1, set x=-1 and

    y k+1= yk - m

    If the absolute slop is greater than 1, set y=-1 and

    x k+1= xk - (1/m)

  • Simple DDA Line Algorithm

    Procedure DDA(X1,Y1,X2,Y2 :Integer);

    Var Length, I:Integer;

    X,Y,Xinc,Yinc :Real;

    Begin

    Length := ABS(X2 - X1);

    If ABS(Y2 -Y1) > Length Then

    Length := ABS(Y2-Y1);

    Xinc := (X2 - X1)/Length;

    Yinc := (Y2 -Y1)/Length;

    X := X1+0.5* sign(Xinc);

    Y := Y1+0.5*sign(Yinc);

    DDA creates good lines but it is too time consuming due to the

    round function and long operations on real values.

    For I := 0 To Length Do

    Begin

    Plot(Round(X), Round(Y));

    X := X + Xinc;

    Y := Y + Yinc

    End {For}

    End; {DDA}

  • Line Drawing Algorithms:

    DDA properties

    A faster method for calculating pixel positions

    than the direct use: y= m . x +b.

    Uses x or y to eliminate the multiplicationin the above equation.

    Rounding successive additions of the floating-

    point increment can cause the calculated pixel

    positions to drift away from the true line path

    for long line segment.

    Rounding and floating-point arithmetic are

    time-consuming operations.

  • Line Drawing Algorithms:

    Bresenhams Line Algorithm:

    Scan converts lines using only incremental

    integer calculations.

    Can be adapted to display circles and other

    curves.

    When sampling at unit x intervals, we need to

    decide which of two possible pixel positions is

    closer to the line path at each sample step by

    using a decision parameter.

  • Line Drawing Algorithms:

    Bresenhams Line Algorithm:

    The decision parameter (d) is an integer number

    that is proportional to the difference between

    the separations of the two pixel positions from

    the actual line path.

    Depending on the slop sign and value the

    decision parameter determine which pixel

    coordinates would be taken in the next step.

  • Bresenham example

    0 1 2 3 4 5

    5

    4

    3

    2

    1

    0

    Draw line having end point (0,0) (5,5)

    dx=5 dy=5 d=5 incrE=10 incrNE=0

    X=0 y=0

    d x y plot

    5

    0 0 0,0

    d>0

    d+incrNE

    d=5 1 1 1,1

  • With any slope and in any quadrant

    Begin

    dx := ABS(xend - xstart);

    dy := ABS(yend - ystart);

    s1=sign(xend - xstart);

    s2= sign(yend - ystart);

    d := 2*dy -dx;

    IncrE := 2*dy

    IncrNE := 2*(dy-dx);

    x := xstart;

    y := ystart

    Plot(x,y);

    If (dy > dx )

    //m>1 then interchange dy & dx

    {

    temp=dx;

    dx=dy;

    dy=temp

    Bresenhams Line Algorithm

    Interchange =1

    Else

    Interchange=0;

    }

    For I := 1 to dx

    if (Interchange ==1)

    If d

  • Bresenhams Line Algorithm

    elseif (Interchange ==0)

    If d

  • Bresenham example

    Draw line having end point (-1,-1) (-5,-8)

    dx=4 dy=7 s1=-1 s2=-1

  • Second order Differences

    Starting point (x0,y0)

    If East pixel will be chosen next pixel (x0+1,y0)

    delEold at (x0,y0)=2x0+3

    delEnew at (x0+1,y0)=2(x0+1)+3

    2nd order diff= delEnew-delEold=2

    Similarly calculate for SE

    If we coose SE in current iteration next pixel (x0+1,y0-1)

  • Bresenham Circle Generation

    AlgorithmBresenham's circle algorithm calculates the locations of the pixels in the first

    45 degrees. It assumes that the circle is centered on the origin shifting the original center coordinates (centerx,centery). So for every pixel (x,y) it calculates, we draw a pixel in each of the 8 octants of the circle :

    putpixel(centerx + x, center y + y)

    putpixel(centerx + x, center y - y)

    putpixel(centerx - x, center y + y)

    putpixel(centerx - x, center y - y)

    putpixel(centerx + y, center y + x)

    putpixel(centerx + y, center y - x)

    putpixel(centerx - y, center y + x)

    putpixel(centerx - y, center y - x)

  • Derivation

    Now, consider a very small continuous arc of the circle interpolated below, passing by the

    discrete pixels as shown.

    At any point (x,y), we

    have two choices to choose the pixel on east

    of it, i.e. N(x+1,y) or the

    south-east pixel S(x+1,y-

    1). To choose the pixel,

    we determine the errors

    involved with both N & S

    which are f(N) and f(S)

    respectively and

    whichever gives the lesser

    error, we choose that

    pixel.

  • Let di = f(N) + f(S), where d can be called as "decision

    parameter", so that

    if (di0),

    then, S(x+1,y-1) is to be chosen as next pixel

    i.e. xi+1 = xi+1 and yi+1 = yi-1.

  • We know that for a circle,

    x2 + y2 = r2,

    where r represents the radius of the circle, an input

    to the algorithm.

    Errors can be represented as

    f(N) = (xi + 1)2 + yi2 - r2, -(1)

    f(S) = (xi + 1)2 + (yi - 1)2 - r2 -(2)

    As di = f(N) + f(S),

    di = 2(xi+1)2 + yi2 + (yi-1)2 2r2 -(3)

  • di = 2(xi+1)2 + yi

    2 + (yi-1)2 2r2 -(3)

    Calculating next decision parameter,

    di+1 = 2(xi+1+1)2 + yi+1

    2 + (yi+1-1)2 2r2 -(4)

    from (4)- (3), we get,

    di+1 di = 2((xi+1+1)2-(xi+1)

    2) + (yi+12 yi

    2) + ((yi+1-1)2 -

    (yi-1)2)

  • di+1 = di + 2((xi+1+1)2-(xi+1)

    2) + (yi+12 yi

    2) + ((yi+1-1)2 - (yi-

    1)2)

    always Xi+1=Xi + 1

    di+1 = di +2((xi+2)2-(xi+1)

    2) + (yi+12 yi

    2) + ((yi+1-1)2 - (yi-1)

    2)

    di+1 = di + 2((xi+2+xi+1)(xi+2-xi-1)) + ((yi+1+yi)(yi+1-yi)) + ((yi+1-1+yi-1)(yi+1-1-yi+1))

  • di+1 = di + 2(2xi+3) + ((yi+1+yi)(yi+1-yi)) + ((yi+1-

    1+yi-1)(yi+1-1-yi+1))

    Now, if (di

  • Now, if (di
  • Else (di >0)

    xi+1=xi+1 and yi+1= yi-1

    di+1 = di + 2(2xi+3) + ((yi-1+yi)(yi-1-yi)) + ((yi-2+yi-1)(yi-2-

    yi+1))

    di+1 = di + 4xi+6 + ((2yi-1)(-1)) + ((2yi-3)(-1))

    di+1 = di + 4xi+6 - 2yi - 2yi + 1 + 3

    di+1 = di + 4(xi - yi) + 10

  • To know di+1, we have to know di first.

    The initial value of di can be obtained by

    replacing x=0 and y=r in (3).

    Thus, we get,

    do = 2 + r2 + (r - 1)2 -2r2

    do = 2 + r2 + r2 + 1 -2r 2r2

    do = 3 2r

  • BRESENHAMS CIRCLE ALGORITHM

    Bresenham Circle ( Xc, Yc, R):

    Description: Here Xc and Yc denote the x coordinate and y coordinate of the center of the circle. R is the radius.

    1. Set X = 0 and Y = R

    2. Set D = 3 2R3. Repeat While (X < Y)

    4. Call Draw Circle(Xc, Yc, X, Y)

    5. Set X = X + 1

    6. If (D < 0) Then

    7. D = D + 4X + 6

    8. Else

    9. Set Y = Y 110. D = D + 4(X Y) + 10

    [End of If]

    X++

    [End of While]

    11. Exit

  • Draw Circle (Xc, Yc, X, Y):

    1. Call PutPixel(Xc + X, Yc, + Y)2. Call PutPixel(Xc - X, Yc, + Y)3. Call PutPixel(Xc + X, Yc, - Y)4. Call PutPixel(Xc - X, Yc, - Y)5. Call PutPixel(Xc + Y, Yc, + X)6. Call PutPixel(Xc - Y, Yc, + X)7. Call PutPixel(Xc + Y, Yc, - X)8. Call PutPixel(Xc - Y, Yc, - X)

    9. Exit

  • 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

    Decision parameter:

    0 if ( , ) is inside the ellipse

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

    0 if ( , ) is outside the ellipse

    ellipse

    x y

    f x y x y

    x y

    1

    Slope = -1

    rx

    ry 2

    2

    2

    2

    2

    y

    x

    r xdySlope

    dx r y

    2 2 2 2 2 2( , )ellipse y x x yf x y r x r y r r

  • Ellipse Algorithms

    Starting at (0, ry) we take unit steps in the x

    direction until we reach the boundary between

    region 1 and region 2. Then we take unit steps

    in the y direction over the remainder of the

    curve in the first quadrant.

    At the boundary

    therefore, we move out of region 1 whenever

    2 21 2 2y xdy

    r x r ydx

    2 22 2y xr x r y

    1

    Slope = -1

    rx

    ry 2

  • Midpoint Ellipse Algorithm

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

    The next position is determined by:

    If p1i < 0 the midpoint is inside the ellipse yi is closer select E

    If p1i 0 the midpoint is outside the ellipse yi 1 is closer select SE

    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

    R2

    R1

    E

    SE

    SES

  • Decision Parameter (Region 1)

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

    OR

    When E is selected yi+1 = yi

    or yi+1 = yi 1

    11 1 1 2

    2 2 2 2 2 211 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)

    Decision parameters are incremented by:

    del E= ry2(2xi+3)

    del SE= ry2(2xi+3)+ rx

    2(-2yi+2)

    At initial position (0, ry)

    2

    2 2

    2 2 2 2 21 10 2 2

    2 2 214

    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

    Over region 2, step in the negative y direction and midpoint is taken between horizontal pixels at each step.

    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

    If p2i > 0 the midpoint is outside the ellipse xi is closer select S

    If p2i 0 the midpoint is inside the ellipse xi + 1 is closer select SE

    R2

    R1

    E

    SE

    SES

  • Decision Parameter (Region 2)

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

    OR

    where xi+1 = xi

    or xi+1 = xi + 1

    11 1 12

    2 2 2 2 2 211 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)

    Decision parameters are incremented by:

    del S= rx2(-2yi+3)

    del SE= ry2(2xi+2)+ rx

    2(2yi+3)

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

    position selected in region 1

    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

    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)

    Calculate the initial parameter in region 1 as

    At each xi position, starting at i = 0, if p1i < 0, E (xi + 1, yi) is

    chosen is and del E is incremented

    P1i= p1i + ry2(2xi+3)

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

    p1=p1+ ry2(2xi+3)+ rx

    2(-2yi+2)

    and continue until

    2 2 210 4

    1 y x y xp r r r r

    2 22 2y xr x r y

  • Midpoint Ellipse Algorithm

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

    initial parameter in region 2 as

    At each yi position, starting at i = 0, if p2i > 0, the next point along

    the ellipse centered on (0, 0) is S(xi, yi 1) and

    P2=P2+rx2(-2yi+3)

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

    P2=P2+ ry2(2xi+2)+ rx

    2(2yi+3)

    Use the same incremental calculations as in region 1. Continue

    until y = 0.

    For both regions determine symmetry points in the other three

    quadrants.

    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 210 0 02

    2 ( ) ( 1)y x x yp r x r y r r

  • Example

    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 1 since

    2ry2x > 2rx

    2y

  • Example

    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 in region 1)

    10 2

    2 (7 , 2) 151ellipsep f

    Stop at y = 0

  • 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 and center at

    (15, 10).

  • Polygon surfaces

    A polygon is an important graphics primitive.

    A polygon is a closed area of image bounded by

    straight lines and filled with one solid color.

    Since images are two dimensional, a polygon is a

    closed planar figure.

    A polygon can be defined as an image which

    consists of a finite ordered set of straight

    boundaries called edges.

    The polygon can also be defined by an ordered

    sequence of vertices, i.e, the corners of the polygon.

    The edges of the polygon are then obtained by

    traversing the vertices in the given order;

  • Polygon surfaces

    Two consecutive vertices define one edge.

    The polygon can be closed by connecting the last

    vertex to the first.

    Face list or polygon surface table is required in

    order to fill the polygon.

    Different types of Polygons

    Simple Convex

    Simple Concave

    Non-simple : self-intersecting

    With holes

  • Different types of Polygons

    Convex

    Concave

    Self-intersecting

  • Area Filling Algorithms

    There are two basic approaches to area filling on raster systems:

    The scan-line approach

    Determine the overlap intervals for scan lines that cross the area.

    is typically used in general graphics packages to fill polygons, circles, ellipses

    Scan line polygon fill algorithm

    Filling approaches

    start from a given interior position and paint outward from this point until we encounter the specified boundary conditions.

    useful with more complex boundaries and in interactive painting systems.

    Boundary fill algorithm

    Flood fill algorithm

  • For each scan line crossing a polygon, the area-fill

    algorithm locates the intersection points of the scan

    line with the polygon edges.

    These intersection points are then sorted from left to

    right, and the corresponding frame-buffer positions

    between each intersection pair are set to the specified

    fill color.

    Scan-Line Polygon Fill Algorithm

  • Scan-Line Polygon Fill Algorithm

  • Calculations performed in scan-conversion and other graphics algorithms typically take advantage of various coherence properties of a scene that is to be displayed.

    Coherence is simply that the properties of one part of a scene are related in some way to other parts of the scene so that the relationship can be used to reduce processing.

    Coherence methods often involve incremental calculations applied along a single scan line or between successive scan lines.

    Scan-Line Polygon Fill Algorithm

  • Inside /0utside Test

    Area-filling algorithms and other graphics processes

    often need to identify interior regions of objects.

    To identify interior regions of an object graphics

    packages normally use either:

    Odd-Even rule

    Nonzero winding number rule

  • Inside /0utside Test

    Odd-Even rule (Odd Parity Rule, Even-Odd Rule):

    Draw a line from any position P to a distant point

    outside the

    co ordinate extents of the object and counting the

    number of edge crossings along the line.

    If the number of polygon edges crossed by this line

    is odd then

    P is an interior point.

    Else

    P is an exterior point

  • Nonzero Winding Number Rule :

    Counts the number of times the polygon edges wind

    around a

    particular point in the counterclockwise direction.

    This count is

    called the winding number, and the interior points of

    a two-

    dimensional object are defined to be those that have

    a nonzero value for the winding number.

    1 Initializing the winding number to Zero.

    2 Imagine a line drawn from any position P to a

    distant point beyond the coordinate extents of the

    object.

  • Nonzero Winding Number Rule :

    3 Count the number of edges that cross the line in each

    direction. We add 1 to the winding number every time

    we intersect a polygon edge that crosses the line from

    right to left, and we subtract 1 every time we intersect

    an edge that crosses from left to right.

    4 If the winding number is nonzero, then

    P is defined to be an interior point

    Else

    P is taken to be an exterior point.

  • Inside /0utside Test

  • Scan-Line Polygon Fill Algorithm

    Finding intersection pairs:

    Scan-line conversion method (for non-horizontal

    edges):

    Edge coherence property: Incremental calculation between

    successive scan lines

    Or using Bresenhams scan line conversion algorithm on each edge and keep a table of span

    extrem for each scan line

  • Incremental scan line method:

    m = (y[k+1] y[k]) / (x[k+1] x[k])

    y[k+1] y[k] = 1

    x[k+1] = x[k] + 1/m x[k] = x[0] + k/m

    Scan line y[k] + 1

    Scan line y[k]

    (x[k+1], y[k+1])

    (x[k], y[k])

    Scan-Line Polygon Fill Algorithm

  • Incremental scan line method:

    Bucket sorted edge table: containing all edges sorted by their smaller y coordinate.

    Each bucket: edges are recorded in order of increasing x coordinate of the lower endpoint.

    Each entry of a bucket: y[max] of the edge, x[min] of lower endpoint and 1/m (x increment)

    Active-edge table (or list): keep track of the set of edges that the scan line intersects and the intersection points in a data structure

    Scan-Line Polygon Fill Algorithm

  • Example:

    5

    1

    4

    3

    2

    y5 x1 1/m[1,5] y2 x1 1/m[1,2]

    y5 x4 1/m[4,5] y3 x4 1/m[4,3]

    y3 x2 1/m[2,3]

    y4

    :

    :

    y1

    y2

    :

    :

    :

    :

    0

    Sorted edge table (ET)

    Scan-Line Polygon Fill Algorithm

  • Boundary Fill

    Suppose that the edges of the polygon has already been colored.

    Suppose that the interior of the polygon is to be colored a different color from the edge.

    Suppose we start with a pixel inside the polygon, then we color that pixel and all surrounding pixels until we meet a pixel that is already colored.

  • Start at a point inside a region and paint the interior

    outward toward the boundary. If the boundary is

    specified in a single color, the fill algorithm proceeds

    outward pixel by pixel until the boundary color is

    encountered.

    It is useful in interactive painting packages, where

    interior points are easily selected.

    The inputs of the this algorithm are:

    Coordinates of the interior point (x, y)

    Fill Color

    Boundary Color

    Boundary Fill

  • Starting from (x, y), the algorithm tests

    neighboring pixels to determine whether they

    are of the boundary color.

    If not, they are painted with the fill color, and

    their neighbors are tested. This process

    continues until all pixels up to the boundary

    have been tested.

    There are two methods for proceeding to

    neighboring pixels from the current test

    position:

    Boundary Fill

  • 1. The 4-connected method.

    2. The 8-connected method.

    Boundary Fill

  • void boundaryFill4 (int x, int y, int fillColor, int borderColor)

    { int interiorColor;

    /* set current color to fillColor, then perform following operations. */

    interiorColor =getPixel (x, y);

    if ( (interiorColor != borderColor) && (interiorColor != fillColor) )

    {

    setPixel (x, y); // set color of pixel to fillColor

    boundaryFill4 (x + 1, y, fillColor, borderColor);

    boundaryFill4 (x - 1, y, fillColor, borderColor);

    boundaryFill4 (x, y + 1, fillColor, borderColor);

    boundaryFill4 (x, y - 1, fillColor, borderColor);

    }

    }

    Boundary Fill

  • Boundary Fill

  • 4-connected and 8-connected methods involve heavy recursion which may consume memory and time. More

    efficient methods are used. These methods fill

    horizontal pixel spans across scan line. This called a

    Pixel Span method.

    We need only stack a beginning position for each horizontal pixel span, instead of stacking all

    unprocessed neighboring positions around the current

    position, where spans are defined as the contiguous

    horizontal string of positions.

    Boundary Fill

  • Pixel Span method

  • Pixel Span method

  • Sometimes we want to fill in (or recolor) an area that is not defined within a single color boundary. We can paint such areas by replacing a specified interior color instead of searching for aboundary color value. This approach is called a flood-fill algorithm.

    Flood Fill

  • We start from a specified interior point (x, y) and reassign all pixel values that are currently set to a given interior color with the desired

    fill color.

    If the area we want to paint has more than one interior color, we can first reassign pixel values so that all interior points have the same

    color. Using either a 4-connected or 8-connected approach, we then

    step through pixel positions until all interior points have been

    repainted.

    Flood Fill

  • void floodFill4 (int x, int y, int fillColor, int interiorColor)

    { int color;

    /* set current color to fillColor, then perform following operations.

    */

    getPixel (x, y, color);

    if (color = interiorColor)

    {

    setPixel (x, y); // set color of pixel to fillColor

    floodFill4 (x + 1, y, fillColor, interiorColor);

    floodFill4 (x - 1, y, fillColor, interiorColor);

    floodFill4 (x, y + 1, fillColor, interiorColor);

    floodFill4 (x, y - 1, fillColor, interiorColor);

    }

    }

    Flood Fill