×
If you can dream it, you can do it.

# Note for Computer Graphics - CG By k ramesh

• Computer Graphics - CG
• Note
• 5 Topics
• 5052 Views
K Ramesh
0 User(s)

#### Text from page-1

CS2401 –Computer Graphics UNIT I - 2D PRIMITIVES Output primitives – Line, Circle and Ellipse drawing algorithms - Attributes of output primitives – Two dimensional Geometric transformation - Two dimensional viewing – Line, Polygon, Curve and Text clipping algorithms Introduction A picture is completely specified by the set of intensities for the pixel positions in the display. Shapes and colors of the objects can be described internally with pixel arrays into the frame buffer or with the set of the basic geometric – structure such as straight line segments and polygon color areas. To describe structure of basic object is referred to as output primitives. Each output primitive is specified with input co-ordinate data and other information about the way that objects is to be displayed. Additional output primitives that can be used to constant a picture include circles and other conic sections, quadric surfaces, Spline curves and surfaces, polygon floor areas and character string. Figure : Pixel Postions reference by scan line number and column number To load an intensity value into the frame buffer at a position corresponding to column x along scan line y, Points and Lines Point plotting is accomplished by converting a single coordinate position furnished by an application program into appropriate operations for the output device. With a CRT monitor, for example, the electron beam is turned on to illuminate the screen phosphor at the selected location setpixel (x, y) To retrieve the current frame buffer intensity setting for a specified location we use a low level function Line drawing is accomplished by calculating intermediate positions along the line path between two specified end points positions. An output device is then directed to fill in these positions between the end points getpixel (x, y) Digital devices display a straight line segment by plotting discrete points between the two end points. Discrete coordinate positions along the line path are calculated from the equation of the line. For a raster video display, the line color (intensity) is then loaded into the frame buffer at the corresponding pixel coordinates. Reading from the frame buffer, the video controller then plots “the screen pixels”. Line Drawing Algorithms Pixel positions are referenced according to scan-line number and column number (pixel position across a scan line). Scan lines are numbered consecutively from 0, starting at the bottom of the screen; and pixel columns are numbered from 0, left to right across each scan line The Cartesian slope-intercept equation for a straight line is Digital Differential Analyzer (DDA) Algorithm Bresenham‟s Line Algorithm Parallel Line Algorithm y=m.x+b Where m as slope of the line and b as the y intercept 1 (1)

#### Text from page-2

CS2401 –Computer Graphics Given that the two endpoints of a line segment are specified at positions (x1,y1) and (x2,y2) as in figure we can determine the values for the slope m and y intercept b with the following calculations Figure : Straight line Segment with five sampling positions along the x axis between x1 and x2 Figure : Line Path between endpoint positions (x1,y1) and (x2,y2) Digital Differential Analyzer (DDA) Algortihm m = ∆y / ∆x = y2-y1 / x2 - x1 (2) b= y1 - m . x1 (3) The digital differential analyzer (DDA) is a scan-conversion line algorithm based on calculation either ∆y or ∆x For any given x interval ∆x along a line, we can compute the corresponding y interval ∆y ∆y= m ∆x (4) The line at unit intervals in one coordinate and determine corresponding integer values nearest the line path for the other coordinate. A line with positive slop, if the slope is less than or equal to 1, at unit x intervals (∆x=1) and compute each successive y values as We can obtain the x interval ∆x corresponding to a specified ∆y as ∆ x = ∆ y/m yk+1 = yk + m Subscript k takes integer values starting from 1 for the first point and increases by 1 until the final endpoint is reached. m can be any real number between 0 and 1 and, the calculated y values must be rounded to the nearest integer For lines with slope magnitudes |m| < 1, ∆x can be set proportional to a small horizontal deflection voltage and the corresponding vertical deflection is then set proportional to ∆y as calculated from Eq (4). For lines with a positive slope greater than 1 we reverse the roles of x and y, (∆y=1) and calculate each succeeding x value as For lines whose slopes have magnitudes |m | >1 , ∆y can be set proportional to a small vertical deflection voltage with the corresponding horizontal deflection voltage set proportional to ∆x, calculated from Eq (5) For lines with m = 1, voltage are equal. (6) (5) xk+1 = xk + (1/m) ∆x = ∆y and the horizontal and vertical deflections (7) Equation (6) and (7) are based on the assumption that lines are to be processed from the left endpoint to the right endpoint. 2

#### Text from page-3

