簡體   English   中英

Pygame 繪制抗鋸齒填充多邊形

[英]Pygame draw antialiased filled polygon

文檔說“對於 aapolygon,使用帶有 'closed' 參數的 aalines。”,但是 pygame.draw.aalines 不允許我指定寬度(0 = 填充),使其無法填充表面。 這看起來很糟糕:在此處輸入圖像描述

這些圓圈看起來好多了:

在此處輸入圖像描述

我怎樣才能做到這一點?

我使用二次貝塞爾曲線生成曲面,其坐標附加到列表中。 然后我像這樣將它繪制到表面上(在上圖中,我做了兩次,一次用於外圈,一次用於內圈):

pygame.draw.polygon(self.image,fclr,self.points)

以及繪制代碼(shape.image和上面代碼中的self.image是同一個surface):

screen.fill((0,0,0))
screen.blit(shape.image,(100,100))
pygame.display.flip()

martineau的評論是關於點:為了用pygame繪制抗鋸齒填充形狀,使用模塊gfxdraw並繪制一個常規填充形狀和一個抗鋸齒輪廓。 來自http://www.pygame.org/docs/ref/gfxdraw.html

“要繪制抗鋸齒和填充形狀,首先使用函數的aa *版本,然后使用填充版本。”

請注意,您需要顯式導入gfxdraw ,即from pygame import gfxdraw

我進行了一些測試,並使用我的 pygame 版本(2.1.2)在多邊形(鋸齒狀邊緣)頂部繪制 aapolygons(線)導致右側和下部出現鋸齒,因為線條位於鋸齒狀多邊形區域內。 我快速測試了一種解決方法 function,它使用超級采樣將鋸齒狀多邊形轉換為平滑多邊形。 對於任何對此感興趣的人是代碼:

def draw_aapolygon(surface, color, points, scale=2):                            
    """                                                                         
    Draw antialiased polygon using supersampling.                               
    """                                                                         
    # Calculate minimum x and y values.                                         
    x_coords = tuple(x for x, _ in points)                                      
    x_min, x_max = min(x_coords), max(x_coords)                                 
    y_coords = tuple(y for _, y in points)                                      
    y_min, y_max = min(y_coords), max(y_coords)                                 
    # Calculate width and height of target area.                                
    w = x_max - x_min + 1                                                       
    h = y_max - y_min + 1                                                       
    # Create scaled surface with properties of target surface.                  
    s = pygame.Surface((w * scale, h * scale), 0, surface)                      
    s_points = [((x - x_min) * scale, (y - y_min) * scale)                      
                for x, y in points]                                             
    pygame.draw.polygon(s, color, s_points)                                     
    # Scale down surface to target size for supersampling effect.               
    s2 = pygame.transform.smoothscale(s, (w, h))                                
    # Paint smooth polygon on target surface.                                   
    surface.blit(s2, (x_min, y_min))

隨意指出錯誤,除了使用相當多的資源,這是顯而易見的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM