因此,我使用两种不同的设计创建了重复模式。现在的问题是制作颜色,因此我必须要求用户选择3种不同的颜色,并使用它们来制作该颜色设计的某些区域。例如,我的意思是,如果我选择5x5补丁布局,则前后两下的前两盒图案应该是一种颜色(所以就是四盒),依此类推。
编辑:清除混乱。
因此,如果您查看500x500像素之一。询问您想要的补丁大小时,键入“ 5”。
您应该看到两种不同的图案设计。
我想要的只是x = 0至x = 200和y = 0至y = 200为蓝色。这意味着该区域中的所有内容(按区域,我是指其中的所有图案设计)都应为蓝色。对于x = 300至x = 500和y = 300至y = 500,应使用相同的颜色。
x = 300至x = 500和y = 0至y = 200应该是红色,x = 0至x = 200和y = 300至y = 500也应该是红色。
,剩下的只是中间的“ +”部分,应该有第三种颜色绿色。
https://prntscr.com/m48xxd这就是我所说的颜色。因此左上角和右下角是相同的颜色。右上和左下是相同的颜色,“ +”是另一种颜色。
7。https://prntscr.com/m48xzm是7x7版本
现在,我只添加颜色以使其更易于查看。
我已经尝试过执行'getX'和'getY',但我似乎无法正确完成。
from graphics import *
def main():
global patchWorkSize
patchWorkSize = int(input("What patchwork size would you like?"))
if patchWorkSize == 5:
patchWork5()
elif patchWorkSize == 7:
patchWork7()
elif patchWorkSize == 9:
patchWork9()
else:
print('Only patchwork sizes 5, 7 and 9 are avaliable')
def patchWork5():
layout()
for y in range(0,500,100):
for x in range(0,500,200):
for l1 in range(0,100,20):
line = Line(Point(l1+x,0+y), Point(100+x,100-l1+y))
line.draw(win)
for l2 in range(100,0,-20):
line3 = Line(Point(l2+x,100+y), Point(0+x,100-l2+y))
line3.draw(win)
for l3 in range(100,0,-20):
line2 = Line(Point(100+x,0+l3+y), Point(0+l3+x,100+y))
line2.setFill('red')
line2.draw(win)
for l4 in range(0,100,20):
line4 = Line(Point(0+x,100-l4+y), Point(100-l4+x,0+y))
line4.setFill('red')
line4.draw(win)
for e in range(0,500,100):
for t in range(100,500,200):
for o in range(0,500,10):
for c in range(5,100,10):
circle1 = Circle(Point(c+t,5+o+e), 5)
circle1.setFill('blue')
circle1.draw(win)
def patchWork7():
layout()
for y in range(0,700,100):
for x in range(0,700,200):
for l1 in range(0,100,20):
line = Line(Point(l1+x,0+y), Point(100+x,100-l1+y))
line.draw(win)
for l2 in range(100,0,-20):
line3 = Line(Point(l2+x,100+y), Point(0+x,100-l2+y))
line3.draw(win)
for l3 in range(100,0,-20):
line2 = Line(Point(100+x,0+l3+y), Point(0+l3+x,100+y))
line2.setFill('red')
line2.draw(win)
for l4 in range(0,100,20):
line4 = Line(Point(0+x,100-l4+y), Point(100-l4+x,0+y))
line4.setFill('red')
line4.draw(win)
for e in range(0,700,100):
for t in range(100,700,200):
for o in range(0,700,10):
for c in range(5,100,10):
circle1 = Circle(Point(c+t,5+o+e), 5)
circle1.setFill('blue')
circle1.draw(win)
def patchWork9():
layout()
for y in range(0,900,100):
for x in range(0,900,200):
for l1 in range(0,100,20):
line = Line(Point(l1+x,0+y), Point(100+x,100-l1+y))
line.draw(win)
for l2 in range(100,0,-20):
line3 = Line(Point(l2+x,100+y), Point(0+x,100-l2+y))
line3.draw(win)
for l3 in range(100,0,-20):
line2 = Line(Point(100+x,0+l3+y), Point(0+l3+x,100+y))
line2.setFill('red')
line2.draw(win)
for l4 in range(0,100,20):
line4 = Line(Point(0+x,100-l4+y), Point(100-l4+x,0+y))
line4.setFill('red')
line4.draw(win)
for e in range(0,900,100):
for t in range(100,900,200):
for o in range(0,900,10):
for c in range(5,100,10):
circle1 = Circle(Point(c+t,5+o+e), 5)
circle1.setFill('blue')
circle1.draw(win)
def layout():
global win
win = GraphWin('Patchwork',patchWorkSize*100, patchWorkSize*100)
for i in range(0, patchWorkSize*100,100):
line = Line(Point(i,0), Point(i, patchWorkSize*100))
line2 = Line(Point(0,i), Point(patchWorkSize*100, i))
line.draw(win)
line2.draw(win)
main()
答案 0 :(得分:1)
关键是将所有内容分解为合适大小的多维数据集,然后将填充代码转换为子例程,可以调用该子例程来填充这些多维数据集。以下是根据这些代码对代码进行的重做,这些代码可以处理输入为3或更大的任何奇数:
from graphics import *
UNIT = 100
def patchWork(win, size):
blocks = size // 2
def hatch_box(x, y):
for n in range(0, UNIT, UNIT//5):
line = Line(Point(n + x * UNIT, y * UNIT), Point((x + 1) * UNIT, UNIT - n + y * UNIT))
line.draw(win)
for n in range(UNIT, 0, -UNIT//5):
line = Line(Point(n + x * UNIT, (y + 1) * UNIT), Point(x * UNIT, UNIT - n + y * UNIT))
line.draw(win)
for n in range(UNIT, 0, -UNIT//5):
line = Line(Point((x + 1) * UNIT, n + y * UNIT), Point(n + x * UNIT, (y + 1) * UNIT))
line.setFill('red')
line.draw(win)
for n in range(0, UNIT, UNIT//5):
line = Line(Point(x * UNIT, UNIT - n + y * UNIT), Point(UNIT - n + x * UNIT, y * UNIT))
line.setFill('red')
line.draw(win)
for y in range(blocks):
for x in range(blocks):
hatch_box(x, y)
for y in range(blocks + 1, 2 * blocks + 1):
for x in range(blocks + 1, 2 * blocks + 1):
hatch_box(x, y)
def draw_circles(x, y):
for o in range(0, UNIT, UNIT // 10):
for c in range(0, UNIT, UNIT // 10):
circle = Circle(Point(c + UNIT // 20 + x * UNIT, o + UNIT // 20 + y * UNIT), UNIT // 20)
circle.setFill('blue')
circle.draw(win)
for y in range(blocks):
for x in range(blocks + 1, 2 * blocks + 1):
draw_circles(x, y)
draw_circles(y, x)
def draw_cube(x, y):
cube = Rectangle(Point(x * UNIT, y * UNIT), Point((x + 1) * UNIT, (y + 1) * UNIT))
cube.setFill('yellow')
cube.draw(win)
x = blocks
for y in range(0, 2 * blocks + 1):
draw_cube(x, y)
draw_cube(y, x)
def layout(size):
win = GraphWin('Patchwork', size * UNIT, size * UNIT)
for i in range(0, size * UNIT, UNIT):
Line(Point(i, 0), Point(i, size * UNIT)).draw(win)
Line(Point(0, i), Point(size * UNIT, i)).draw(win)
return win
def main():
patchWorkSize = int(input("What patchwork size would you like? "))
if patchWorkSize % 2 == 1 and patchWorkSize > 2:
win = layout(patchWorkSize)
patchWork(win, patchWorkSize)
win.getMouse() # pause for click in window
win.close()
else:
print('Only odd sizes 3 or greater are available')
main()
您应该能够在合理的范围内更改UNIT
。
答案 1 :(得分:1)
我不会为您编写所有内容,但是下面的代码显示了如何根据每个区域在拼凑网格中的位置(i
和j
)来选择颜色。颜色由color_func()
确定。
希望这足以让您弄清楚如何应用所示的相对简单的编码模式来绘制所需的图形。
提示:根据(或相对于)这些相同的位置值来计算每个图形元素的坐标(即,不要为每种可能的拼凑大小编写单独的函数)。
此代码对我来说似乎仍然很重复,并且可能更简洁,但我也将其留给您... ;¬)
def main():
# size = int(input("What patchwork size would you like?"))
# Hardcoded for testing.
#
# if size % 2 == 0:
# raise RuntimeError('patchwork size must be odd')
patch_work(5)
print()
patch_work(7)
def patch_work(n):
colors = '0', '1', '2'
mid = n // 2
# Call color_func() for every possible position in patchwork and
# prints the results in rows.
for i in range(n):
row = []
for j in range(n):
row.append(color_func(n, i, j, colors))
print(''.join(row))
def color_func(n, i, j, colors):
mid = n // 2
if i == mid:
index = 2
elif i < mid:
if j < mid:
index = 0
elif j == mid:
index = 2
elif j > mid:
index = 1
elif i > mid:
if j < mid:
index = 1
elif j == mid:
index = 2
elif j > mid:
index = 0
return colors[index]
if __name__ == '__main__':
main()
输出:
00211
00211
22222
11200
11200
0002111
0002111
0002111
2222222
1112000
1112000
1112000