抗锯齿 Arc Pygame [英] Anti-aliased Arc Pygame
问题描述
我正在尝试使用 Pygame 在 Python 中编写一个简单的圆形计时器.目前它看起来像这样:
I am trying to code a simple circle timer in Python using Pygame. At the moment it looks like this:
如您所见,蓝线呈波浪状,其中有白点.我通过使用 pygame.draw.arc() 函数来实现这条蓝线,但它没有抗锯齿,看起来很糟糕.我希望它是抗锯齿的,但是应该让我实现这一点的 gfxdraw 模块不支持弧宽选择.这是代码片段:
As you can see, the blue line is very wavy and has white dots in it. I am achieving this blue line by using pygame.draw.arc() function, but it is not anti-aliased and looks bad. I would like it to be anti-aliased, but gfxdraw module which should let me achieve this, doesn't support arc width selection. Here's code snippet:
pygame.draw.arc(screen, blue, [center[0] - 120, center[1] - 120, 240, 240], pi/2, pi/2+pi*i*koef, 15)
pygame.gfxdraw.aacircle(screen, center[0], center[1], 105, black)
pygame.gfxdraw.aacircle(screen, center[0], center[1], 120, black)
推荐答案
我用多边形创建了圆弧.
I did it creating the arc with a polygon.
def drawArc(surface, x, y, r, th, start, stop, color):
points_outer = []
points_inner = []
n = round(r*abs(stop-start)/20)
if n<2:
n = 2
for i in range(n):
delta = i/(n-1)
phi0 = start + (stop-start)*delta
x0 = round(x+r*math.cos(phi0))
y0 = round(y+r*math.sin(phi0))
points_outer.append([x0,y0])
phi1 = stop + (start-stop)*delta
x1 = round(x+(r-th)*math.cos(phi1))
y1 = round(y+(r-th)*math.sin(phi1))
points_inner.append([x1,y1])
points = points_outer + points_inner
pygame.gfxdraw.aapolygon(surface, points, color)
pygame.gfxdraw.filled_polygon(surface, points, color)
当然可以使用生成器更优雅地创建 for 循环,但我对 python 不是很熟练.
The for loop could certainly be created more elegantly with a generator, but I am not very sophisticated with python.
弧线看起来肯定比 pygame.draw.arc 好看,但是当我将它与我的 mac 上的屏幕渲染进行比较时,还有改进的空间.
The arc definitely looks nicer than pygame.draw.arc, but when I compare it to the screen rendering on my mac, there is room for improvement.
这篇关于抗锯齿 Arc Pygame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!