Open In App

Illustration for tracing all the 8 octaves in Bresenham’s line algorithm

Last Updated : 28 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Bresenham’s Line Generation Algorithm

In this article, we will understand how Bresenham’s Line Algorithm is used to find all the octaves.

While understanding the algorithm considers only the case where coordinate of the two points P0(x0, y0) and P1(x1, y1) such that x0 < x1 and y0 < y1. The task is to find all the intermediate points required for drawing a line P0(x0, y0) and P1(x1, y1) on the computer screen of pixels.

Below is the algorithm used in Bresenham’s Line Algorithm:

plotLine(x0, y0, x1, y1)
    dx = x1 - x0
    dy = y1 - y0
    D = 2*dy - dx
    DE = 2*dy
    DNE = 2(dy - dx)
    y = y0
    x = x0
    plot(x, y)
    while (x < x1)
        if D > 0
            D = D + DNE
            y = y + 1
        else
               D = D + DE
        end if
        x = x + 1
        plot(x, y)
    end while

The following are the changes that should be done to draw a line in other octaves:

  • Octave 1: None.
  • Octave 2: Switch roles of x and y.
  • Octave 3: Switch roles of x and y; Use rule(8).
  • Octave 4: Draw from P1 to P0; Use rule(8).
  • Octave 5: Draw from P1 to P0.
  • Octave 6: Draw from P1 to P0; Use rule(2).
  • Octave 7: Switch roles of x and y; Use rule(4).
  • Octave 8: Use y = y – 1.

Below is the illustration for the first octave:

P0 = (5, 8) and  P1 = (9, 11)
dx = x1 – x0 = 9 – 5 = 4
dy = y1 – y0 = 11 – 8 = 3
D = 2*dy – dx = 2(3) – 4 = 2
DE = 2*dy = 2*3 = 6
DNE = 2(dy – dx) = 2(3 – 4) = -2

Initially, plot (x0, y0) which is (5, 8)

  • plot(5, 8)
  • D = 2 -> NE is selected -> plot(6, 9) -> D = 2 + DNE = 2 – 2 = 0.
  • D = 0 -> E is selected -> plot(7, 9) -> D = 2 + DE = 0 + 6 = 6.
  • D = 6 -> NE is selected -> plot(8, 10) -> D = 6+ DNE = 6 – 2 = 4.
  • D = 4 -> NE is selected -> plot(9, 11) -> D = 4 + DNE = 4 – 2 = 2.
  • x = x1 so exit while loop.

The points plotted are (5, 8), (6, 9), (7, 9), (8, 10), (9, 11).

Below is the illustration for the second octave:

P0 = (2, 6) and  P1 = (5, 15)

=> Perform the following steps for the third octave:

  • Switch roles of x and y mean swap(x0, y0) and swap(x1, y1) and while plotting we have to plot(y, x)
  • New points become P0 = (6, 2) and  P1 = (15, 5)

dx = x1 – x0 = 15 – 6 = 9
dy = y1 – y0 = 5 – 2 = 3
D = 2*dy – dx = 2(3) – 9 = 6 – 9 = -3
DE = 2*dy = 2*3 = 6
DNE = 2(dy – dx) = 2(3 – 9) = -12

Initially, we have to plot (y0, x0) which is (2, 6)

  • Plot(2, 6).
  • D = -3 -> E is selected -> plot(2, 7) -> D = -3 + DE = -3 + 6 = 3.
  • D = 3 -> NE is selected -> plot(3, 8).-> D = 3 + DNE = 3 + -12 = -9.
  • D = -9 -> E is selected -> plot(3, 9) -> D = -9 + DE = -9 + 6 = -3.
  • D = -3 -> E is selected -> plot(3, 10) -> D = -3 + DE = -3 + 6 = 3.
  • D = 3 -> NE is selected -> plot(4, 11).-> D = 3 + DNE = 3 + -12 = -9.
  • D = -9 -> E is selected -> plot(4, 12) -> D = -9 + DE = -9 + 6 = -3.
  • D = -3 -> E is selected -> plot(4, 13) -> D = -3 + DE = -3 + 6 = 3.
  • D = 3 -> NE is selected -> plot(5, 14).-> D = 3 + DNE = 3 + -12 = -9.
  • D = -9 -> E is selected -> plot(5, 15) -> D = -9 + DE = -9 + 6 = -3.
  • x = x1 so exit while loop.

The points plotted are (2, 6), (2, 7), (3, 8), (3, 9), (3, 10), (4, 11), (4, 12), (4, 13), (5, 14), (5, 15).

Below is the illustration for the third octave:

P0 = (-2, 4) and P1 = (-6, 12)

