首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mondrian艺术程序Python

Mondrian艺术程序Python
EN

Stack Overflow用户
提问于 2016-04-06 03:23:03
回答 5查看 4.7K关注 0票数 3

我正在尝试创建一个蒙德里安艺术程序……我有生成正方形的代码,randomly..but,我在随机填充正方形时遇到了问题?有谁知道怎么解决这个问题吗?这是我的代码:

代码语言:javascript
复制
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()

谢谢!:)

EN

回答 5

Stack Overflow用户

发布于 2016-04-06 03:52:28

这里是您的程序略微清理,并与临时固定的输入,以方便开发。请注意,所有矩形的右下角都是原点。你也应该随机化。

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2016-04-08 08:33:37

作为一个粉丝,我认为Mondrian更多的是用紧张和递归的暗示来划分空间,而不是随机的正方形。有更多的空白而不是颜色。

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

代码语言:javascript
复制
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,我希望这能给你一些关于你自己的项目的想法。

票数 1
EN

Stack Overflow用户

发布于 2016-04-06 03:45:52

我使用的海龟不多,但经过调查,你可能错过了在begin_fill之前放下笔,在那之后设置piet.color

**更新**看起来fillcolor是个不错的选择,因为你不需要挂起或挂起。下面是mondarian函数的更新代码。

代码语言:javascript
复制
 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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36435261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档