100% found this document useful (1 vote)
24 views

Line Generation Algorithm

The document describes two algorithms for line generation: Digital Differential Analyzer's (DDA) algorithm and Bressenham's algorithm. DDA uses incremental calculations at each step to determine the next point, but results in lower accuracy due to rounding. Bressenham's algorithm only uses integer calculations and determines which of two pixel positions is closer to the line at each step to improve efficiency over DDA. The algorithms are then demonstrated on an example line between points A and B.

Uploaded by

Arief Rachman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
24 views

Line Generation Algorithm

The document describes two algorithms for line generation: Digital Differential Analyzer's (DDA) algorithm and Bressenham's algorithm. DDA uses incremental calculations at each step to determine the next point, but results in lower accuracy due to rounding. Bressenham's algorithm only uses integer calculations and determines which of two pixel positions is closer to the line at each step to improve efficiency over DDA. The algorithms are then demonstrated on an example line between points A and B.

Uploaded by

Arief Rachman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Line Generation Algorithm

Irfan Dwiguna Sumitra, M.Kom, Ph.D


Outlines
Digital Differential Analyzers Algorithm
Bressenhams Algorithm
DDAs Algorithm
An incremental scan conversion method
Such an approach is characterized by performing calculations at each
step using results from the preceding step
Suppose, at step i we have calculated (xi, yi) to be a point on the line
Since, the next point (xi+1, yi+1) should satisfy = + 1 and
= + 1
We have, yi + 1 = +
+ 1 = + (1)

Or x + 1 = + (2)

Algorithm:
1. Define 1 , 1 (2 , 2 )
2. Calculate: = 2 1 = 2 1
3. Determine step:
- If > , then step =
- If null, then step =
4. Calculate incremental pixel coordinates:
_ = /
_ = /
5. Further coordinates:
= + _ = + _
6. Round = , = , the pixel plot (, )
7. Repeat step 5 and 6 to obtain the next pixels position up to = 2
and = 2
Ex.
Question:
Scan convert line having end points A(2,1) and B(8.5) using DDA

Solution:
Start point 1 , 1 = (2,1) end point 2 , 2 = 8,5
= 2 1 = 8 2 = 6 and = 2 1 = 5 1 = 4
Due to : > , then step = = 6
6
_ = = =1
6
4
_ = = = 0,67
6
1st iteration: , = (2,1)
+ _ = 2 + 1 = 3
+ _ = 1 + 0,67 = 1,67
next coordinate: , = 3; 1,67
round of 3; 1,67 3,2 on screen
2nd iteration: , = 3; 1,67
+ _ = 3 + 1 = 4
+ _ = 1,67 + 0,67 = 2,34
next coordinate: , = 4; 2,34
round of 4; 2,34 4,2 on screen
3rd iteration: , = 4; 2,34
+ _ = 4 + 1 = 5
+ _ = 2,34 + 0,67 = 3,01
next coordinate: , = 5; 3,01
round of 5; 3,01 5,3 on screen
4th iteration: , = 5; 3,01
+ _ = 5 + 1 = 6
+ _ = 3,01 + 0,67 = 3,68
next coordinate: , = 6; 3,68
round of 6; 3,68 6,4 on screen
5th iteration: , = 6; 3,68
+ _ = 6 + 1 = 7
+ _ = 3,68 + 0,67 = 4,35
next coordinate: , = 7; 4,35
round of 7; 4,35 7,4 on screen
6th iteration: , = 7; 4,35
+ _ = 7 + 1 = 8
+ _ = 4,35 + 0,67 = 5,02
next coordinate: , = 8; 5,02
round of 8; 5,02 8,5 on screen
Because = 2 , then iteration stopped
Point of line generation as following : (2,1), (3,2), (4,2), (5,3), (6,4),
(7,4) and (8,5)
DDA vs Brute Force
Pro:
DDAs algorithm is faster than brute forces algorithm and are well
used for gradient > 1
No multiplications

Cons:
The procedure for drawing lines still uses the rounding function x and
y, so it takes time
Variables of x, y and m require floating point because the gradient is
in floating point, hence end point accuracy is poor
Bressenhams Line Algorithm
Developed by Bressenham
Efficient than DDA:
- Only integer calculations are involved
Determines which of two pixel positions are closer to the line path
For example:
If gradient m is 0 < < 1, start point (0 , 0 ) and end point (1 , 1 )
Suppose, the newest point , and we have to choose between
E(East) or NE (North East)
If Q is intersection point with = + 1 and M is mid point between E
and NE
If M is located above the line, then E is selected
Otherwise, if M is located below the line, then NE is selected
In term, we have to determine whether the line is above midpoint M or
below
We observe that each line can be expressed as follows:
- F , = + + = 0
- Calculate = 1 0 and = 1 0 and the line can write as,

- = + = + or + =0

- Multiply with then + = 0
- Obtained = , = and =
If , = 0, then , is on the line
If , > 0, then , is below the line
If , < 0, then , is above the line

- To apply midpoint criteria, we calculate:


1 1
= + 1, + = + 1 + + +
2 2
- To test the sign of =
If d > 0, M is below the line, then NE selected
If d = 0, select E (actually, select NE is also possible)
If d < 0, M is above the line, then E selected

For example E is selected, M increased by one step closer to x, and we have new d as

1 1
= + 2, + = + 2 + + +
2 2
1 1
= + 1, + 2 = a + 1 + + +
2
Hence, = =
So, after E is selected, incremental to obtained new d is =
For example, NE is selected, M is increased of one step closer to x
and y, so that:
3 3
= + 2, + 2 = + 2 + + 2 +
1 1
= + 1, + 2 = a + 1 + + 2 +
Hence, = + =
To begin the algorithm, start point , = 0 , 0
1
The first of midpoint is (0 + 1, 0 + )
2
1 1
= 0 + 1, 0 + 2 = 0 + 1 + 0 + 2 +
= 0 + 0 + + + /2

=0++ , because 0 , 0 is on the line
2

So, we can begin from = + =
2 2
To remove the floating point, define F with multiplication by 2
, = 2 + + = 0
This define could cause the value of d become = 2
In this way, Bressenham midpoint algorithm (for gradient 0 < < 1) are:
1. Determine the two points to be linked in the line generation
2. Determine one point as start point 0 , 0 and end point 1 , 1
3. Calculate , , 2 and 2 2
4. Calculate the parameter: 0 = 2
5. Each along the line, started with = 0
If < 0, then the next point is:
+ 1, and +1 = + 2
Otherwise, next point is:
( +1, + 1) and +1 = + 2 2
6. Repeat step no.5 to determined the next pixel positions, until:
= 1 = 1
Ex
Question:
There are two points A(2,1) and B(8,5). If A is start point and B is end point,
then make the line to connected those lines using Bressenhams
Algorithm?

Solution:
1. Start point 0 , 0 = 2,1 and end point 1 , 1 = 8,5
2. = 1 0 = 8 2 = 6 and = 1 0 = 5 1 = 4
4
3. = = 6 ; 0 1: 0 < < 1
4. 2 = 2.6 = 12 ; 2 = 2.4 = 8 2 2 = 8 12 = 4
5. 0 = 2 = 8 6 = 2
1. 1st iteration = 0
start point = 2,1
0 = 2 > 0, then the next point is
= 2 + 1 = 3 and = 1 + 1 = 2, next coordinate: 3,2
1 = 0 + 2 2 = 2 4 = 2
2. 2nd iteration = 1
start point = 3,2
1 = 2 < 0, then the next point is
= 3 + 1 = 4 and = 2, next coordinate: 4,2
2 = 1 + 2 = 2 + 8 = 6
3. 3rd iteration = 2
start point = 4,2
2 = 6 > 0, then the next point is
= 4 + 1 = 5 and = 2 + 1 = 3, next coordinate: 5,3
3 = 2 + 2 2 = 6 4 = 2
4. 4th iteration = 3
start point = 5,3
3 = 2 > 0, then the next point is
= 5 + 1 = 6 and = 3 + 1 = 4, next coordinate: 6,4
4 = 3 + 2 2 = 2 4 = 2
5. 5th iteration = 4
start point = 6,4
4 = 2 < 0, then the next point is
= 6 + 1 = 7 and = 4, next coordinate: 7,4
5 = 4 + 2 = 2 + 8 = 6
6. 6th iteration = 5
start point = 7,4
5 = 6 > 0, then the next point is
= 7 + 1 = 8 and = 4 + 1 = 5, next coordinate: 8,5
Due to = 2 = 8, then iteration is stopped
Those points to generated line are (2,1), (3,2), (4,2), (5,3), (6,4), (7,4),
and (8,5)
Other case
In case, gradient 1 < < 0, then we only replace for each
along line path, started with = 0
If < 0, then the next point is:
+ 1, and +1 = + 2
Otherwise, next point is:
( +1, 1) and +1 = + 2 2
Ex.
Question:
There are two points A(2,9) and B(8,5). If A is start point and B is end point, then
make the line to connected those lines using Bressenhams Algorithm?

