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

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:**

X1 | Y1 | X2 | Y2 | L | Dx | Dy | I | X | Y | Result |

3 | 2 | 4 | 7 | 5 | .2 | 1 | 0 | 3.5 | 5.5 | 3.5, 5.5 |

1 | 3.9 | 1.5 | 3.9,1.5 | |||||||

2 | 4.9 | 3.5 | 4.9,3.5 | |||||||

3 | 5.1 | 7.5 | 5.1,7.5 | |||||||

4 | 3.7 | 4.5 | 3.7,4.5 | |||||||

5 | 4.5 | 7.5 | 4.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โ