【问题标题】:Mondrian Art Program Python蒙德里安艺术程序 Python
【发布时间】:2016-04-05 19:23:03
【问题描述】:

我正在尝试创建一个蒙德里安艺术程序..我有随机生成正方形的代码..但是我无法用原色随机填充正方形?有谁知道如何解决这个问题?这是我的代码:

import turtle
import random


turtle.screensize(1000,1000)
turtle.setworldcoordinates(-500,-500,500,500)

piet = turtle.Turtle()
piet.speed(300)

#primary colors, red, blue, yellow
#piet.color(red,blue,yellow)

rectangles = int(input('How many rectangles should be drawn? '))
rectangle_w = int(input('What should be the max width of the rectangles? '))
rectangle_h = int(input('What should be the max height of the rectangles? '))

def mondrian(t,random_w,random_h):
    piet.begin_fill()
    for number_r in range(1):
        for box in range(2):
            t.left(90)
            t.forward(random_w)
            t.left(90)
            t.forward(random_h)
    piet.end_fill()



mondrian(piet,random.randint(10,rectangle_w),random.randint(10,rectangle_h))

def repeat_mondrian():
    for i in range(rectangles - 1):
        mondrian(piet, random.randint(10, rectangle_w), random.randint(10, rectangle_h))

repeat_mondrian()

谢谢! :)

