KEMBAR78
Line drawing Algorithm DDA in computer Graphics.pdf
18CSE338J – Artificial Intelligence
Unit – 1 : Session –5 : SLO -1,2
Line drawing Algorithm DDA, Bresenhams
1
DDA Line drawing algorithm
• The steps involved in DDA line generation algorithm are:
• Input the two endpoints of the line segment, (x1,y1) and (x2,y2).
• Calculate the difference between the x-coordinates and y-coordinates of the endpoints as dx
and dy respectively.
• Calculate the slope of the line as m = dy/dx.
• Set the initial point of the line as (x1,y1).
• Loop through the x-coordinates of the line, incrementing by one each time, and calculate the
corresponding y-coordinate using the equation y = y1 + m(x – x1).
• Plot the pixel at the calculated (x,y) coordinate.
• Repeat steps 5 and 6 until the endpoint (x2,y2) is reached.
• DDA algorithm is relatively easy to implement and is computationally efficient, making it
suitable for real-time applications. However, it has some limitations, such as the inability to
handle vertical lines and the need for floating-point arithmetic, which can be slow on some
systems. Nonetheless, it remains a popular choice for generating lines in computer graphics.
•
2
• DDA Algorithm:
• Consider one point of the line as (X0, Y0) and the second point of the line as (X1, Y1).
• // calculate dx , dy
• dx = X1 – X0;
dy = Y1 – Y0;
• // Depending upon absolute value of dx & dy
// choose number of steps to put pixel as
• // steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy)
steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
• // calculate increment in x & y for each steps
• Xinc = dx / (float) steps;
Yinc = dy / (float) steps;
• // Put pixel for each step
• X = X0;
Y = Y0;
• for (int i = 0; i <= steps; i++)
{
putpixel (round(X),round(Y),WHITE);
X += Xinc;
Y += Yinc;
}
3
• Advantages of DDA Algorithm:
• It is a simple and easy-to-implement algorithm.
• It avoids using multiple operations which have high time complexities.
• It is faster than the direct use of the line equation because it does not use
any floating point multiplication and it calculates points on the line.
• Disadvantages of DDA Algorithm:
• It deals with the rounding off operation and floating point arithmetic so it has
high time complexity.
• As it is orientation-dependent, so it has poor endpoint accuracy.
• Due to the limited precision in the floating point representation, it produces a
cumulative error.
4
Bresenham’s Line Generation Algorithm
• Step1: Start Algorithm
• Step2: Declare variable x1,x2,y1,y2,d,i1,i2,dx,dy
• Step3: Enter value of x1,y1,x2,y2
Where x1,y1are coordinates of starting point
And x2,y2 are coordinates of Ending point
• Step4: Calculate dx = x2-x1
Calculate dy = y2-y1
Calculate i1=2*dy
Calculate i2=2*(dy-dx)
Calculate d=i1-dx
• Step5: Consider (x, y) as starting point and xendas maximum possible value of x.
If dx < 0
Then x = x2
y = y2
xend=x1
If dx > 0
Then x = x1
y = y1
xend=x2
• Step6: Generate point at (x,y)coordinates.
•
5
• Step7: Check if whole line is generated.
If x > = xend
Stop.
• Step8: Calculate co-ordinates of the next pixel
If d < 0
Then d = d + i1
If d ≥ 0
Then d = d + i2
Increment y = y + 1
• Step9: Increment x = x + 1
• Step10: Draw a point of latest (x, y) coordinates
• Step11: Go to step 7
• Step12: End of Algorithm
6

Line drawing Algorithm DDA in computer Graphics.pdf

  • 1.
    18CSE338J – ArtificialIntelligence Unit – 1 : Session –5 : SLO -1,2 Line drawing Algorithm DDA, Bresenhams 1
  • 2.
    DDA Line drawingalgorithm • The steps involved in DDA line generation algorithm are: • Input the two endpoints of the line segment, (x1,y1) and (x2,y2). • Calculate the difference between the x-coordinates and y-coordinates of the endpoints as dx and dy respectively. • Calculate the slope of the line as m = dy/dx. • Set the initial point of the line as (x1,y1). • Loop through the x-coordinates of the line, incrementing by one each time, and calculate the corresponding y-coordinate using the equation y = y1 + m(x – x1). • Plot the pixel at the calculated (x,y) coordinate. • Repeat steps 5 and 6 until the endpoint (x2,y2) is reached. • DDA algorithm is relatively easy to implement and is computationally efficient, making it suitable for real-time applications. However, it has some limitations, such as the inability to handle vertical lines and the need for floating-point arithmetic, which can be slow on some systems. Nonetheless, it remains a popular choice for generating lines in computer graphics. • 2
  • 3.
    • DDA Algorithm: •Consider one point of the line as (X0, Y0) and the second point of the line as (X1, Y1). • // calculate dx , dy • dx = X1 – X0; dy = Y1 – Y0; • // Depending upon absolute value of dx & dy // choose number of steps to put pixel as • // steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy) steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy); • // calculate increment in x & y for each steps • Xinc = dx / (float) steps; Yinc = dy / (float) steps; • // Put pixel for each step • X = X0; Y = Y0; • for (int i = 0; i <= steps; i++) { putpixel (round(X),round(Y),WHITE); X += Xinc; Y += Yinc; } 3
  • 4.
    • Advantages ofDDA Algorithm: • It is a simple and easy-to-implement algorithm. • It avoids using multiple operations which have high time complexities. • It is faster than the direct use of the line equation because it does not use any floating point multiplication and it calculates points on the line. • Disadvantages of DDA Algorithm: • It deals with the rounding off operation and floating point arithmetic so it has high time complexity. • As it is orientation-dependent, so it has poor endpoint accuracy. • Due to the limited precision in the floating point representation, it produces a cumulative error. 4
  • 5.
    Bresenham’s Line GenerationAlgorithm • Step1: Start Algorithm • Step2: Declare variable x1,x2,y1,y2,d,i1,i2,dx,dy • Step3: Enter value of x1,y1,x2,y2 Where x1,y1are coordinates of starting point And x2,y2 are coordinates of Ending point • Step4: Calculate dx = x2-x1 Calculate dy = y2-y1 Calculate i1=2*dy Calculate i2=2*(dy-dx) Calculate d=i1-dx • Step5: Consider (x, y) as starting point and xendas maximum possible value of x. If dx < 0 Then x = x2 y = y2 xend=x1 If dx > 0 Then x = x1 y = y1 xend=x2 • Step6: Generate point at (x,y)coordinates. • 5
  • 6.
    • Step7: Checkif whole line is generated. If x > = xend Stop. • Step8: Calculate co-ordinates of the next pixel If d < 0 Then d = d + i1 If d ≥ 0 Then d = d + i2 Increment y = y + 1 • Step9: Increment x = x + 1 • Step10: Draw a point of latest (x, y) coordinates • Step11: Go to step 7 • Step12: End of Algorithm 6