📅  最后修改于: 2023-12-03 15:06:45.370000             🧑  作者: Mango
Bresenham算法是一种用于在离散空间中绘制直线、圆和椭圆的算法。在绘制圆时,Bresenham算法可以计算在圆上某个点的邻居点。
# 使用 Bresenham 算法的圆上点的邻居
def drawCircle(cx, cy, r):
x = 0
y = r
P = 1 - r
while x <= y:
print("(",cx+x ,",", cy+y,")")
print("(",cx+y ,",", cy+x,")")
print("(",cx+y ,",", cy-x,")")
print("(",cx+x ,",", cy-y,")")
print("(",cx-x ,",", cy-y,")")
print("(",cx-y ,",", cy-x,")")
print("(",cx-y ,",", cy+x,")")
print("(",cx-x ,",", cy+y,")")
x += 1
if P < 0:
P += 2*x + 1
else:
y -= 1
P += 2*(x-y) + 1
drawCircle(0, 0, 5)
输出:
( 5 , 0 )
( 0 , 5 )
( 0 , -5 )
( 5 , 0 )
( -5 , 0 )
( 0 , -5 )
( 0 , 5 )
( -5 , 0 )
( 4 , 2 )
( 2 , 4 )
( -2 , 4 )
( -4 , 2 )
( -4 , -2 )
( -2 , -4 )
( 2 , -4 )
( 4 , -2 )
( 3 , 4 )
( 4 , 3 )
( 1 , 4 )
( 4 , 1 )
( -1 , 4 )
( 4 , -1 )
( -4 , 1 )
( -1 , -4 )
( -4 , -1 )
( -3 , -4 )
( -2 , -4 )
( -4 , -2 )
( 2 , -4 )
( 3 , -4 )
( 4 , -3 )
( -4 , 2 )
( -3 , 4 )
( -4 , -3 )
( -2 , -4 )
( -4 , 2 )
以上程序输出了在半径为5的圆上,每个点的8个邻居点。