Lecture02 Bresenham Line Algo

Embed Size (px)

Citation preview

  • 7/30/2019 Lecture02 Bresenham Line Algo

    1/28

    Line Drawing Algorithm

    Prepared By:

    Prof. Bhavini R. Bhatt

  • 7/30/2019 Lecture02 Bresenham Line Algo

    2/28

    Algorithms for Drawing line

    Introduction

    - Pixel addressing

    - Primitives and attributes

    Line drawing algorithms

    - DDA

    - Bresenham

  • 7/30/2019 Lecture02 Bresenham Line Algo

    3/28

    Pixel addressing in raster graphics and how

    does computer draw line?

    Pixel

    address

    Pixel

    x x+1 x+2 x+3 x+4

    y

    y+1

    y+2

    y+3

    Theoretical length

    Actual length

    Screen made of pixels

    High-level language specifies line System must color pixels

  • 7/30/2019 Lecture02 Bresenham Line Algo

    4/28

    Line drawing algorithms

    imagessymbols &

    y = 2x + 5x0 = 100

    y0 = 50

    d = 100

    thickness = 4

    descriptions

  • 7/30/2019 Lecture02 Bresenham Line Algo

    5/28

    Line raster representation

  • 7/30/2019 Lecture02 Bresenham Line Algo

    6/28

    DDA ( Digital Differential Algorithm )

    m < 1

  • 7/30/2019 Lecture02 Bresenham Line Algo

    7/28

    DDA ( Digital Differential Algorithm )

    m > 1

  • 7/30/2019 Lecture02 Bresenham Line Algo

    8/28

    Line Drawing Algorithms

    Line drawn as pixels Graphics system

    Projects the endpoints to their pixel locations inthe frame buffer (screen coordinates as integers)

    Finds a path of pixels between the two

    Loads the color

    Plots the line on the monitor from frame buffer(video controller)

    Rounding causes all lines except horizontal orvertical to be displayed as jigsaw appearance (lowresolution)

    Improvement: high resolution, or adjust pixelintensities on the line path.

  • 7/30/2019 Lecture02 Bresenham Line Algo

    9/28

    Line Drawing Algorithms

    Line equation

    Slope-intercept form

    y = m . x +

    bslope m

    y-intercept b

    Slope

    y-intercept

    x

    y

    xx

    yym

    end

    end

    0

    0

    00mxyb

  • 7/30/2019 Lecture02 Bresenham Line Algo

    10/28

    Line Drawing Algorithms

    xmy m

    yx

    For lines with slope |m|

  • 7/30/2019 Lecture02 Bresenham Line Algo

    11/28

    Line Drawing Algorithms

    DDA (Digital Differential Analyzer)

    Scan conversion line algorithm based on calculating

    eitherx ory

    Line sampled at regular intervals of x, then

    corresponding y is calculated and rounded

    From left to right

    )1=(m

    1

    +x=x1.0,>mif

    )1=(m+y=y1.0,mif

    yk1+k

    xk1+k

    )-1=(

    m

    1-x=x1.0,>mif

    )-1=(m-y=y1.0,mif

    yk1+k

    xk1+k

  • 7/30/2019 Lecture02 Bresenham Line Algo

    12/28

    Line Drawing Algorithmsvoid lineDDA (int x0, int y0, int xEnd, int yEnd)

    {int dx = xEnd - x0, dy = yEnd - y0, steps, k;

    float xIncrement, yIncrement, x = x0, y = y0;

    if (fabs (dx) > fabs (dy))

    steps = fabs (dx);

    elsesteps = fabs (dy);

    xIncrement = float (dx) / float (steps);

    yIncrement = float (dy) / float (steps);

    setPixel (round (x), round (y));

    for (k = 0; k < steps; k++) {x += xIncrement;

    y += yIncrement;

    setPixel (round (x), round (y));

    }

    }

  • 7/30/2019 Lecture02 Bresenham Line Algo

    13/28

    DDA Algorithm (continued)

    but floating point operations and rounding operations

    are expensive

    Y_inc

    X_inc

  • 7/30/2019 Lecture02 Bresenham Line Algo

    14/28

    DDA Algorithm

    Start with starting and ending coordinates of the line:(x0, y0) and (x1, y1)

    Color first pixel (round to nearest integer)

    Suppose x1-x0 > y1-y0 (gentle slope) There will be x1-x0 steps (# pixels to be colored)

    Set x=x0, y=y0

    At each step,

    increment x by (x1-x0)/numsteps, and increment y by (y1-y0)/numsteps

    For each step, round off x and y to nearest integer,and color pixel

  • 7/30/2019 Lecture02 Bresenham Line Algo

    15/28

    DDA Pseudo-code

    // assume that slope is gentle

    DDA(float x0, float x1, float y0, float y1) {

    float x, y;

    float xinc, yinc;

    int numsteps;

    numsteps = Round(x1) Round(x0);

    xinc = (x1 x0) / numsteps;

    yinc = (y1 y0) / numsteps;

    x = x0;

    y = y0;

    ColorPixel(Round(x),Round(y));

    for (int i=0; i

  • 7/30/2019 Lecture02 Bresenham Line Algo

    16/28

    DDA Example

    Suppose we want todraw a line starting atpixel (2,3) and endingat pixel (12,8).

    What are the valuesof the variables x andy at each timestep?

    What are the pixelscolored, according tothe DDA algorithm?

    numsteps = 12 2 = 10

    xinc = 10/10 = 1.0

    yinc = 5/10 = 0.5

    t x y R(x) R(y)

    0 2 3 2 3

    1 3 3.5 3 42 4 4 4 4

    3 5 4.5 5 5

    4 6 5 6 5

    5 7 5.5 7 6

    6 8 6 8 6

    7 9 6.5 9 7

    8 10 7 10 7

    9 11 7.5 11 8

    10 12 8 12 8

  • 7/30/2019 Lecture02 Bresenham Line Algo

    17/28

    DDA Algorithm

    Advantage Does not calculate coordinates based on the complete

    equation (uses offset method)

    Disadvantage Round-off errors are accumulated, thus line diverges more

    and more from straight line

    Round-off operations take time

    Perform integer arithmetic by storing float asintegers in numerator and denominator andperforming integer arithmetic.

  • 7/30/2019 Lecture02 Bresenham Line Algo

    18/28

    Bresenhams Line Drawing

    Algorithm

    Efficient line drawing algorithm using onlyincremental integer calculations

    Can be adapted to draw circles and other

    curves Principle

    Vertical axes show scan line positions

    Horizontal axes show show pixel columns

    At each step, determine the best next pixel basedon the sign of an integer parameter whose value is

    proportional to the difference between the vertical

    separations of the two pixel positions from the

    actual line.

  • 7/30/2019 Lecture02 Bresenham Line Algo

    19/28

    Bresenhams Algorithm

    Uses only integer calculations

    Uses distance between ideal y-coordinate and the upper

    and lower pixel (assuming gentle slope)

    dupper

    dlower

  • 7/30/2019 Lecture02 Bresenham Line Algo

    20/28

    General idea how Bresenham

    works first consider the scan-conversion process for lines with positive

    slope less than 1.

    Pixel positions along a line path are then determined by sampling at

    unit x intervals.

    Start ing from the left endpoin t(xo, yo) of a given line, we step toeach successive column (x p os it ion) andplot the pixel whose scan-

    line y value is closest to the line path.

    Figure 3-7 demonstrates the Kth step in this process.

    Assuming we have determined that the pixel at (xk, yk) is to be

    displayed, we next need to decide which pixel to plot in

    column xk+1,

    Our ch oices are the pixels at

    posit io ns (Xk+l, Yk) and (Xk+l, Yk+l).

  • 7/30/2019 Lecture02 Bresenham Line Algo

    21/28

    Bresenhams Line Drawing

    Algorithm

    Section of the screen grid showing a pixel in column xkon scanline yk that is to be plotted along the path of a line segment withslope

    O

  • 7/30/2019 Lecture02 Bresenham Line Algo

    22/28

    Bresenhams Line Drawing Algorithm

    Distances between pixel positions and the line y coordinate atsampling position xk+ I.

  • 7/30/2019 Lecture02 Bresenham Line Algo

    23/28

    Bresenham's line algorithm

    d1

    d2

    x x+1

    y

    y = m(x+1) + b

    y = mx + b

  • 7/30/2019 Lecture02 Bresenham Line Algo

    24/28

    Bresenham's line algorithm (slope 1)

    1. Input the two line endpoints and store the leftendpoint in (x0, y0).

    2. Set the color for the frame-buffer position (x0, y0)i.e. plot the first point.

    3. Calculate the constantx, y, 2y and 2y 2x,and set the starting value for the decision parameter

    as p0 = 2y x

    4. At each xk along the line, from k=0, perform thefollowing test:if pk

  • 7/30/2019 Lecture02 Bresenham Line Algo

    25/28

  • 7/30/2019 Lecture02 Bresenham Line Algo

    26/28

    Number of Operations in

    Bresenhams AlgorithmQ: In each step, how many floating point operations are there?

    A: 0

    Q: In each step, how many integer operations are there?A: 3 or 4

  • 7/30/2019 Lecture02 Bresenham Line Algo

    27/28

    Bresenhams Algorithm Example

    Suppose we want to drawa line starting at pixel(2,3) and ending at pixel(12,8).

    What are the values ofp0, dx and dy?

    What are the values ofthe variable p at eachtimestep?

    What are the pixelscolored, according toBresenhams algorithm?

    dx = 12 2 = 10

    dy = 8 3 = 5p0 = 2dy dx = 15

    t p P(x) P(y)

    0 0 2 3

    1 -10 3 42 0 4 4

    3 -10 5 5

    4 0 6 5

    5 -10 7 6

    6 0 8 6

    7 -10 9 7

    8 0 10 7

    9 -10 11 8

    10 0 12 8

    2dy = 10

    2dy 2dx = -10

  • 7/30/2019 Lecture02 Bresenham Line Algo

    28/28

    DDA versus Bresenhams Algorithm

    DDA works with floating point arithmetic

    Rounding to integers necessary

    Bresenhams algorithm uses integer arithmetic

    Constants need to be computed only once

    Bresenhams algorithm generally faster than DDA