【问题标题】:Diamond made out of pararel lines in Python TurtlePython Turtle 中由平行线组成的钻石
【发布时间】:2018-04-11 03:09:16
【问题描述】:

我必须制作一颗钻石,它的线条数量必须与用户想要的完全相同。我被卡住了,我想了很长时间,但没有得到任何结果。我的钻石需要和图片一样:

【问题讨论】:

    标签: python python-2.7 turtle-graphics


    【解决方案1】:

    这应该也可以:

    nl = 5
    
    for line in range(nl):
        if line < nl/2:      
            spc = (int(nl/2-line))*' '
            dsh = (line*2+1)*'_'
            print(spc, dsh)
        else:
            spc = (int(line-nl/2+1))*' '
            dsh = ((nl-line)*2-1)*'_'
            print(spc, dsh)
    

    它会产生:

       _
      ___
     _____
      ___
       _
    

    【讨论】:

      【解决方案2】:

      虽然您已经接受了自己的解决方案,但看着它得出结论还是很痛苦。你的思维偏向于总是从左到右工作,并将问题分成两个独立的部分。让我们在双向思考的同时绘制一个解决问题的单一形状:

      import turtle
      
      lines = int(raw_input('How many lines do you wish your diamond to have? (Odd number only): '))
      
      half = lines // 2
      canvas = max(200, lines + 1)
      shrinkage = canvas / float(lines)
      
      clyde = turtle.Turtle()
      clyde.speed('fastest')  # because I have no patience
      
      clyde.forward(canvas)
      clyde.penup()
      clyde.left(90)
      clyde.forward(shrinkage)
      clyde.left(90)
      
      for i in range(1, half + 1):
      
          clyde.forward(shrinkage)
      
          for j in range(2):
              clyde.pendown()
              clyde.forward(canvas - i * shrinkage * 2)  # visible horizontal lines
              clyde.penup()
              clyde.left(90)
              clyde.forward(((i * 2) + j) * shrinkage)  # invisible vertical lines
              clyde.left(90)
      
      clyde.hideturtle()
      
      turtle.mainloop()
      

      在此示例中,我有意省略了您的输入错误检查以及将端点强制转换为单个像素。您可以稍后再添加:

      看看你的解决方案是如何绘制的,然后看看这个。请注意,浪费的动作更少。它从左到右和从右到左绘制。它将图案绘制为一系列越来越窄和越来越高的矩形:

      但让关键行不可见。如果你用鼠标跟随它,你会发现它实际上是一个螺旋。

      【讨论】:

      • 感谢您的贡献。我对编程比较陌生,虽然机械,但这是我从逐行编写代码然后定义函数和添加循环中找到的唯一方法。另外,我选择了自己的解决方案,因为我也是 StackOverflow 的新手,没想到其他人会给出答案。您的解决方案看起来不错,我一尝试就会选择正确的。谢谢。
      【解决方案3】:

      如果打印为文本,请尝试使用 for 循环、'_' 字符和空格

      编辑: 这是一些伪代码,

      For i in range(totalLines/2):
         For j in range(totalLines/2 - i):
             Print(" ")
         For j in range(i):
             Print("_")
      
      For i in range(totalLines/2):
         For j in range(totalLines/2 - i):
             Print("_")
         For j in range(i):
             Print(" ")
      

      【讨论】:

        【解决方案4】:

        标题说需要在 Python Turtle 中完成。但是哦,好吧。我想出了一个符合我要求的代码,唯一的问题是上半部和下半部之间存在间隙。
        这是我的代码:

        import turtle
        
        clyde = turtle.Turtle()
        P.S.: I speak spanish, but you should understand my code easily.
        
        while True:
            try:
                lineas = int(raw_input('Cuantas lineas quieres que tenga tu rombo?(Numero Impar): '))
                assert lineas > 2 and lineas < 646 and lineas % 2 != 0
                break
            except AssertionError:
                if lineas > 646 or lineas < 2:
                    print "El numero de lineas debe ser entre de 3 a 645."
                    print
                else:
                    print 'El numero deber ser un impar positivo.'
                    print
            except:
                print 'Ingresa un numero valido'
                print
        
        if lineas <= 21:
            clyde.speed(8)
        elif lineas <= 41:
            clyde.speed(5)
        else:
            clyde.speed(0)
        
        canvas = 200
        
        if lineas > canvas:
            canvas = lineas + 1
        
        def trasPunta(punta):
            clyde.pu()
            clyde.goto(0, punta)
            clyde.pd()
            clyde.fd(1)
            clyde.pu()
            clyde.back(1)
        def drawLine(inc):
            clyde.pd()
            clyde.fd(inc * (canvas / float(lineas)))
            clyde.pu()
            clyde.bk(inc * (canvas / float(lineas)))
        
        trasPunta(canvas / 2.0)
        a = 2
        for i in range(lineas / 2):
        
            clyde.right(90)
            clyde.fd(canvas / float(lineas))
            clyde.right(90)
            clyde.fd(canvas / float(lineas))
            clyde.right(180)
        
            drawLine(a)
            a += 2
        
        trasPunta((-canvas / 2.0) + (canvas / float(lineas)))
        b = 2
        for i in range((lineas / 2) - 1):
        
            clyde.left(90)
            clyde.fd(canvas / float(lineas))
            clyde.left(90)
            clyde.fd(canvas / float(lineas))
            clyde.left(180)
        
            drawLine(b)
            b += 2
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-07-12
          • 2012-07-23
          • 2016-11-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多