scan conversion algo

23
Unit 1 – Cha pt er 2 – Scan-conv ersion aalgorithms of computer graphics primitives Ms. Neha Mukesh Srivastava

Upload: koolnash8784

Post on 18-Oct-2015

27 views

Category:

Documents


0 download

DESCRIPTION

Scan Conversion

TRANSCRIPT

  • 5/28/2018 Scan Conversion Algo

    1/23

    Unit 1 Chapter 2

    Scan-conversion aalgorithms of computer graphics

    primitives

    Ms. Neha Mukesh Srivastava

  • 5/28/2018 Scan Conversion Algo

    2/23

    SyllabusScan-Conversion of a LinesDigital Differential Analyzer Algorithm

    Bresenhams Line-Drawing Algorithm-

    Bresenhams Method of Circle Drawing

    Midpoint Circle Algorithm

    Drawing Ellipses and Other Conics.

    2 Computer Graphics - Prof. Neha M. Srivastava

  • 5/28/2018 Scan Conversion Algo

    3/23

    Introduction The process to determine which pixel provides the best approximation

    to shape the object is called rasterization, and when such procedure iscombined with picture generation using scan line, it is called scan-conversion.

    A point is a geometric element that is defined by it coordinates.

    Mathematically, a point P in space has coordinates x andy represented byP x .

    It is important to note that a point coordinates have integral values inorder to display it on the screen coordinates, as a display system requiresno floating point coordinates.

    If one generates such coordinates then it must be converted to its

    nearest integer part. For example, a point with coordinates P(1.3, 4.2) has to be converted

    to P( 1,4) for display using suitable functions in any programmingenvironment.

    3 Computer Graphics - Prof. Neha M. Srivastava

  • 5/28/2018 Scan Conversion Algo

    4/23

    Scan conversion of lines Lines, or more accurately segments, are the most basic of computer

    graphics objects.

    The display system of a computer is a two-dimensional array consistingof rows and columns.

    The primary objective of scan-conversion is to determine the

    intersection of rows and column to find the area, called pixels, to paintan ob ect.

    A line can be described as an infinitely thin, infinitely long, and perfectlystraight, containing an infinite number of points.

    In Euclidean geometry, exactly one line can be found that passes throughany two points.

    The line provides the shortest connection between the points. Each line should appear to have an even visual thickness, that is, it should

    have as constant a density as possible, and this thickness should beindependent of its length and slope.

    4 Computer Graphics - Prof. Neha M. Srivastava

  • 5/28/2018 Scan Conversion Algo

    5/23

    The choice of pixel on the line or a curve is difficult, because the screenresolution may not be a perfect square in dimension - equal horizontaland vertical width.

    But this is not the case.

    A line along the diagonal is straight, but its width is not constant.

    This is because each pixel is visualized to be a square element in display.

    Hence, it requires a careful introspection in determining the next pixelin the sequence to be painted to give it a straight uniform look.

    A line is a graphic element that is defined by its two end points and begiven by the equationy = mx + c, where m is the slope of the line and c

    Computer Graphics - Prof. Neha M. Srivastava5

    - .

    This equation describes all the points that lie on this line.

    Therefore, a line drawing requires determining such points that lie on it.

    This process is incremental in nature.

    Most (straight) lines and curves drawn in computer graphics satisfyequating a function of x andy with zero.

    For example, we can re-express the line equationy = mx + c as 0 = mx+ c y.

  • 5/28/2018 Scan Conversion Algo

    6/23

    Digital differential analyzer algorithm -

    DDA

    Digital differential analyzer (DDA) is anincremental scan-conversion method todetermine points on a line.

    It re uires calculatin the difference form the

    Computer Graphics - Prof. Neha M. Srivastava6

    previous step calculations.

    The differences are x and y in x andy

    directions respectively. These determine the direction of the movement

    and convergence.

  • 5/28/2018 Scan Conversion Algo

    7/23

    Step 1: Input the coordinates of the two end points A(x1,y1) and B(x2, y2) for the

    line AB respectively.

    Note that points A and B are not equal. (If they are equal, then it is a point. Plot the point and return.)

    Step 2: [Calculate dx and dy] dx = (x2 x1) and dy = (y2 y1)

    Step 3: [Calculate the length L] = =

    Computer Graphics - Prof. Neha M. Srivastava7

    Else L = abs (y2 y1)

    Step 4: [Calculate the increment factor] x = (x2 x1)/L and y = (y2 y1)/L This step makes either x or y equal to 1, because L is either |x2 x1|

    or |y2 y1|. Therefore, a step increment in x or y direction is equal to 1.

  • 5/28/2018 Scan Conversion Algo

    8/23

    Step 5: [Initialize the initial point on the line and plot] xnew = x1 + 0.5 and ynew = y1 + 0.5

    Plot (Integer (xnew), Integer (ynew)) The values are rounded using the factor of 0.5 rather than truncating, so that the

    central pixel addressing is handled correctly. Moreover, the sign function given makes the algorithm work in all the quadrants.

    Step 6:

    [Obtain the new pixel on the line and plot the same]

    Initialize i to 1While (i

  • 5/28/2018 Scan Conversion Algo

    9/23

    Procedure lineDDA(xa, ya, xb, yb : integer);

    Var

    dx, dy, steps, k : integer;

    xIncrement, yIncrement, x, y : real;

    Begin

    dx = xb xa;

    dy = yb ya;

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

    else steps = abs(dy)

    xIncrement = dx / steps;

    yIncrement = dy / steps;x = xa;

    Computer Graphics - Prof. Neha M. Srivastava9

    y = ya;

    setpixel(round(x), round(y), color);

    for k = 1 to steps do

    begin

    x = x + xIncrement

    y = y + yIncrement

    setpixel(round(x), round(y), color);

    end

    End;(lineDDA)

  • 5/28/2018 Scan Conversion Algo

    10/23

    Example 1 Consider a line AB with A = (0,0) and B = (8, 4). Apply a simple DDA algorithm and

    calculate the pixels on this line. Solution:

    Trace for the algorithm for line AB.

    Step 1: [Initialize the inputs]

    x1 = 0 and y1 = 0 and x2 = 8 and y2 = 4

    Step 2: [Calculate dx and dy]= = - = = = =

    Computer Graphics - Prof. Neha M. Srivastava10

    -

    Step 3: [Calculate length estimate]

    L = |dy| = 8 as dx > dy

    Step 4: [Calculate increment factors in x and y directions]

    xIncrement = dx/L = 8/8 = 1 and yIncrement = dy/L = 4/8 = 0.5

    Step 5: [Initialize the points]

    xnew = xl + 0.5 = 0 + 0.5 = 0.5 and ynew = y1 + 0.5 = 0 + 0.5 = 0.5.

    Plot (0, 0)

  • 5/28/2018 Scan Conversion Algo

    11/23

    Step 6: [Obtain the new pixel on the line and plot thesame]

    Initialize i to 1

    While (i

  • 5/28/2018 Scan Conversion Algo

    12/23

    Bresenhams line drawing algorithm Although the DDA algorithm for drawing straight lines is simple, they involve real arithmetic.

    Some simple modifications result in an algorithm that does only integer arithmetic, and onlyinvolves additions.

    The Bresenham's line algorithm is an algorithm that determines which points in an n-dimensionalraster should he plotted to form a close approximation to a straight line between two given points.

    It is commonly used to draw lines on a computer screen, as it uses only integer addition,subtraction, and bit shifting, all of which are very cheap operations in standard computer

    architectures.

    Computer Graphics - Prof. Neha M. Srivastava

    12

    lines onto a discrete plane of computer display.

    The algorithm basically approximates a real valued line by calculating what pixels to illuminate andto provide illusion of the line.

    Since the pixels are sufficiently small, the approximation is good enough to trick the human eyes

    and get illusion of a real line.

    To proceed with the algorithm, it always increments in either x ory by one unit based on the slopeof the line.

    The increment factor in the other variable is deter mined by calculating the error distance betweenthe actual line and the nearest pixel plot.

  • 5/28/2018 Scan Conversion Algo

    13/23

    The algorithm is based on whether the predefined error term is positive ornegative.

    There fore, the algorithm only examines the error terms to determine the

    direction of the movement of the line. The line segment will lie in one of the eight octets.

    This will define the direction of movement of the line segment, which can betowards positive x-axis, positive y-axis, negative x-axis, or negative y-axis.

    The algorithm plots the points, which approximate the line in direction that isperpendicular to the axis towards which the line is sloping.

    Thus for the line in a first octet, the algorithm plots the points and moves in the positiveyaxis.

    Computer Graphics - Prof. Neha M. Srivastava13

    This is done as long as the error term is non-negative.

    When the error term is negative, the algorithm plots the points in the direction of line andupdates the error term until it becomes non-negative again.

    The algorithm assumes the two end

    points to be non-equal and variable tobe integer.

  • 5/28/2018 Scan Conversion Algo

    14/23

    Algorithm Step 1:

    Initialize the end points of line, say, AB with A(x1

    , y1

    ) and B(x2

    , y2

    ). The two end points are assumed to be distinct.

    Step 2: [Calculate x and y] x = x2 x1 and y = y2 y1

    Step 3: [Initialize the point and error term to compensate for nonzero intercepts]

    e = (y / x) (1 / 2) and x = x1, and y = y1,

    Step 4:

    Computer Graphics - Prof. Neha M. Srivastava14

    For i = 0 tox

    Plot (int (x), int (y))

    while (e >= 0)

    y=y+1

    e=e-1

    End while loop x=x+1

    e= y/ x+ enext i

    End for loop

    Step 5: Finish

  • 5/28/2018 Scan Conversion Algo

    15/23

    Example2 Consider the line coordinates (0, 0) and (8, 4). Rasterize the

    line segment using Bresenhams algorithm.

    Solution

    Step 1: [Initialize]x1 = 0 and y1 = 0 and x2 = 8 and y2 = 4

    Computer Graphics - Prof. Neha M. Srivastava15

    Step 2: [Calculate x and y]x = x2 x1 = 8 0 = 8 and y = y2 y1 = 4 0 = 4

    Step 3: [Initialize the start and error term]

    e = (y / x) (1 / 2) = (4 / 8) (1 / 2) = 0 andx = x1 = 0 and y = y1 = 0

    Step 4: [Determine the new pixels on the line]

  • 5/28/2018 Scan Conversion Algo

    16/23

    Counter

    i

    Error

    term e

    Plot

    (x, y)

    x Y5

    0 0 (0,0) 0 0

    0 -1 - 0 1

    0 -0.5 - 1 1

    1 -0.5 (1,1) 1 1

    1 -0.5 - 1 1

    1 0 - 2 1

    2 0 (2,1) 2 12 -1 - 2 2

    Counter

    i

    Error

    term e

    Plot

    (x, y)

    x y

    5 -0.5 (5,3) 5 3

    5 -0.5 - 5 35 0 - 6 3

    6 0 (6,3) 6 3

    6 -1 - 6 4

    6 -0.5 - 7 4

    7 -0.5 (7,4) 7 4

    Computer Graphics - Prof. Neha M. Srivastava16

    2 -0.5 - 3 2

    3 -0.5 (3,2) 3 2

    3 -0.5 - 3 2

    3 0 - 4 24 0 (4,2) 4 2

    4 -1 - 4 3

    4 -0.5 - 5 3

    - . -

    7 0 - 8 4

    8 0 (8,4) 8 4

    8 -1 - 8 5

    8 -0.5 - 9 5

  • 5/28/2018 Scan Conversion Algo

    17/23

    Scan-conversion of circle and ellipse Euclidean geometry defines a circle as a set of all points in a plane at a fixed distance (called a

    radius) from a given point (called the center).

    Circles are simple closed curves, which divide the plane into an interior and exterior part.

    A circle is a symmetric figure.

    This property of symmetry of the circle could be utilized to scan-convert the circle by plottingeight points for each point on the circle for eight octants in a coordinate system.

    The entire circle could be produced by just computing first octant and then repeated reflection ofthe point about each 45o axis.

    Computer Graphics - Prof. Neha M. Srivastava17

    It produces seven more points on the circumference of the circle.

    Hence it is sufficient to calculate only one octant.

    Before we proceed with the circle drawing algorithms, we will describe the mathematical structureof the circle.

    A circle centered at the origin can be mathematically defined by either polynomial or atrigonometric method .

    Firstly, any circle centered at the origin can be described by a send-order polynomial equation givenby

    r2 = x2 + y2

    Where x and y are the x and y coordinates and r is the radius of the circle.

  • 5/28/2018 Scan Conversion Algo

    18/23

    The polynomial relation between x y, and r could be used to find y coordinates fromnoted x coordinates.

    Computer Graphics - Prof. Neha M. Srivastava18

    From Figure 2.12, it is evident that the sector between 90o to 45o generates x by steppingx by 0 to and y by calculating (r2 x2)for each x.

    The method works well, but it is a little economical since for each point squares of r and xmust be found and the square root of the difference is also a must .

    The second approach describes the circle using trigonometric relations, where x and ycoordinate of the circle are given by x = r cos and y = r sin , where is the currentangle, r is the radius of the circle, x is the x coordinate, and y is the y coordinated.

    The method increments from 0 to and calculates x and y each time.

    The method is again inefficient, because computation of sin and cos takes much time.

    2r

    4

  • 5/28/2018 Scan Conversion Algo

    19/23

    Bresenhams method for circle drawing Method of Bresenham uses scan conversion with only integer operation like

    addition, subtraction, and multiplication, rather than trigonometric functions.

    Therefore, the method proves to be faster and efficient for real-timeimplementation.

    Bresenham considered the eight-way symmetry of the circle and generates the

    points to plot only 1/8th of the circle between 90 and 45, which incrementsx in positive and y in negative direction respectively.

    Computer Graphics - Prof. Neha M. Srivastava19

    Let P(xi, yi)be a point on the circumference of the circle just now generated.

    As the direction of the circle generation is from 90o to 45, the next point onthe circumference can b R(xi+1 , yi) or S(xi+1 , yi-1).

    A pixel R or S chosen next will be increment in +x by one unit, or it will beincrement in +x and y simultaneously.

    To choose among these two, the best approximation is to find out the pixel orthe point that falls nearest to the true circle.

  • 5/28/2018 Scan Conversion Algo

    20/23

    Algorithm Step 1:

    Obtain the radius of the circle r. Step 2:

    Set the base decision factor = 1 = 3 2r

    Step 3: Set the base values of the coordinates x = 0 and y = r

    Step 4: [Compute the next pixels on the circle and update the decision factor]While (x

  • 5/28/2018 Scan Conversion Algo

    21/23

    Midpoint circle algorithm

    The midpoint subdivision circle algorithm also utilizes theeight-point symmetry about the origin in the respectiveoctants.

    It is also similar to the Bresenham's circle algorithm butrather chooses the ixel a roximation based on the s atial

    Computer Graphics - Prof. Neha M. Srivastava21

    relationship between the arbitrary point (x, y) and the radiusof the circle about the origin.

    As we draw the circle again from 90o to 45o, the circle movespositive in x and negative in y directions.

  • 5/28/2018 Scan Conversion Algo

    22/23

    Algorithm The following algorithm generates pixels on a circle using the midpoint method for the 90o to 45o octant. The remaining pixels for

    the circle may be generated by successive reflection around the eight axes.

    Step 1: Input the radius of the circle. Note that radius is the distance from the origin.

    Step 2:

    Initialize the start pixel on tht circle x = 0 and y = r

    Step 3: [Compute the initial decision factor]

    Mi = M = (5 / 4) r

    Step 4:

    [Compute the decision factor and determine the next pixel coordinates on the circle]

    Computer Graphics - Prof. Neha M. Srivastava22

    While (x

  • 5/28/2018 Scan Conversion Algo

    23/23

    The End

    Computer Graphics - Prof. Neha M. Srivastava23