- 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/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!