先决条件: Bresenham 的线生成算法
在本文中,我们将了解如何使用 Bresenham’s Line Algorithm 来查找所有八度音程。
在理解算法的同时,仅考虑两点P 0 (x 0 , y 0 )和P 1 (x 1 , y 1 ) 的坐标使得x 0 < x 1和y 0 < y 1 的情况。任务是在像素的计算机屏幕上找到绘制直线 P 0 (x 0 , y 0 )和P 1 (x 1 , y 1 )所需的所有中间点。
下面是 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
以下是在其他八度音阶中绘制一条线应进行的更改:
- 八度音阶:无。
- Octave 2:交换 x 和 y 的角色。
- Octave 3:切换x和y的角色;使用规则(8)。
- 八度音阶:从 P1 画到 P0;使用规则(8)。
- 八度音阶:从 P1 到 P0。
- 八度音阶:从 P1 画到 P0;使用规则(2)。
- Octave 7:交换 x 和 y 的角色;使用规则(4)。
- 八度音阶:使用 y = y – 1。
下面是第一个八度的图示:
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
最初,绘制(x 0 , y 0 )即(5, 8)
- 情节(5, 8)
- D = 2 -> NE 被选中 -> plot(6, 9) -> D = 2 + DNE = 2 – 2 = 0。
- D = 0 -> E 被选中 -> plot(7, 9) -> D = 2 + DE = 0 + 6 = 6。
- D = 6 -> NE 被选中 -> plot(8, 10) -> D = 6+ DNE = 6 – 2 = 4。
- D = 4 -> NE 被选中 -> plot(9, 11) -> D = 4 + DNE = 4 – 2 = 2。
- x = x1 所以退出 while 循环。
绘制的点是 (5, 8), (6, 9), (7, 9), (8, 10), (9, 11)。
下面是第二个八度的图示:
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
最初,我们必须绘制(y 0 , x 0 )即(2, 6)
- 绘图(2, 6)。
- D = -3 -> E 被选中 -> plot(2, 7) -> D = -3 + DE = -3 + 6 = 3。
- D = 3 -> NE 被选中 -> plot(3, 8).-> D = 3 + DNE = 3 + -12 = -9。
- D = -9 -> E 被选中 -> plot(3, 9) -> D = -9 + DE = -9 + 6 = -3。
- D = -3 -> E 被选中 -> plot(3, 10) -> D = -3 + DE = -3 + 6 = 3。
- D = 3 -> NE 被选择 -> plot(4, 11).-> D = 3 + DNE = 3 + -12 = -9。
- D = -9 -> E 被选中 -> plot(4, 12) -> D = -9 + DE = -9 + 6 = -3。
- D = -3 -> E 被选中 -> plot(4, 13) -> D = -3 + DE = -3 + 6 = 3。
- D = 3 -> NE 被选择 -> plot(5, 14).-> D = 3 + DNE = 3 + -12 = -9。
- D = -9 -> E 被选中 -> plot(5, 15) -> D = -9 + DE = -9 + 6 = -3。
- x = x1 所以退出 while 循环。
绘制的点是 (2, 6), (2, 7), (3, 8), (3, 9), (3, 10), (4, 11), (4, 12), (4, 13) ), (5, 14), (5, 15)。
下面是第三个八度的图示:
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
最初,我们必须绘制 (y 0 , x 0 ) 即 (-2, 4)
- 绘图(-2, 4)。
- D = 0 -> E 被选中 -> plot(-2, 5) -> D = 0 + DE = 0 + 8 = 8。
- D = 8 -> NE 被选中 -> plot(-3, 6).-> D = 8 + DNE = 8 + -8 = 0。
- D = 0 -> E 被选中 -> plot(-3, 7) -> D = 0 + DE = 0 + 8 = 8。
- D = 8 -> NE 被选中 -> plot(-4, 8).-> D = 8 + DNE = 8 + -8 = 0。
- D = 0 -> E 被选中 -> plot(-4, 9) -> D = 0 + DE = 0 + 8 = 8。
- D = 8 -> NE 被选中 -> plot(-5, 10).-> D = 8 + DNE = 8 + -8 = 0。
- D = 0 -> E 被选中 -> plot(-5, 11) -> D = 0 + DE = 0 + 8 = 8。
- D = 8 -> NE 被选中 -> plot(-6, 12).-> D = 8 + DNE = 8 + -8 = 0。
- x = x1 所以退出 while 循环。
绘制的点是 (-2, 4), (-2, 5), (-3, 6), (-3, 7), (-4, 8), (-4, 9), (-5, 10), (-5, 11), (-6, 12)。
下面是第四个八度的图示:
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
最初,我们必须绘制 (x0, y0) 即 (-6, 3)
- 绘图(-6, 3)。
- D = 0 -> E 被选中 -> plot(-5, 3) -> D = 0 + DE = 0 + 4 = 4。
- D = 4 -> NE 被选中 -> plot(-4, 2).-> D = 4 + DNE = 4 + -4 = 0。
- D = 0 -> E 被选中 -> plot(-3, 2) -> D = 0 + DE = 0 + 4 = 4。
- D = 4 -> NE 被选中 -> plot(-2, 1).-> D = 4 + DNE = 4 + -4 = 0。
- x = x1 所以退出 while 循环。
绘制的点是 (-6, 3), (-5, 3), (-4, 2), (-3, 2), (-2, 1)。
下面是第五个八度的图示:
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
最初,我们必须绘制 (x0, y0) 即 (-9, -3)
- 绘图(-9,-3)。
- D = -2 -> E 被选中 -> plot(-8, -3) -> D = -2 + DE = -2 + 4 = 2。
- D = 2 -> NE 被选择 -> plot(-7, -2).-> D = 2 + DNE = 2 + -8 = -6。
- D = -6 -> E 被选中 -> plot(-6, -2) -> D = -6 + DE = -6 + 4 = -2。
- D = -2 -> E 被选中 -> plot(-5, -2) -> D = -2 + DE = -2 + 4 = 2。
- D = 2 -> NE 被选中 -> plot(-4, -1).-> D = 2 + DNE = 2 + -8 = -6。
- D = -6 -> E 被选中 -> plot(-3, -1) -> D = -6 + DE = -6 + 4 = -2。
- x = x1 所以退出 while 循环。
绘制的点是 (-9, -3), (-8, -3), (-7, -2), (-6, -2), (-5, -2), (-4, -1 ), (-3, -1)。
下面是第六个八度的图示:
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
最初,我们必须绘制 (y0, x0) 即 (-3, -6)
- 绘图(-3,-6)。
- D = 0 -> E 被选中 -> plot(-3, -5) -> D = 0 + DE = 0 + 4 = 4。
- D = 4 -> NE 被选中 -> plot(-2, -4).-> D = 4 + DNE = 4 + -4 = 0。
- D = 0 -> E 被选中 -> plot(-2, -3) -> D = 0 + DE = 0 + 4 = 4。
- D = 4 -> NE 被选中 -> plot(-1, -2).-> D = 4 + DNE = 4 + -4 = 0。
- x = x1 所以退出 while 循环。
绘制的点是 (-3, -6), (-3, -5), (-2, -4), (-2, -3), (-1, -2)。
下面是第七个八度的图示:
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
最初,我们必须绘制 (y0, x0) 即 (4, -10)
- 绘图(4,-10)。
- D = -1 -> E 被选中 -> plot(4, -9) -> D = -1 + DE = -1 + 4 = 3。
- D = 3 -> NE 被选择 -> plot(3, -8).-> D = 3 + DNE = 3 + -6 = -3。
- D = -3 -> E 被选中 -> plot(3, -7) -> D = -3 + DE = -3 + 4 = 1。
- D = 1 -> NE 被选中 -> plot(2, -6).-> D = 1 + DNE = 1 + -6 = -5。
- D = -5 -> E 被选中 -> plot(2, -5) -> D = -5 + DE = -5 + 4 = -1。
- x = x1 所以退出 while 循环。
绘制的点是 (-4, 10), (-4, 9), (-3, 8), (-3, 7), (-2, 6), (-2, 5)。
下面是第八个八度的图示:
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
最初,我们必须绘制 (x 0 , y0) 即 (3, -2)
- 绘图(3,-2)。
- D = 1 -> NE 被选中 -> plot(4, -3) -> D = 1 + DNE = 1 + (-2) = -1。
- D = -1 -> E 被选中 -> plot(5, -3).-> D = -1 + DE = -1 + 4 = 3。
- D = 3 -> NE 被选中 -> plot(6, -4) -> D = 3 + DNE = 3 + -2 = 1。
- x = x1 所以退出 while 循环。
绘制的点是 (3, -2), (4, -3), (5, -3), (6, -4)。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。