Perform the following steps for the third octave:

  • Switch roles of x and y = swap(x0, y0) and swap(x1, y1) and while plotting we have to plot(y, x)
  • Use y = y – 1 = we have to use y = y – 1 instead of y = y + 1 and dy = -dy.
  • New points become P0 = (4, -2) and  P1 = (12, -6)

dx = x1 – x0 = 12 – 4 = 8
dy = y1 – y0 = -6 – (-2) = -4

We have to make dy = -dy. Therefore, dy = +4

D = 2*dy – dx = 2(4) – 8 = 8 – 8 = 0
DE = 2*dy = 2*4 = 8
DNE = 2(dy – dx) = 2(4 – 8) = -8

Initially, we have to plot (y0, x0) which is (-2, 4)

  • Plot(-2, 4).
  • D = 0 -> E is selected -> plot(-2, 5) -> D = 0 + DE = 0 + 8 = 8.
  • D = 8 -> NE is selected -> plot(-3, 6).-> D = 8 + DNE = 8 + -8 = 0.
  • D = 0 -> E is selected -> plot(-3, 7) -> D = 0 + DE = 0 + 8 = 8.
  • D = 8 -> NE is selected -> plot(-4, 8).-> D = 8 + DNE = 8 + -8 = 0.
  • D = 0 -> E is selected -> plot(-4, 9) -> D = 0 + DE = 0 + 8 = 8.
  • D = 8 -> NE is selected -> plot(-5, 10).-> D = 8 + DNE = 8 + -8 = 0.
  • D = 0 -> E is selected -> plot(-5, 11) -> D = 0 + DE = 0 + 8 = 8.
  • D = 8 -> NE is selected -> plot(-6, 12).-> D = 8 + DNE = 8 + -8 = 0.
  • x = x1 so exit while loop.

The points plotted are (-2, 4), (-2, 5), (-3, 6), (-3, 7), (-4, 8), (-4, 9), (-5, 10), (-5, 11), (-6, 12).

Below is the illustration for the fourth octave:

P0 = (-2, 1) and  P1 = (-6, 3)

Perform the following steps for the fourth octave:

  • Draw from P1 to P0 = swap(P0, P1).
  • Use y = y – 1 = we have to use y = y – 1 instead of y = y + 1 and dy = -dy.
  • New points become P0 = (-6, 3) and  P1 = (-2, 1) */

dx = x1 – x0 = -2 – (-6) = 4
dy = y1 – y0 = 1 – 3 = -2

We have to make dy = -dy. Therefore, dy = +2

D = 2*dy – dx = 2(2) – 4 = 4 – 4 = 0
DE = 2*dy = 2*2 = 4
DNE = 2(dy – dx) = 2(2 – 4) = -4

Initially, we have to plot (x0, y0) which is (-6, 3)

  • Plot(-6, 3).
  • D = 0 -> E is selected -> plot(-5, 3) -> D = 0 + DE = 0 + 4 = 4.
  • D = 4 -> NE is selected -> plot(-4, 2).-> D = 4 + DNE = 4 + -4 = 0.
  • D = 0 -> E is selected -> plot(-3, 2) -> D = 0 + DE = 0 + 4 = 4.
  • D = 4 -> NE is selected -> plot(-2, 1).-> D = 4 + DNE = 4 + -4 = 0.
  • x = x1 so exit while loop.

The points plotted are (-6, 3), (-5, 3), (-4, 2), (-3, 2), (-2, 1).

Below is the illustration for the fifth octave:

P0 = (-3, -1) and  P1 = (-9, -3)

Perform the following steps for the fifth octave:

  • Draw from P1 to P0 = swap(P0, P1).
  • New points become P0 = (-9, -3) and  P1 = (-3, -1)

dx = x1 – x0 = -3 – (-9) = 6
dy = y1 – y0 = -1 – (-3) = 2
D = 2*dy – dx = 2(2) – 6 = 4 – 6 = -2
DE = 2*dy = 2*2 = 4
DNE = 2(dy – dx) = 2(2 – 6) = -8

Initially, we have to plot (x0, y0) which is (-9, -3)

  • Plot(-9, -3).
  • D = -2 -> E is selected -> plot(-8, -3) -> D = -2 + DE = -2 + 4 = 2.
  • D = 2 -> NE is selected -> plot(-7, -2).-> D = 2 + DNE = 2 + -8 = -6.
  • D = -6 -> E is selected -> plot(-6, -2) -> D = -6 + DE = -6 + 4 = -2.
  • D = -2 -> E is selected -> plot(-5, -2) -> D = -2 + DE = -2 + 4 = 2.
  • D = 2 -> NE is selected -> plot(-4, -1).-> D = 2 + DNE = 2 + -8 = -6.
  • D = -6 -> E is selected -> plot(-3, -1) -> D = -6 + DE = -6 + 4 = -2.
  • x = x1 so exit while loop.