Solution:
1. Start point 0 , 0 = 2,9 and end point 1 , 1 = 8,5
2. = 1 0 = 8 2 = 6 and = 1 0 = 5 9 = 4
4
3. = = 6 ; 0 1: 1 < < 0
4. 2 = 2.6 = 12 ; 2 = 2. 4 = 8 2 2 = 8 12 = 4
5. 0 = 2 = 8 6 = 2
1. 1st iteration = 0
start point = 2,9
0 = 2 > 0, then the next point is
= 2 + 1 = 3 and = 9 1 = 8, next coordinate: 3,8
1 = 0 + 2 2 = 2 4 = 2
2. 2nd iteration = 1
start point = 3,8
1 = 2 < 0, then the next point is
= 3 + 1 = 4 and = 8, next coordinate: 4,8
2 = 1 + 2 = 2 + 8 = 6
3. 3rd iteration = 2
start point = 4,8
2 = 6 > 0, then the next point is
= 4 + 1 = 5 and = 8 1 = 7, next coordinate: 5,7
3 = 2 + 2 2 = 6 4 = 2
4. 4th iteration = 3
start point = 5,7
3 = 2 > 0, then the next point is
= 5 + 1 = 6 and = 7 1 = 6, next coordinate: 6,6
4 = 3 + 2 2 = 2 4 = 2
5. 5th iteration = 4
start point = 6,6
4 = 2 < 0, then the next point is
= 6 + 1 = 7 and = 6, next coordinate: 7,6
5 = 4 + 2 = 2 + 8 = 6
6. 6th iteration = 5
start point = 7,6
5 = 6 > 0, then the next point is
= 7 + 1 = 8 and = 6 1 = 5, next coordinate: 8,5
6 = 5 + 2 2 = 6 4 = 2
Due to = 2 = 8, then iteration is stopped
Those points to generated line are (2,9), (3,8), (4,8), (5,7), (6,6), (7,6),
and (8,5)
For gradient > 1, swap x and y, thus generated line algorithm for > 1 as follows:
1. Determine the two points to be linked in the line generation
2. Determine one point as start point 0 , 0 and end point 1 , 1
3. Calculate , , 2 and 2 2
4. Calculate the parameter: 0 = 2
5. Each along the line, started with = 0
If < 0, then the next point is:
, + 1 and +1 = + 2
Otherwise, next point is:
( +1, + 1) and +1 = + 2 2
6. Repeat step no.5 to determined the next pixel positions, until:
= 1 = 1
Ex
Question:
There are two points A(2,1) and B(4,7). If A is start point and B is end point,
then make the line to connected those lines using Bressenhams
Algorithm?