【问题讨论】:

    标签: python python-3.x turtle-graphics


    【解决方案1】:

    这是您的程序稍微清理了一下,为了便于开发,暂时固定了输入。请注意,所有矩形的右下角都是原点。你也应该随机化。

    import turtle
    import random
    
    
    turtle.screensize(1000,1000)
    turtle.setworldcoordinates(-500,-500,500,500)
    
    piet = turtle.Turtle()
    piet.speed(300)
    
    rectangles = 8 #int(input('How many rectangles '))
    rectangle_w = 500 #int(input('Max width of the rectangles? '))
    rectangle_h = 500 #int(input('Max height of the rectangles? '))
    
    def mondrian(t,random_w, random_h):
        piet.fillcolor(random.choice(('red','blue','yellow')))
        piet.begin_fill()
        for box in range(2):
            t.left(90)
            t.forward(random_w)
            t.left(90)
            t.forward(random_h)
        piet.end_fill()
    
    def repeat_mondrian():
        for i in range(rectangles):
            mondrian(piet,
                     random.randint(10, rectangle_w),
                     random.randint(10, rectangle_h))
    
    repeat_mondrian()
    

    【讨论】:

      【解决方案2】:

      作为一名粉丝,我认为蒙德里安更倾向于用张力和递归的暗示来划分空间,而不是随机的正方形。留白比颜色多。

      如果这些人可以teach a computer to paint a Rembrandt,那么我们应该能够集体教一个人画蒙德里安。这是我对这项工作的谦虚:

      import turtle as turtle_graphics
      import random
      import collections
      
      BORDER_COLOR = '#000000'  # so you can add 'black' to COLORS below
      
      BORDER_WIDTH = 10
      
      MINIMUM_DIVISIBLE_PORTION = .2  # limits recursion
      
      COLORS = ('white', 'white', 'red', 'white', 'blue', 'yellow')  # multiple 'white' to increase probability
      
      Bounds = collections.namedtuple('Bounds', ['x', 'y', 'width', 'height'])
      
      PICTURE_BOUNDS = Bounds(x=-250, y=-300, width=500, height=600)
      
      
      def fill_rectangle(turtle, bounds, color=BORDER_COLOR):
          """ Fill a rectangle with the border color (by default) and then fill the center with a bright color """
          turtle.penup()
          turtle.goto(bounds.x, bounds.y)
          turtle.color(color)
          turtle.pendown()
          turtle.begin_fill()
          for _ in range(2):
              turtle.forward(bounds.width)
              turtle.left(90)
              turtle.forward(bounds.height)
              turtle.left(90)
          turtle.end_fill()
          turtle.penup()
      
          if color == BORDER_COLOR:
              fill_rectangle(turtle, Bounds(bounds.x + BORDER_WIDTH, bounds.y + BORDER_WIDTH, bounds.width - BORDER_WIDTH*2, bounds.height - BORDER_WIDTH*2), random.choice(COLORS))
      
      
      def mondrian(piet, bounds):
          """ Divide, fill and divide & fill some more.  Intuitively and recursively """
      
          if bounds.width < bounds.height:
              dimension = 'height'
              random_dimension = random.randint(getattr(bounds, dimension) // 5, 2 * getattr(bounds, dimension) // 3)
              bounds_yin = Bounds(bounds.x, y=bounds.y + random_dimension, width=bounds.width, height=bounds.height - random_dimension)
              bounds_yang = Bounds(bounds.x, bounds.y, bounds.width, random_dimension)
          else:
              dimension = 'width'
              random_dimension = random.randint(getattr(bounds, dimension) // 5, 2 * getattr(bounds, dimension) // 3)
              bounds_yin = Bounds(bounds.x, bounds.y, random_dimension, bounds.height)
              bounds_yang = Bounds(x=bounds.x + random_dimension, y=bounds.y, width=bounds.width - random_dimension, height=bounds.height)
      
          if getattr(bounds_yin, dimension) > getattr(bounds_yang, dimension):
              bounds_paint, bounds_divide = bounds_yang, bounds_yin
          else:
              bounds_paint, bounds_divide = bounds_yin, bounds_yang
      
          fill_rectangle(piet, bounds_paint)
      
          if getattr(bounds_divide, dimension) < MINIMUM_DIVISIBLE_PORTION * getattr(PICTURE_BOUNDS, dimension):
              fill_rectangle(piet, bounds_divide)
          else:
              mondrian(piet, bounds_divide)
      
      
      def paint_canvas(dummy_x=0, dummy_y=0):
          """ Runs the program and can be used as an event handler """
          turtle_graphics.onscreenclick(None)
          fill_rectangle(turtle_graphics, PICTURE_BOUNDS, 'black')
          mondrian(turtle_graphics, PICTURE_BOUNDS)
          turtle_graphics.onscreenclick(paint_canvas)
      
      turtle_graphics.screensize(PICTURE_BOUNDS.width, PICTURE_BOUNDS.height)
      turtle_graphics.speed('fastest')
      turtle_graphics.hideturtle()
      
      paint_canvas()
      
      turtle_graphics.listen()
      
      turtle_graphics.mainloop()
      

      如果你不喜欢你得到的画,点击画布,它会画出另一幅,希望更符合你的喜好:

      @KaileeCollins,我希望这能给你一些关于你自己项目的想法。

      【讨论】:

        【解决方案3】:

        您只需要更改turtle.color(您已将其注释掉,只是在错误的地方:

        def mondrian(t, random_w, random_h):
            piet.begin_fill()
            piet.color(random.choice(['red', 'blue', 'yellow']))
            # rest of your code goes here
        

        【讨论】:

        • color(colorstring) 更改画笔和填充颜色。 fillcolor 没有。阿尼尔没有具体说明笔的颜色,但蒙德里安使用了粗黑线。
        • 正如我所说,我不怎么使用海龟,因此正在弄清楚。之后我更新了代码。现在它可以随机选择颜色,代码竟然和特里一样。只需要换一行。
        【解决方案4】:

        我不怎么用 turtle ,但调查后看起来你可能错过了在begin_fill 之前放下笔并在之后设置piet.color

        ** 更新 ** 看起来fillcolor 是一种方式,因为你不需要做 pendown 或 penup。 这是mondarian 函数的更新代码。

         def mondrian(t,random_w,random_h):    
            piet.begin_fill()    
            for number_r in range(1):
                for box in range(2):
                    piet.fillcolor(random.choice(('red','blue','yellow')))            
                    t.left(90)
                    t.forward(random_w)
                    t.left(90)
                    t.forward(random_h)        
            piet.end_fill()
        

        【讨论】:

          【解决方案5】:

          又一个实现。该程序可以在浏览器中运行,在http://www.transcrypt.org/live/turtle_site/turtle_site.html 处选择“Mondrian”演示并稍等片刻,画面每隔几秒就会变化一次。

          你可以修改程序,看看效果如何。它还可以从控制台使用 Python 3.5 运行。

          # Inspired by our cat Pietje
          
          from turtle import *
          from random import *
          
          speed (0)
          
          colors = ('gray', 'green', 'red', 'white', 'blue', 'yellow')
          delta = 8
          threshold = 100
          color ('black', 'black')
          
          def maybe (bias = None):
              return choice ([False, True, bias, bias] if bias != None else [False, True])    
          
          def between (a, b):
              return a + (0.2 + 0.3 * random ()) * (b - a)
          
          recentColors = ['black', 'black']
          def originalColor ():
              global recentColors
              while True:
                  result = choice (colors)
                  if not result in recentColors:
                      recentColors = [result, recentColors [0]]
                      return result
          
          def rect (xMin, yMin, xMax, yMax):
              for aColor in ('black', originalColor ()):
                  color (aColor, aColor)
          
                  up ()
                  goto (xMin, yMin)
                  down ()
          
                  begin_fill ()
                  goto (xMax, yMin)
                  goto (xMax, yMax)
                  goto (xMin, yMax)
                  goto (xMin, yMin)
                  end_fill ()
          
                  xMin += delta
                  yMin += delta
                  xMax -= delta
                  yMax -= delta
          
          def draw (xMin = -250, yMin = -300, xMax = 250, yMax = 300):
              if xMax - xMin > threshold and yMax - yMin > threshold:
                  if maybe (xMax - xMin > yMax - yMin):
                      xMid = between (xMin, xMax)
                      if maybe ():
                          draw (xMin, yMin, xMid, yMax)
                          rect (xMid, yMin, xMax, yMax)
                      else:
                          rect (xMin, yMin, xMid, yMax)
                          draw (xMid, yMin, xMax, yMax)
                  else:
                      yMid = between (yMin, yMax)
                      if maybe ():
                          draw (xMin, yMin, xMax, yMid)
                          rect (xMin, yMid, xMax, yMax)
                      else:
                          rect (xMin, yMin, xMax, yMid)
                          draw (xMin, yMid, xMax, yMax)
              else:
                  rect (xMin, yMin, xMax, yMax)
                  ontimer (lambda: (clear (), draw ()), 2000)
          draw ()
          done ()     
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多