中点画圆算法python
时间: 2023-09-16 08:13:06 浏览: 193
以下是使用Python实现中点画圆算法的示例代码:
```python
import pygame
def mid_point_circle(x_centre, y_centre, radius):
x = radius
y = 0
pygame.draw.circle(screen, (255, 255, 255), (x+x_centre, y+y_centre), 1)
if radius > 0:
pygame.draw.circle(screen, (255, 255, 255), (x+x_centre, -y+y_centre), 1)
pygame.draw.circle(screen, (255, 255, 255), (y+x_centre, x+y_centre), 1)
pygame.draw.circle(screen, (255, 255, 255), (-y+x_centre, x+y_centre), 1)
p = 1 - radius
while x > y:
y += 1
if p <= 0:
p += 2*y + 1
else:
x -= 1
p += 2*y - 2*x + 1
if x < y:
break
pygame.draw.circle(screen, (255, 255, 255), (x+x_centre, y+y_centre), 1)
pygame.draw.circle(screen, (255, 255, 255), (-x+x_centre, y+y_centre), 1)
pygame.draw.circle(screen, (255, 255, 255), (x+x_centre, -y+y_centre), 1)
pygame.draw.circle(screen, (255, 255, 255), (-x+x_centre, -y+y_centre), 1)
if x != y:
pygame.draw.circle(screen, (255, 255, 255), (y+x_centre, x+y_centre), 1)
pygame.draw.circle(screen, (255, 255, 255), (-y+x_centre, x+y_centre), 1)
pygame.draw.circle(screen, (255, 255, 255), (y+x_centre, -x+y_centre), 1)
pygame.draw.circle(screen, (255, 255, 255), (-y+x_centre, -x+y_centre), 1)
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption("Mid-point Circle Algorithm")
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 0))
mid_point_circle(320, 240, 100)
pygame.display.update()
pygame.quit()
```
这个示例代码使用了Pygame库来绘制图形。 `mid_point_circle` 函数接受三个参数:圆心的x和y坐标以及半径。它使用中点画圆算法来绘制圆。在这个函数中,我们首先绘制圆的四个象限的第一个像素点,然后在while循环中绘制其他点。在绘制完整个圆之后,我们使用 `pygame.display.update()` 来更新屏幕。在主循环中,我们使用 `pygame.event.get()` 来处理事件并退出程序。
阅读全文