Pygame中的递归曲面

问题描述

假设您的应用程序有一个很大的负空间漏洞,那么您想用某种东西来填充它……那么为什么不用应用程序本身的副本来填充空白呢?

我有一个应用。它将具有嵌套的应用程序。一些应用程序是正常的(即矩形),但是(大多数)其他应用程序是椭圆形甚至三角形的。有连接器层可嵌套应用几何的各种组合。

尽管仍在进行中,但有一种想法可以管理这种嵌套导致的正空间与负空间的比率。我正在研究的一个想法是将这些连接器子类化,以在会有正空间的地方创建更多区域,并动态选择一个子类。

这将能够给我带来很多额外的积极空间,这些空间需要充满一些东西。

基于: Pygame. How do I resize a surface and keep all objects within proportionate to the new window size?

解决方法

从高层次来看,我们需要:

  1. 拿一份要分形的副本
  2. 计算分形图像可以重复的位置 (边界矩形,这里称为“递归点”)
  3. 创建用于绘制的临时曲面
  4. 将所述副本缩放为临时表面的大小
  5. 将缩放后的副本绘制到临时表面上

换句话说:

def draw_background (self,temp):
    App.draw_background (self,temp) # default background for apps is to show a default image
    self.child.draw_scene (temp)                   # draw whatever is going to be fractalized   
def draw_foreground (self,temp):                    # fractalization function
    TR = temp.get_rect ()                            # bounding rect for parent
    X,Y,W,H = TR
    ts = pygame.Surface ((W,H))                     # get a fresh surface for working
        
    pic = temp.copy ()                               # could be anything
        
    for rp in self.recursion_points (temp):
        x,y,w,h = rp
        w,h = tr ((w,h))                           # round coordinates
        trans = pygame.transform.scale (pic,(w,h)) # scale fake screen to bounding rect
        ts.blit (trans,(x,y))                      # blit fake screen onto working surface
                
    temp.blit (ts,(X,Y))                           # blit working-surface onto real surface

客户代码:

d = None                # default behavior for no app is to not display a foreground
# these adapters handle differing window geometries
c = CircledSquare (d,rotation=STRAIGHT)               # middle circle,inner square
b = SquaredCircle (c,background=SECONDARY_BACKGROUND) # outer square,middle circle
a = RecursiveComposite (b) # without this layer,the inner square is not visible... boring!

几何函数:

def recursive_affine (rect,dx,dy,rw,rh,n):
    x,h = rect
    for k in range (1,n + 1):
        dx,dy = dx * rw,dy * rh
        x,y  =  x + dx,y + dy
        w,h  =  w * rw,h * rh
        yield x,h
def recurse_point (rect,rp,minsz):
    X,H = rect
    x,h = rp
    # get scale and offset for recursion point
    dx,dy = x - X,y - Y
    rw,rh = w / W,h / H
    # get number of recursions until < minsz
    f = lambda a,b,c: (log (a) - log (b)) / log (c)
    xmin,ymin = minsz
    xn,yn = f (xmin,rw),f (ymin,h,rh)
    n = min (xn,yn)
    n = ceil (n)
    # recursively apply scale and offset
    tail = recursive_affine (rp,n)
    return rp,*tail