How do you draw an antialiased circular line of a certain thickness? How to set width on pygame.gfx.aacircle()?

删除回忆录丶 提交于 2021-01-05 09:12:50

问题


Like pygame.draw.circle(Surface, color, pos, radius, width), I want to get antialiased circle.

But, there is no option about width on pygame.gfxdraw.aacircle().

Anyone knows alternative way?


回答1:


Neither the pygame.draw module nor the pygame.gfxdraw module provide a function for an antialiased circle with an scalable thickness. While pygame.draw.circle() can draw a circle with different width, pygame.gfxdraw.circle() can draw a thin, antialiased circle.


You can try to stitch the circle with a pygame.draw.circle() for the body and pygame.gfxdraw.circle() on the edges. However, the quality is low and can depend on the system:

def drawAACircle(surf, color, center, radius, width):
    pygame.gfxdraw.aacircle(surf, *center, 100, color)  
    pygame.gfxdraw.aacircle(surf, *center, 100-width, color)  
    pygame.draw.circle(surf, color, center, radius, width) 

I recommend drawing a image with an antialiased circle and blit the image. You can create the image using OpenCV (opencv-python). See OpenCV - Drawing Functions.

Minimal example:

import pygame
import cv2
import numpy

def drawAACircle(surf, color, center, radius, width):
    circle_image = numpy.zeros((radius*2+4, radius*2+4, 4), dtype = numpy.uint8)
    circle_image = cv2.circle(circle_image, (radius+2, radius+2), radius-width//2, (*color, 255), width, lineType=cv2.LINE_AA)  
    circle_surface = pygame.image.frombuffer(circle_image.flatten(), (radius*2+4, radius*2+4), 'RGBA')
    surf.blit(circle_surface, circle_surface.get_rect(center = center))

pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    window.fill((32, 32, 32))
    drawAACircle(window, (255, 0, 0), window.get_rect().center, 100, 20)
    pygame.display.flip()


来源:https://stackoverflow.com/questions/64816341/how-do-you-draw-an-antialiased-circular-line-of-a-certain-thickness-how-to-set

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!