Lines Midpoint

Embed Size (px)

Citation preview

  • 7/25/2019 Lines Midpoint

    1/25

    ihar Ranjan Roy

    Scan Conversion of Lines

    MIDPOINT LINE DRAWING ALGORITHM

  • 7/25/2019 Lines Midpoint

    2/25

    Midpoint Algorithm

    Given a point just drawn, determine whether we move E orNE on next step

    EW

    N

    Nihar Ranjan Roy,https://sites.google.com/site/niharranjanroy/

  • 7/25/2019 Lines Midpoint

    3/25

    Midpoint Algorithm

    Given a point just drawn, determine whether we move E or NEon next step?

    Is the line above or below ?

    Below: move E

    Above: move NE

    ),1( 21 yx

    ),( yxNihar Ranjan Roy,https://sites.google.com/site/niharranjanroy/

  • 7/25/2019 Lines Midpoint

    4/25

    Midpoint Algorithm Implicit

    Forms

    How do we tell if a point is above or below

    the line?Bmxy

    BxyLH

    LH

    xx

    yy

    ))(()( LHxxyy

    LH xxBxyxxLH

    LH

    Bxxxyyyxx LHLHLH )()()(

    Nihar Ranjan Roy,https://sites.google.com/site/niharranjanroy/

  • 7/25/2019 Lines Midpoint

    5/25

    Midpoint Algorithm Implicit

    Forms

    How do we tell if a point is above or below the line?

    0)()()( Bxxyxxxyy LHLHLH

    Bxxxyyyxx LHLHLH )()()(

    Nihar Ranjan Roy,https://sites.google.com/site/niharranjanroy/

  • 7/25/2019 Lines Midpoint

    6/25

    Midpoint Algorithm Implicit

    Forms

    How do we tell if a point is above or below the line?

    cbyaxyxfd ),(

    yyya LH

    xxxb LH )(

    )(LH

    xxBc

    Location of midpoint d=?

    f(x,y)=0 (x,y) on the line

    f(x,y)0 (x,y) below the line

    0)()()( Bxxyxxxyy LHLHLH

    Nihar Ranjan Roy,https://sites.google.com/site/niharranjanroy/

  • 7/25/2019 Lines Midpoint

    7/25

    Midpoint Algorithm Implicit

    Forms

    x

    y

    )2,0(

    )5,6(

    263 xy

    Nihar Ranjan Roy,https://sites.google.com/site/niharranjanroy/

  • 7/25/2019 Lines Midpoint

    8/25

    Midpoint Algorithm Implicit

    Forms

    x

    y

    )2,0(

    263 xy

    )5,6(1263),( yxyxf

    Location of midpoint d=?

    f(x,y)=0 (x,y) on the line

    f(x,y)0 (x,y) below the lineNihar Ranjan Roy,

    https://sites.google.com/site/niharranjanroy/

  • 7/25/2019 Lines Midpoint

    9/25

    Midpoint Algorithm Implicit

    Forms

    x

    y

    )2,0(

    263 xy

    18)5,0( f

    )5,6(1263),( yxyxf

    Location of midpoint d=?

    f(x,y)=0 (x,y) on the line

    f(x,y)0 (x,y) below the lineNihar Ranjan Roy,

    https://sites.google.com/site/niharranjanroy/

  • 7/25/2019 Lines Midpoint

    10/25

    Midpoint Algorithm Implicit

    Forms

    x

    y

    )2,0(

    263 xy

    18)1,4( f

    )5,6(

    Location of midpoint d=?

    f(x,y)=0 (x,y) on the line

    f(x,y)0 (x,y) below the line

    1263),( yxyxf

    Nihar Ranjan Roy,https://sites.google.com/site/niharranjanroy/

  • 7/25/2019 Lines Midpoint

    11/25

    Midpoint Algorithm Implicit

    Forms

    x

    y

    )2,0(

    263 xy

    0)4,4( f

    )5,6(

    Location of midpoint d=?

    f(x,y)=0 (x,y) on the line

    f(x,y)0 (x,y) below the line

    1263),( yxyxf

    Nihar Ranjan Roy,https://sites.google.com/site/niharranjanroy/

  • 7/25/2019 Lines Midpoint

    12/25

    Midpoint Algorithm Implicit

    Forms

    x

    y

    )2,0(

    263 xy

    )5,6(

    Location of midpoint d=?

    f(x,y)=0 (x,y) on the line

    f(x,y)0 (x,y) below the line

    1263),( yxyxf

    Nihar Ranjan Roy,https://sites.google.com/site/niharranjanroy/

  • 7/25/2019 Lines Midpoint

    13/25

    Location of midpoint d=?

    d=f(x,y)=0 (x,y) on the line

    d=f(x,y)0 (x,y) below the line

    Midpoint Algorithm

    Need value of to determine E or NE

    Build incremental algorithm

    Assume we have value of

    Find value of if E chosen

    Find value of if NE chosen

    ),1( 21 yxf

    ),1(21

    yxf

    ),2( 21 yxf),2(

    23

    yxf

    E

    NE

    d?Nihar Ranjan Roy,https://sites.google.com/site/niharranjanroy/

  • 7/25/2019 Lines Midpoint

    14/25

    Midpoint Algorithm d

  • 7/25/2019 Lines Midpoint

    15/25

    Midpoint Algorithm d>0

    If NE was chosen, find ),2( 23 yxf

    E

    NE

    dold dnew

    cybxayxfdnew )()2(),2(23

    23

    Nihar Ranjan Roy,https://sites.google.com/site/niharranjanroy/

  • 7/25/2019 Lines Midpoint

    16/25

    If NE was chosen, find

    Midpoint Algorithm d>0

    ),2( 23 yxf

    xydd

    badd

    yxfbayxfd

    oldnew

    oldnew

    new

    ),1(),2(21

    23

    3

    Nihar Ranjan Roy,https://sites.google.com/site/niharranjanroy/

  • 7/25/2019 Lines Midpoint

    17/25

    Midpoint Algorithm d0 ?

    What about starting value?

    cybxayxfd LLLL )()1(),1( 21

    21

    0

    Nihar Ranjan Roy,https://sites.google.com/site/niharranjanroy/

  • 7/25/2019 Lines Midpoint

    18/25

    Midpoint Algorithm d0 ?

    What about starting value?

    cybxayxfd LLLL )()1(),1( 21

    21

    0

    bayxfyxfd LLLL 21210 ),(),1(

    Nihar Ranjan Roy,https://sites.google.com/site/niharranjanroy/

  • 7/25/2019 Lines Midpoint

    19/25

    Midpoint Algorithm

    What about starting value?

    cybxayxfd LLLL )()1(),1( 21

    21

    0

    bayxfyxf LLLL 2121 ),(),1(

    is on the line! =0),( LL yx

    bayxfd LL 21

    21

    0 ),1(

    Nihar Ranjan Roy,https://sites.google.com/site/niharranjanroy/

  • 7/25/2019 Lines Midpoint

    20/25

    xyyxfd LL 2),1( 210

    Midpoint Algorithm

    Multiplying coefficients by 2 doesnt change sign of f

    bayxfd LL 2),1( 21

    0

    bayxfd LL 21

    21

    0 ),1(

    5ydd oldnew 2

    )(2 xydd oldnew 6

    4

    Similarly equation 2 and 3 becomes

    E is Chosen

    NE is Chosen

    Nihar Ranjan Roy,https://sites.google.com/site/niharranjanroy/

  • 7/25/2019 Lines Midpoint

    21/25

    Midpoint Algorithm Summary

    int dx, dy, d, incE, incNE, x, y;dx = x2 - x1;

    dy = y2 - y1;

    d = 2*dy - dx;

    incE = 2*dy;

    incNE = 2*(dy - dx);

    y = y1;

    for (x=x1; x0)

    { d = d + incNE;

    y = y + 1; }

    else

    { d = d + incE; }

    }

    Location of midpoint d=?

    d=f(x,y)=0 (x,y) on the lined=f(x,y)0 (x,y) below the line

    E

    NE

    Nihar Ranjan Roy,https://sites.google.com/site/niharranjanroy/

  • 7/25/2019 Lines Midpoint

    22/25

    Problem

    Plot all the raster points that make a line

    from (0,2) to (6,4) using Mid-point line

    drawing algorithm.

    Nihar Ranjan Roy,https://sites.google.com/site/niharranjanroy/

  • 7/25/2019 Lines Midpoint

    23/25

    int dx, dy, d, incE, incNE, x, y;

    dx = x2 - x1;

    dy = y2 - y1;

    d = 2*dy - dx;

    incE = 2*dy;

    incNE = 2*(dy - dx);y = y1;

    for (x=x1; x0)

    { d = d + incNE;y = y + 1; }

    else

    { d = d + incE; }

    }

    Midpoint Algorithm Example

    d E NE (x,y)

    -2 (1,2) (1,2)

    2 (2,3) (2,3)

    -6 (3,3) (3,3)

    -2 (4,3) (4,3)

    2 (5,4) (5,4)

    -6 (6,4) (6,4)

    Draw a line from (0,2) to (6,4)dx=6 dy=2 d0=-2

    incE=4

    incNE=-8

    Nihar Ranjan Roy,https://sites.google.com/site/niharranjanroy/

  • 7/25/2019 Lines Midpoint

    24/25

    output

    )2,0(

    )4,6(d E NE (x,y)

    -2 (1,2) (1,2)

    2 (2,3) (2,3)

    -6 (3,3) (3,3)

    -2 (4,3) (4,3)

    2 (5,4) (5,4)

    -6 (6,4) (6,4)

    Nihar Ranjan Roy,https://sites.google.com/site/niharranjanroy/

  • 7/25/2019 Lines Midpoint

    25/25

    Midpoint Algorithm vs DDA

    Only integer operations

    Faster than DDA

    No multiplication no division

    Nihar Ranjan Roy,https://sites.google.com/site/niharranjanroy/