Introduction to Line Drawing Algorithm

Line-Drawing-Algorithm

Introduction to Line Drawing Algorithm

In this article, we will see an outline on the Line Drawing Algorithm. Line drawing on the computer means the computer screen is dividing into two parts rows and columns. Those rows and columns are also known as Pixels. In case we have to draw a line on the computer, first of all, we need to know which pixels should be on. A line is a part of a straight line that extends in the opposite direction indefinitely. The line is defined by two Endpoints. Its density should be separate from the length of the line.
The formula for a line interception of the slope:Y = mx + b
In this formula, m is a line of the slope and b is intercept of y in the line. In positions (x1, y1) and (x2, y2), two endpoints are specified for the line segment.

line segment

The value of slope m and b can be determined accordingly
โ€ข m = y2 โ€“ y1 / x2 โ€“ x1
โ€ข i.e. M = ฮ”y / ฮ”x

Example: 

Line endpoints are (0,0) and (4,12). Plot the result to calculate each value of y as the x steps of 0 to 4.

Solution: 

So we have a formula of equation of line:Y = mx + b
โ€ข m = y2 โ€“ y1 / x2 โ€“ x1
โ€ขm = 12 โ€“ 0 / 4 โ€“ 0
โ€ข m = 3
The y intercept b is then found by linking y1 and x1 to the y = 3 x + b formula, 0 = 3(0) + b. Therefore, b = 0, so the y = 3x line formula.
The goal is to determine the next x, y location as quickly as possible by the previous one.

Types of Line Drawing Algorithm

Below given are the types of the algorithm:

1. Digital Differential Algorithm ( DDA)

An incremental conversion method is a DDA Algorithm and also we called Digital Differential Algorithm (DDA). This approach is characterized by the use of the results from the previous stage in each calculation.

Let us look at the examples given below:

Example #1

End point line are (x1 , y1) and (x2, y2) Popular Course in this categoryAll in One Software Development Bundle (600+ Courses, 50+ projects)600+ Online Courses | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (3,144 ratings)Course Price
โ‚น8999 โ‚น125000
View Course


Related CoursesWindows 10 Training (4 Courses, 4+ Projects)JWS Java Web Services Training (4 Courses, 11 Projects)Java Training (40 Courses, 29 Projects, 4 Quizzes)

  • dx = x2 โ€“ x1
  • dy = y2 โ€“ y1

so, now we will determine the length of the line if abs(dx) >= abs(dy) then length = abs (dx) else length = abs (dy)

  • ฮ”x = dx / length
  • ฮ”y = dy / length
  • X = x1
  • Y = y1

Setpixel (round (x), round (y));

  • i = 1

while ( i <= length )

  • x = x + ฮ”x;
  • y = y + ฮ”y;

setpixel ( round (x) , round (y) );
i = i + 1
end while

Example #2

A line of end points (5, 4) & (6, 9) can be converted with the DDA.

Solution:
โ€ข dx = x2 โ€“ x1
โ€ข dx = 6 โ€“ 5 = 1
โ€ข dy = y2 โ€“ y1
โ€ข dy = 9 โ€“ 4

dy= 5
As, dx < dy then,
โ€ข length = y2 โ€“ y1 = 5
โ€ข dx = (x2 โ€“ x1) / length = 1/5 = 0.2
โ€ข dy = (y2- y1) / length = 5/5 = 1
x1 y1 x2 y2 L dx dy i x y

Result:

X1Y1X2Y2LDxDyIXYResult
32475.2103.55.53.5, 5.5
13.91.53.9,1.5
24.93.54.9,3.5
35.17.55.1,7.5
43.74.53.7,4.5
54.57.54.5,7.5

Limitation of DDA Algorithm

  •  Arithmetic for floating and rounding points are time-consuming procedures.
  •  A round-off error may lead to a distance from the true long-line segment path by the measured pixel location.

2. The Bresenham Line Algorithm

The Scan conversion algorithm is Bresenham-algorithm. This algorithm offers the main advantage of using only integer calculations.

1. Endpoints of the row and store the left endpoint in (x1, y1)
2. For the decision parameter to get the first value ฮ”x i.e. Dx, ฮ”y i.e. Dy, 2 ฮ”y and 2 ฮ”x.
3. Initialize starting
4. Initialize i =1 as a counter,
Otherwise, the next point to plot is (xk + 1, yk + 1) and Repeat step 4 ( ฮ”x โ€“ 1 ) times.
Adjustment
For m > 1, we can say if we increase x every time we increase y.
After the decision variable pk will be resolved the formula is very similar, only the x and y in the equation will be replaced.

Bresenham Line Algorithm Summary

These are the following advantages to the Bresenham line algorithm:
โ€ข A fast incremental algorithm.
โ€ข This uses only integer calculations.
DDA has the following problems when compared with the DDA algorithm:
โ€ข The pixelated line can be distant from the expected accumulation of round-off errors.
โ€ข Time is required for rounding operations and floating points arithmetic.

Recommended Articles

This has been a guide to Line Drawing Algorithm. Here we discuss what is line drawing algorithm along with the various examples. You may also have a look at the following articles to learn moreโ€“

  1. Machine Learning Algorithms
  2. SVM Algorithm
  3. Simple Linear Regression
  4. Multivariate Regression

Leave a Comment

Your email address will not be published. Required fields are marked *