[英]Python — Using turtle graphics with nested lists. How do I solve this?
I have a nested list like this: 我有一个这样的嵌套列表:
data_set_07 = [['A', 1, 3, 'S'], ['A', 2, 3, 'S'], ['A', 3, 1, 'S'],
['A', 3, 0, 'S'], ['A', 5, 2, 'S'], ['A', 5, 3, 'S'],
['B', 0, 0, 'S'], ['B', 0, 2, 'S'],
['C', 3, 2, 'S'], ['C', 4, 0, 'S'],
['D', 1, 0, 'S']]
'A'
, 'B'
, 'C'
and 'D'
are all references to dimensions (1x1, 1x2, 2x2, etc.). 'A'
, 'B'
, 'C'
和'D'
都是尺寸的引用(1x1、1x2、2x2等)。 The numbers are in reference to a specific column and row on a graph and the 'S'
stands for 'SOUTH' (as in, the icon should face south). 这些数字是指图形上特定的列和行,
'S'
代表“ SOUTH”(例如,图标应朝南)。
I have drawn 4 different icons using the turtle graphics on python and I need each icon to be drawn on the graph in these different settings (some smaller than others, and in different locations but all specific to the numbers above in the lists.) 我使用python上的turtle图形绘制了4个不同的图标,我需要在这些不同的设置下绘制每个图标(有些比其他的小,并且在不同的位置,但都特定于列表中的数字)。
What I need to know is, what do I do to my code to add data_set_07
so it puts my icons on the graph in the correct positions and not just in the default position? 我需要知道的是,如何对代码添加
data_set_07
以便将我的图标放在图形上的正确位置,而不仅仅是默认位置?
One of my icons to be used with data_set_07
: 我的与
data_set_07
一起使用的图标data_set_07
:
def NBC(data_set):
pu()
color('yellow')
begin_fill()
setheading(183)
pd()
fd(150)
circle(-40, 190)
right(19)
fd(165)
end_fill()
pu()
left(5)
fd(5)
left(173)
pd()
color('orange')
begin_fill()
pd()
fd(150)
circle(-40, 190)
right(19)
fd(165)
end_fill()
pu()
left(5)
left(173)
pd()
color('red')
begin_fill()
pd()
fd(150)
circle(-40, 190)
right(19)
fd(165)
end_fill()
pu()
setheading(90)
right(170)
left(168)
pd()
color('purple')
begin_fill()
pd()
fd(150)
circle(-40, 190)
right(19)
fd(165)
end_fill()
pu()
left(179)
pd()
color('blue')
begin_fill()
pd()
fd(150)
circle(-40, 190)
right(19)
fd(165)
end_fill()
pu()
left(180)
pd()
color('green')
begin_fill()
pd()
fd(150)
circle(-40, 190)
right(19)
fd(165)
end_fill()
pu()
NBC(data_set_07)
It looks like your icon will always be drawn the same size. 看起来您的图标将始终被绘制为相同大小。 If you want to be able to vary the size according to the dimensions, as I take it, you'll have to parametrize your icon functions.
如果您希望能够根据尺寸来改变大小(如我所言),则必须对图标功能进行参数设置。 Something like
就像是
def NBC(height, width, x, y)
Then you'll have to add code to move to (x, y) to begin with. 然后,您必须先添加代码以移至(x,y)。 I haven't looked at turtle graphics in many years, so I can't say what the exact command for this is.
我已经好多年没有看过海龟图形了,所以我不能说确切的命令是什么。 After that, you have to scale your movements by height and width.
之后,您必须按高度和宽度缩放运动。 (Height and width could be multiples of the default size, so that that height=1, width=1 means draw it in the default size, and height=1, width=2 means draw it with the default height but make the width twice as large.)
(高度和宽度可以是默认大小的倍数,因此height = 1,width = 1表示使用默认大小绘制,height = 1,width = 2表示使用默认高度绘制,但宽度设为两倍一样大。)
If you really want to change the aspect ratio like this, you may not like the result. 如果您确实想像这样更改长宽比,则可能不喜欢结果。 You'll also have to change the headings.
您还必须更改标题。 It just takes a little trigonometry to figure out what the angle should be.
只需一点三角即可确定角度。 If the original heading is theta, then the new heading would be
如果原始标题为theta,则新标题为
arctan(width*tan(theta)/height)
if I haven't made a mistake. 如果我没有记错的话。 You could also make the logo face in different directions by passing the direction as a parameter.
您还可以通过将方向作为参数传递来使徽标面朝不同的方向。 If the default direction is 'E' then you can just define a dict, say
如果默认方向是“ E”,那么您可以定义一个字典,例如
angle = {'E':0, 'N':90, 'W':180, 'S':270}
pass the desired direction as a parameter phi, and add angle[phi] to all the headings. 将所需方向作为参数phi传递,并向所有航向添加角度。
May I suggest that you not try to get this all working at once. 我可以建议您不要尝试使所有这些工作一次完成。 Start by just moving the logo to different positions.
首先将徽标移动到其他位置。 Then work on scaling where the aspect ratio doesn't change (width = height).
然后在比例不变的情况下进行缩放(宽度=高度)。 Then work on changing the orientation.
然后更改方向。 Finally try changing the aspect ratio if you really want to.
最后,如果确实需要,请尝试更改长宽比。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.