Solution:
1. Start point 0 , 0 = 2,1 and end point 1 , 1 = 4,7
2. = 1 0 = 4 2 = 2 and = 1 0 = 7 1 = 6
6
3. = = 2 ; 1: > 1
4. 2 = 4 ; 2 = 12 2 2 = 4 12 = 8
5. 0 = 2 = 4 6 = 2
1. 1st iteration = 0
start point = 2,1
0 = 2 < 0, then the next point is
= 2 and = 1 + 1 = 2, next coordinate: 2,2
1 = 0 + 2 = 2 + 4 = 2
2. 2nd iteration = 1
start point = 2,2
1 = 2 > 0, then the next point is
= 2 + 1 = 3 and = 2 + 1 = 3, next coordinate: 3,3
2 = 1 + 2 2 = 2 8 = 6
3. 3rd iteration = 2
start point = 3,3
2 = 6 < 0, then the next point is
= 3 and = 3 + 1 = 4, next coordinate: 3,4
3 = 2 + 2 = 6 + 4 = 2
4. 4th iteration = 3
start point = 3,4
3 = 2 < 0, then the next point is
= 3 and = 4 + 1 = 5, next coordinate: 3,5
4 = 3 + 2 = 2 + 4 = 2
5. 5th iteration = 4
start point = 3,5
4 = 2 > 0, then the next point is
= 3 + 1 = 4 and = 5 + 1 = 6, next coordinate: 4,6
5 = 4 + 2 2 = 2 8 = 6
6. 6th iteration = 5
start point = 4,6
5 = 6 < 0, then the next point is
= 4 and = 6 + 1 = 7, next coordinate: 4,7
6 = 5 + 2 = 6 + 4 = 2
For gradient < 1, we only exchange this components
Each along line path, started with = 0
1. If < 0, then the next point is:
, 1 and +1 = + 2
2. Otherwise, next point is:
( +1, 1) and +1 = + 2 2
Ex
Question:
There are two points A(-6,10) and B(0,0). If A is start point and B is end
point, then make the line to connected those lines using Bressenhams
Algorithm?

Solution:
1. Start point 0 , 0 = 6,10 and end point 1 , 1 = 0,0
2. = 1 0 = 0 (6) = 6 and = 1 0 = 0 10 = 10
10
3. = = 6 ; 1: < 1
4. 2 = 12 ; 2 10 = 20 2 2 = 12 20 = 8
5. 0 = 2 = 12 10 = 2
1. 1st iteration = 0
start point = 6,10
0 = 2 > 0, then the next point is
= 6 + 1 = 5 and = 10 1 = 9, next coordinate: 5,9
1 = 0 + 2 2 = 2 8 = 6
2. 2nd iteration = 1
start point = 5,9
1 = 6 < 0, then the next point is
= 5 and = 9 1 = 8, next coordinate: 5,8
2 = 1 + 2 = 6 + 12 = 6
3. 3rd iteration = 2
start point = 5,8
2 = 6 > 0, then the next point is
= 5 + 1 = 4 and = 8 1 = 7, next coordinate: 4,7
3 = 2 + 2 2 = 6 8 = 2
4. 4th iteration = 3
start point = 4,7
3 = 2 < 0, then the next point is
= 4 and = 7 1 = 6, next coordinate: 4,6
4 = 3 + 2 = 2 + 12 = 10
5. 5th iteration = 4
start point = 4,6
4 = 10 > 0, then the next point is
= 4 + 1 = 3 and = 6 1 = 5, next coordinate: 3,5
5 = 4 + 2 2 = 10 8 = 2
6. 6th iteration = 5
start point = 3,5
5 = 2 > 0, then the next point is
= 3 + 1 = 2 and = 5 1 = 4, next coordinate: 2,4
6 = 5 + 2 2 = 2 8 = 6
7. 7th iteration = 6
start point = 2,4
6 = 6 < 0, then the next point is
= 2 and = 4 1 = 3, next coordinate: 2,3
7 = 6 + 2 = 6 + 12 = 6
8. 8th iteration = 7
start point = 2,3
7 = 6 > 0, then the next point is
= 2 + 1 = 1 and = 3 1 = 2, next coordinate: 1,2
8 = 7 + 2 2 = 6 8 = 2
9. 9th iteration = 8
start point = 1,2
8 = 2 < 0, then the next point is
= 1 and = 2 1 = 1, next coordinate: 1,1
9 = 8 + 2 = 2 + 12 = 10
10. 10th iteration = 9
start point = 1,1
9 = 10 > 0, then the next point is
= 1 + 1 = 0 and = 1 1 = 0, next coordinate: 0,0
10 = 9 + 2 2 = 10 8 = 2
THANK YOU

You might also like