CS2401 –Computer Graphics If this processing is reversed, ∆x=-1 that the starting endpoint is at the right yk+1 = yk – m { x += xIncrement; y += yIncrement; setpixel (ROUND(x), ROUND(y)); } } (8) When the slope is greater than 1 and ∆y = -1 with xk+1 = xk-1(1/m) (9) Algorithm Description: If the absolute value of the slope is less than 1 and the start endpoint is at the left, we set ∆x = 1 and calculate y values with Eq. (6) Step 1 : Accept Input as two endpoint pixel positions Step 2: Horizontal and vertical differences between the endpoint positions are assigned to parameters dx and dy (Calculate dx=xb-xa and dy=yb-ya). Step 3: The difference with the greater magnitude determines the value of parameter steps. Step 4 : Starting with pixel position (xa, ya), determine the offset needed at each step to generate the next pixel position along the line path. Step 5: loop the following process for steps number of times When the start endpoint is at the right (for the same slope), we set ∆x = -1 and obtain y positions from Eq. (8). Similarly, when the absolute value of a negative slope is greater than 1, we use ∆y = -1 and Eq. (9) or we use ∆y = 1 and Eq. (7). k x Y 0 1 2 3 4 5 0+0.66=0.66 0.66+0.66=1.32 1.32+0.66=1.98 1.98+0.66=2.64 2.64+0.66=3.3 3.3+0.66=3.96 0+1=1 1+1=2 2+1=3 3+1=4 4+1=5 5+1=6 Plotting points (Rounded to Integer) (1,1) (1,2) (2,3) (3,4) (3,5) (4,6) a. b. c. Use a unit of increment or decrement in the x and y direction if xa is less than xb the values of increment in the x and y directions are 1 and m if xa is greater than xb then the decrements -1 and – m are used. Example : Consider the line from (0,0) to (4,6) 1. 2. 3. 4. 5. Algorithm #define ROUND(a) ((int)(a+0.5)) void lineDDA (int xa, int ya, int xb, int yb) { int dx = xb - xa, dy = yb - ya, steps, k; float xIncrement, yIncrement, x = xa, y = ya; if (abs (dx) > abs (dy) steps = abs (dx) ; else steps = abs dy); xIncrement = dx / (float) steps; yIncrement = dy / (float) steps setpixel (ROUND(x), ROUND(y) ) : for (k=0; k<steps; k++) 6. 7. 8. 3 xa=0, ya =0 and xb=4 yb=6 dx=xb-xa = 4-0 = 4 and dy=yb-ya=6-0= 6 x=0 and y=0 4 > 6 (false) so, steps=6 Calculate xIncrement = dx/steps = 4 / 6 = 0.66 and yIncrement = dy/steps =6/6=1 Setpixel(x,y) = Setpixel(0,0) (Starting Pixel Position) Iterate the calculation for xIncrement and yIncrement for steps(6) number of times Tabulation of the each iteration

#### Text from page-4

CS2401 –Computer Graphics In addition, Bresenham‟s line algorithm can be adapted to display circles and other curves. To illustrate Bresenham's approach, we- 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. Starting from the left endpoint (x0,y0) of a given line, we step to each successive column (x position) and plot the pixel whose scan-line y value is closest to the line path. Result : To determine the pixel (xk,yk) is to be displayed, next to decide which pixel to plot the column xk+1=xk+1.(xk+1,yk) and .(xk+1,yk+1). At sampling position xk+1, we label vertical pixel separations from the mathematical line path as d 1 and d2. The y coordinate on the mathematical line at pixel column position x k+1 is calculated as y =m(xk+1)+b (1) Then d1 = y-yk = m(xk+1)+b-yk d2 = (yk+1)-y = yk+1-m(xk+1)-b To determine which of the two pixel is closest to the line path, efficient test that is based on the difference between the two pixel separations Advantages of DDA Algorithm 1. 2. d1- d2 = 2m(xk+1)-2yk+2b-1 It is the simplest algorithm It is a is a faster method for calculating pixel positions A decision parameter Pk for the kth step in the line algorithm can be obtained by rearranging equation (2). By substituting m=∆y/∆x where ∆x and ∆y are the vertical and horizontal separations of the endpoint positions and defining the decision parameter as Disadvantages of DDA Algorithm 1. 2. (2) Floating point arithmetic in DDA algorithm is still time-consuming End point accuracy is poor pk = ∆x (d1- d2) = 2∆y xk.-2∆x. yk + c Bresenham’s Line Algorithm The sign of pk is the same as the sign of d1- d2,since ∆x>0 An accurate and efficient raster line generating algorithm developed by Bresenham, that uses only incremental integer calculations. 4 (3)