2013-09-30 89 views
-5

我需要编写的函数,使得在龟平行线和采用下列四个参数:绘制平行线与龟

  • 长度,即每行的长度
  • 代表,即行数来绘制
  • 分离,也就是平行线之间的距离

到目前为止我得到这个:

import turtle as t 

def parallelLines(length, reps, separation): 
    t.fd(length) 

    t.penup() 

    t.goto(0, separation) 

    for i in reps: 
     return i 
+0

好了,你可以想想如何'在现实生活中做到这一点?暂时忽略Python。 – Ryan

+0

你会如何做它作为一只乌龟,因为你的线条是非常直的,你的转身是完全准确的? – Ryan

+0

绘制第一行X的长度,然后从第一行Y的长度开始向下移动并重复,直到我有我需要的许多代表 –

回答

0

您已经回答了自己的问题:

绘制的第一行X长度,再往下从 是第一线Y长度开始移动,并重复,直到我有但是许多代表我 需要

这翻译成代码是这样:

goto start position 
for _ in reps: 
    pen down 
    move length to the right 
    pen up 
    move length to the left 
    move separation to the bottom 

现在你只需要填写正确的调用你的乌龟API。

1

我建议改为:

def parallel(): 
    turtle.forward(length) 
    turtle.rt(90) 
    turtle.pu() 
    turtle.forward(distanceyouwantbetweenthem) 
    turtle.rt(90) 
    turtle.forward(length) 
0

至今都是不完整的,不正确的和/或破给出的答案。我在下面有一个使用声明的API并绘制平行线。

的OP没有明确在线条应该出现相对于乌龟的位置,所以我与龟是在两个维度中心点去:

import turtle 

STAMP_SIZE = 20 

def parallelLines(my_turtle, length, reps, separation): 
    separation += 1 # consider how separation 1 & 0 differ 

    my_stamp = my_turtle.clone() 
    my_stamp.shape('square') 
    my_stamp.shapesize(1/STAMP_SIZE, length/STAMP_SIZE, 0) 
    my_stamp.tilt(-90) 
    my_stamp.penup() 
    my_stamp.left(90) 
    my_stamp.backward((reps - 1) * separation/2) 

    for _ in range(reps): 
     my_stamp.stamp() 
     my_stamp.forward(separation) 

    my_stamp.hideturtle() 

turtle.pencolor('navy') 

parallelLines(turtle.getturtle(), 250, 15, 25) 

turtle.hideturtle() 
turtle.exitonclick()