×
Expect problems and eat them for breakfast.

# Note for Computer Graphics - CG by The Engineer Guy

• Computer Graphics - CG
• Note
• Indian Institute of Information Technology Una - IIITUNA
• Computer Science Engineering
• B.Tech
• 431 Views
0 User(s)

#### Text from page-2

CS2401 –Computer Graphics Unit - I 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, setpixel (x, y) To retrieve the current frame buffer intensity setting for a specified location we use a low level function getpixel (x, y) Line Drawing Algorithms Digital Differential Analyzer (DDA) Algorithm Bresenham’s Line Algorithm Parallel Line Algorithm The Cartesian slope-intercept equation for a straight line is y=m.x+b (1) Where m as slope of the line and b as the y intercept 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 2

#### Text from page-3

CS2401 –Computer Graphics Unit - I Figure : Line Path between endpoint positions (x1,y1) and (x2,y2) m = ∆y / ∆x = y2-y1 / x2 - x1 (2) b= y1 - m . x1 (3) For any given x interval ∆x along a line, we can compute the corresponding y interval ∆y ∆y= m ∆x (4) We can obtain the x interval ∆x corresponding to a specified ∆y as ∆ x = ∆ y/m (5) 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 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, are equal. ∆x = ∆y and the horizontal and vertical deflections voltage Figure : Straight line Segment with five sampling positions along the x axis between x1 and x2 3

#### Text from page-4

CS2401 –Computer Graphics Unit - I Digital Differential Analyzer (DDA) Algortihm The digital differential analyzer (DDA) is a scan-conversion line algorithm based on calculation either ∆y or ∆x 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 yk+1 = yk + m (6) 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 a positive slope greater than 1 we reverse the roles of x and y, (∆y=1) and calculate each succeeding x value as xk+1 = xk + (1/m) (7) Equation (6) and (7) are based on the assumption that lines are to be processed from the left endpoint to the right endpoint. If this processing is reversed, ∆x=-1 that the starting endpoint is at the right yk+1 = yk – m (8) When the slope is greater than 1 and ∆y = -1 with xk+1 = xk-1(1/m) (9) 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) 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). 4

#### Text from page-5

CS2401 –Computer Graphics Unit - I 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++) { x += xIncrement; y += yIncrement; setpixel (ROUND(x), ROUND(y)); } } Algorithm Description: 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 a. Use a unit of increment or decrement in the x and y direction b. if xa is less than xb the values of increment in the x and y directions are 1 and m c. 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. 6. 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) 5