×
If you belive yourself, anything is possible.

# 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
• 806 Views
0 User(s)

#### 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

#### Text from page-6

CS2401 –Computer Graphics Unit - I 7. Iterate the calculation for xIncrement and yIncrement for steps(6) number of times 8. Tabulation of the each iteration 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) Result : Advantages of DDA Algorithm 1. It is the simplest algorithm 2. It is a is a faster method for calculating pixel positions Disadvantages of DDA Algorithm 1. Floating point arithmetic in DDA algorithm is still time-consuming 2. End point accuracy is poor Bresenham’s Line Algorithm An accurate and efficient raster line generating algorithm developed by Bresenham, that uses only incremental integer calculations. 6