- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的任务是使用 Python 绘制拼凑图案。我需要从用户那里得到宽度和高度,两者都必须大于 3 但小于 10,并且有 4 种颜色,不能相同。我已经为罚款编写了代码。但我需要绘制两种不同类型的图案。一个图案沿着边缘,只有 1 个补丁深,第二个图案填充中心方 block 。现在,我用来绘制它的代码是沿着顶部、底部和每一侧重复第一个补丁,然后在整个中间重复第二个图案。但现在我必须为每个不同的单独补丁分配一种颜色,循环使用给定的颜色,以便它从第一个开始,循环遍历所有颜色,然后再次从第一个开始。
我的问题是,我不知道如何循环显示颜色,因为顺序会逐行不同。有没有一种有效的方法可以用我到目前为止所写的来做到这一点,即。绘制一个单独的补丁并在线条上重复它,如果是这样,最好的方法是什么?
from graphics import *
def main():
width, height = getDimensions()
colour1, colour2, colour3, colour4 = getColours()
win = drawGraphWin(width, height)
drawPattern(win, width, height, colour1, colour2, colour3, colour4)
def getDimensions():
width = input("How many patches, between 4 and 9, would you like \
horizontally? :")
while True:
try:
width = int(width)
break
except ValueError:
width = input("How many patches, between 4 and 9, would you like \
horizontally? :")
while width < 4 or width > 9:
width = eval(input("How many patches, between 4 and 9, would you like \
horizontally? :"))
height = input("How many patches, between 4 and 9, would you like \
vertically? :")
while True:
try:
height = int(height)
break
except ValueError:
height = input("How many patches, between 4 and 9, would you like \
vertically? :")
while height < 4 or height > 9:
height = eval(input("How many patches, between 4 and 9, would you like \
vertically? :"))
return width, height
def getColours():
colour1 = input("Please enter a colour \
(red, green, blue, yellow, magenta, orange, cyan): ")
while valid1(colour1) == False:
colour1 = input("Please enter a colour \
(red, green, blue, yellow, magenta, orange, cyan): ")
colour2 = input("Please enter a second colour \
(red, green, blue, yellow, magenta, orange, cyan): ")
while valid2(colour1, colour2) == False:
colour2 = input("Please enter a second colour \
(red, green, blue, yellow, magenta, orange, cyan): ")
colour3 = input("Please enter third a colour \
(red, green, blue, yellow, magenta, orange, cyan): ")
while valid3(colour1, colour2, colour3) == False:
colour3 = input("Please enter third a colour \
(red, green, blue, yellow, magenta, orange, cyan): ")
colour4 = input("Please enter a fourth colour \
(red, green, blue, yellow, magenta, orange, cyan): ")
while valid4(colour1, colour2, colour3, colour4) == False:
colour4 = input("Please enter a fourth colour \
(red, green, blue, yellow, magenta, orange, cyan): ")
return colour1, colour2, colour3, colour4
def valid1(colour1):
if any( [colour1 == "red", colour1 == "green", colour1 == "blue", \
colour1 == "yellow", colour1 == "magenta", colour1 == "orange", \
colour1 == "cyan"]):
return True
else:
return False
def valid2(colour1, colour2):
if any( [colour2 == "red", colour2 == "green", colour2 == "blue", \
colour2 == "yellow", colour2 == "magenta", colour2 == "orange", \
colour2 == "cyan"]):
if colour2 == colour1:
return False
else:
return True
else:
return False
def valid3(colour1, colour2, colour3):
if any( [colour3 == "red", colour3 == "green", colour3 == "blue", \
colour3 == "yellow", colour3 == "magenta", colour3 == "orange", \
colour3 == "cyan"]):
if any( [colour3 == colour2, colour3 == colour1]):
return False
else:
return True
else:
return False
def valid4(colour1, colour2, colour3, colour4):
if any( [colour4 == "red", colour4 == "green", colour4 == "blue", \
colour4 == "yellow", colour4 == "magenta", colour4 == "orange", \
colour4 == "cyan"]):
if any( [colour4 == colour3, colour4 == colour2, colour4 == colour1]):
return False
else:
return True
else:
return False
def drawGraphWin(width, height):
win = GraphWin("CW PatchWork Deisgn", width*100, height*100)
win.setCoords(0.0,0.0,4*width,3*height)
for i in range(width):
vLineN = Line(Point(i*4, 0), Point(i*4, height*3))
vLineN.draw(win)
for j in range(height):
hLineN = Line(Point(0, j*3), Point(width*4, j*3))
hLineN.draw(win)
return win
def drawPattern(win, width, height, colour1, colour2, colour3, colour4):
for widthNo in range(width):
drawPatch1(win, widthNo, 0, colour1, colour2, colour3, colour4)
for widthNo in range(width):
drawPatch1(win, widthNo, height-1, colour1, colour2, colour3, colour4)
for heightNo in range(height-2):
drawPatch1(win, 0, heightNo+1, colour1, colour2, colour3, colour4)
for heightNo in range(height-2):
drawPatch1(win, width-1, heightNo+1, colour1, colour2, colour3, colour4)
for innerNoH in range(width-2):
innerWidth = innerNoH +1
for innerNoV in range(height-2):
innerHeight = height - 2 - innerNoV
drawPatch2(win, innerWidth, innerHeight, colour1, colour2, colour3,\
colour4)
win.getMouse()
win.close()
def drawPatch1(win, widthNo, height, colour1, colour2, colour3, colour4):
for i in range(6):
vLineN = Line(Point((0.8*(i))+widthNo*4, 0+height*3), \
Point((0.8*(i))+widthNo*4, 3+height*3))
vLineN.draw(win)
hLineN = Line(Point(0+widthNo*4, (0.6*(i))+height*3), \
Point(4+widthNo*4, (0.6*(i))+height*3))
hLineN.draw(win)
if i == 0:
for j in range(5):
hiMessage = drawHiMessage(0.4+widthNo*4, (0.3+(j*0.6))+height*3,\
win, colour1, colour2, colour3, colour4)
hiMessage.setFill(colour1)
vLineN.setFill(colour1)
hLineN.setFill(colour1)
elif i == 1:
for k in range(5):
hiMessage = drawHiMessage(1.2+widthNo*4, (0.3+(k*0.6))+height*3,\
win, colour1, colour2, colour3, colour4)
hiMessage.setFill(colour2)
vLineN.setFill(colour2)
hLineN.setFill(colour2)
elif i == 2:
for l in range(5):
hiMessage = drawHiMessage(2+widthNo*4, (0.3+(l*0.6))+height*3, \
win, colour1, colour2, colour3, colour4)
hiMessage.setFill(colour3)
vLineN.setFill(colour3)
hLineN.setFill(colour3)
elif i == 3:
for m in range(5):
hiMessage = drawHiMessage(2.8+widthNo*4, (0.3+(m*0.6))+height*3,\
win, colour1, colour2, colour3, colour4)
hiMessage.setFill(colour4)
vLineN.setFill(colour4)
hLineN.setFill(colour4)
elif i == 4:
for n in range(5):
hiMessage = drawHiMessage(3.6+widthNo*4, (0.3+(n*0.6))+height*3,\
win, colour1, colour2, colour3, colour4)
hiMessage.setFill(colour1)
vLineN.setFill(colour1)
hLineN.setFill(colour1)
def drawHiMessage(x, y, win, colour1, colour2, colour3, colour4):
hiMessage = Text(Point(x, y), "hi!")
hiMessage.draw(win)
hiMessage.setSize(7)
return hiMessage
def drawPatch2(win, innerNoH, height, colour1, colour2, colour3, colour4):
for i in range(4):
x = (0.5+i)+innerNoH*4
for j in range(3):
y = (2.2-j)+height*3
sail = drawBoat(x, y, win, colour1, colour2, colour3, colour4)
if i == 0:
sail.setFill(colour1)
elif i == 1:
sail.setFill(colour2)
elif i ==2:
sail.setFill(colour3)
elif i == 3:
sail.setFill(colour4)
def drawBoat(x, y, win, colour1, colour2, colour3, colour4):
sail = Polygon(Point(x-0.5,y), Point(x,y+0.8), Point(x+0.5,y))
hull = Polygon(Point(x-0.5,y-0.1), Point(x+0.5,y-0.1), Point(x+0.3,y-0.2), \
Point(x-0.3,y-0.2))
mast = Line(Point(x,y), Point(x,y-0.1))
sail.draw(win)
hull.draw(win)
mast.draw(win)
hull.setFill("white")
return sail
main()
最佳答案
您可以使用 itertools.cycle
反复检查所有颜色。因此,假设您将所有补丁都放在一个名为 patches
的列表中,您可以执行如下操作
import itertools
colors = ['red','blue','green','yellow']
patches = ['a','b','c','d','e','f','g','h','i','j']
for patch, color in itertools.izip(patches, itertools.cycle(colors)):
# color a patch
print 'Colour', patch, 'as', color
打印出来
Colour a as red
Colour b as blue
Colour c as green
Colour d as yellow
Colour e as red
Colour f as blue
Colour g as green
Colour h as yellow
Colour i as red
Colour j as blue
关于python - 如何循环不同的颜色并将它们分配给已经绘制的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13442707/
一段时间以来,我一直在做这个反复出现的噩梦(阅读 - 我的应用程序中的错误)。出于某种原因,某个计时器在我停止后继续发送“Elapsed”事件,即使 在事件本身 计时器“承认”已被禁用!检查一下: /
为了找到 2 个 git 分支的共同祖先,需要做的是: git merge-base branch another_branch 好的。但是……如果两个分支都已经 merge 了怎么办?当我在这种情况
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 1年前关闭。 Improve this
我想要一个相机 View ,可以将图像捕获到本地文件或让用户从本地照片库中选择图像。我想也许有人为此编写了很好的库/代码。也许我可以利用它。已经有好的了吗?谢谢。我只是避免重新发明轮子:) 最佳答案
我从 master 分支创建了一个功能分支。之后有来自功能分支的提交 [F1]。 [F1] -- Feature Branch / [M1]-[M2
我喜欢使用 .NET 进行编程,尤其是 C# 3.0、.NET 3.5 和 WPF。但我特别喜欢的是 Mono .NET 确实与平台无关。 现在我听说了 Mono 中的 Olive 项目。我找不到某种
介绍和搜索 所以我认为我犯了一个严重的错误,我很担心。我已经分析了独立负责人的论坛,我已经接近找到答案,但场景太具体,不适用于我所在的位置。如果您找到可以回答我的问题的特定主题,请链接我。 例如:Ho
我有一个类似于下图的提交图。标记为 * 的提交表示大量提交。 A* | B--------- | | C* D* master 和 cor
我喜欢使用 .NET 进行编程,尤其是 C# 3.0、.NET 3.5 和 WPF。但我特别喜欢的是 Mono .NET 确实与平台无关。 现在我听说了 Mono 中的 Olive 项目。我找不到某种
我们最近接手了一个 .NET 项目,在查看 db 后,我们在某些列中有以下内容: 1)某些列具有诸如" & etc etc 2) 有些有 标签和其他非 html 编码的标签 这些数据
你好,当我导航到应用程序中的另一个页面时出现此错误 我不知道为什么这个错误出现 #0 _AsyncCompleter.complete (dart:async/future_impl.da
我使用以下 C 算法计算数据的 CRC32: #define CRC32_POLYNOM_REVERSED 0xEDB88320 uint32 calcCrc32(uint8* buffer, u
我试图在我的一个测试中断言模型中的字段没有改变。我知道从哲学上这是不正确的,但由于我控制了我需要知道的所有变量,所以我只想检查我的数据库条目是否没有改变。 我愿意接受一个解决方案,该解决方案可以将其转
我是 GitHub 的新手。并通过 Eclipse 使用它我们是两个人在开发一个应用程序。当我在 Git shell 中检查 git status 时,我得到以下状态。 On branch maste
简单代码: std::ifstream file("file.txt"); std::string line; while(getline(file,line)) ; //exhaust file
是的,我又找不到这个 Gradle DSL 方法:'compile()' 问题。 我检查了我有: buildscript { repositories { jcenter()
HTML: articles CSS: #main_menu { float: left; padding-top: 10px; vertical-align: m
我是一名优秀的程序员,十分优秀!