The points plotted are (-9, -3), (-8, -3), (-7, -2), (-6, -2), (-5, -2), (-4, -1), (-3, -1).

Below is the illustration for the sixth octave:

P0 = (-1, -2) and  P1 = (-3, -6)

Perform the following steps for the sixth octave:

  • Draw from P1 to P0 = swap(P0, P1).
  • Switch roles of x and y = swap(x0, y0) and swap(x1, y1) and while plotting we have to plot(y, x).
  • New points become P0 = (-6, -3) and  P1 = (-2, -1)

dx = x1 – x0 = -2 – (-6) = 4
dy = y1 – y0 = -1 – (-3) = 2
D = 2*dy – dx = 2(2) – 4 = 4 – 4 = 0
DE = 2*dy = 2*2 = 4
DNE = 2(dy – dx) = 2(2 – 4) = -4

Initially, we have to plot (y0, x0) which is (-3, -6)

  • Plot(-3, -6).
  • D = 0 -> E is selected -> plot(-3, -5) -> D = 0 + DE = 0 + 4 = 4.
  • D = 4 -> NE is selected -> plot(-2, -4).-> D = 4 + DNE = 4 + -4 = 0.
  • D = 0 -> E is selected -> plot(-2, -3) -> D = 0 + DE = 0 + 4 = 4.
  • D = 4 -> NE is selected -> plot(-1, -2).-> D = 4 + DNE = 4 + -4 = 0.
  • x = x1 so exit while loop.

The points plotted are (-3, -6), (-3, -5), (-2, -4), (-2, -3), (-1, -2).

Below is the illustration for the seventh octave:

P0 = (2, -5) and  P1 = (4, -10)

Perform the following steps for the seventh octave:

  • Switch roles of x and y = swap(x0, y0) and swap(x1, y1) and while plotting we have to plot(y, x).
  • Draw from P1 to P0 = swap(P0, P1).
  • Use y = y – 1 = we have to use y = y – 1 instead of y = y + 1 and dy = -dy.
  • New points become P0 = (-10, 4) and  P1 = (-5, 2)

dx = x1 – x0 = -5 -(-10) = 5
dy = y1 – y0 = 2 – 4 = -2

We have to make dy = -dy. Therefore, dy = +2

D = 2*dy – dx = 2(2) – 5 = 4 – 5 = -1
DE = 2*dy = 2(2) = 4
DNE = 2(dy – dx) = 2(2 – 5) = -6

Initially, we have to plot (y0, x0) which is (4, -10)

  • Plot(4, -10).
  • D = -1 -> E is selected -> plot(4, -9) -> D = -1 + DE = -1 + 4 = 3.
  • D = 3 -> NE is selected -> plot(3, -8).-> D = 3 + DNE = 3 + -6 = -3.
  • D = -3 -> E is selected -> plot(3, -7) -> D = -3 + DE = -3 + 4 = 1.
  • D = 1 -> NE is selected -> plot(2, -6).-> D = 1 + DNE = 1 + -6 = -5.
  • D = -5 -> E is selected -> plot(2, -5) -> D = -5 + DE = -5 + 4 = -1.
  • x = x1 so exit while loop.

The points plotted are (-4, 10), (-4, 9), (-3, 8), (-3, 7), (-2, 6), (-2, 5).

Below is the illustration for the eighth octave:

P0 = (3, -2) and  P1 = (6, -4)

Perform the following steps for the eighth octave:

  • Use y = y – 1 = we have to use y = y – 1 instead of y = y + 1 and dy = -dy.

dx = x1 – x0 = 6 – 3 = 3
dy = y1 – y0 = -4 – (-2) = -2

We have to make dy = -dy. Therefore, dy = +2

D = 2*dy – dx = 2(2) – 3 = 4 – 3 = 1
DE = 2*dy = 2*2 = 4
DNE = 2(dy – dx) = 2(2 – 3) = -2

Initially, we have to plot (x0, y0) which is (3, -2)

  • Plot(3, -2).
  • D = 1 -> NE is selected -> plot(4, -3) -> D = 1 + DNE = 1 + (-2) = -1.
  • D = -1 -> E is selected -> plot(5, -3).-> D = -1 + DE = -1 + 4 = 3.
  • D = 3 -> NE is selected -> plot(6, -4) -> D = 3 + DNE = 3 + -2 = 1.
  • x = x1 so exit while loop.

The points plotted are (3, -2), (4, -3), (5, -3), (6, -4).



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads