擅长:python、mysql、java
<p>不绘制单个像素,只需添加一条线来连接彼此对应的像素(可以是-x和+x,也可以是-y和+y)。对于每个Bresenham步骤,您将绘制四条线(每条线连接两个八分音符)。你知道吗</p>
<p>这是您的改编样本代码。我放弃了点数组,而是直接画线。我还添加了定义圆心的<code>cx</code>和<code>cy</code>变量。在代码中,有时使用负索引。这只是巧合,因为圆在中心:</p>
<pre class="lang-py prettyprint-override"><code>from PIL import Image, ImageDraw
radius = 100 # radius of circle
xpts = [] # array to hold x pts
ypts = [] # array to hold y pts
img = Image.new('RGB', (1000, 1000))
draw = ImageDraw.Draw(img) # to use draw.line()
pixels = img.load()
d = (5 / 4) - radius
x = 0
y = radius
cx = 500
cy = 500
def draw_scanlines(x, y):
color = (255, 255, 0)
draw.line((cx - x, cy + y, cx + x, cy + y), fill=color)
draw.line((cx - x, cy - y, cx + x, cy - y), fill=color)
draw.line((cx - y, cy + x, cx + y, cy + x), fill=color)
draw.line((cx - y, cy - x, cx + y, cy - x), fill=color)
draw_scanlines(x, y)
while x < y:
if d < 0:
d += (2 * x + 3)
x += 1
else:
d += (2 * (x - y) + 5)
x += 1
y -= 1
draw_scanlines(x, y)
img.show()
</code></pre>