<p>作为一个粉丝,我认为蒙德里安更多的是用张力和递归的暗示来划分空间,而不是随机的正方形。白色空间多于颜色。在</p>
<p>如果这些人能<a href="http://www.bbc.com/news/technology-35977315" rel="nofollow noreferrer">teach a computer to paint a Rembrandt</a>,那么我们就可以集体教他们画蒙德里安了。以下是我对这一努力的谦虚提议:</p>
<pre><code>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()
</code></pre>
<p>如果你不喜欢你得到的画,点击画布,它会画另一幅,希望你更喜欢:</p>
<p><a href="https://i.stack.imgur.com/tOzNe.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tOzNe.jpg" alt="enter image description here"/></a></p>
<p>@KaileeCollins,我希望这能给你自己的项目一些想法。在</p>