【问题标题】:drawing a jagged mountain curve using turtle-graphics and recursion使用海龟图形和递归绘制锯齿状山曲线
【发布时间】:2019-04-19 23:56:38
【问题描述】:

我正在尝试为家庭作业创建一个函数,该函数使用海龟和递归绘制锯齿状的山曲线。该函数称为jaggedMountain(x,y,c,t),其中x x,y 是结束坐标,c 是复杂度常数,t 是海龟对象。我正在尝试创建这样的图像:

def jaggedCurve(x,y,c,t):
    t.pendown()
    x1 = t.xcor() + x / 2
    y1 = t.ycor() + y / 2
    y1 = y + (random.uniform(0,c)-0.5) * (t.xcor() - x)
    if (x1,y1) == (x,y):
        return None
    else:
        jaggedCurve(x1,y1,c,t)

这会很快崩溃,因为基本情况永远不会执行,函数被调用了 993 次,并且超出了递归深度。这个问题困扰了我好久,有什么建议吗?

【问题讨论】:

    标签: python python-3.x recursion turtle-graphics


    【解决方案1】:

    最初,我发现您的代码存在两个问题。第一个是:

    if (x1,y1) == (x,y):
    

    海龟在浮点平面上游荡,它们完全相等的几率很小。您可能最好执行以下操作:

    def distance(x1, y1, x2, y2):
        return ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
    
    ...
    
        if distance(x1, y1, x, y) < 1.0:
    

    第二个问题是jaggedCurve() 绘制nothing 也不返回任何可用于绘制的东西。你需要在某个地方实际移动海龟才能绘制一些东西。

    最后,虽然很难确定没有 c 的值,但我的猜测是,即使进行了上述更改,您也不会得到您想要的。祝你好运。

    【讨论】:

      【解决方案2】:

      非常有趣的问题!

      我的解决方案是创建一个递归函数,在给定两个端点的情况下绘制一条山曲线。随机选择一个位于两个端点之间的 x 坐标值,并在给定最大可能斜率的情况下计算可能的 y 坐标范围,并在此范围之间随机选择一个 y 值并递归执行此操作。当到端点足够接近时,只需在它们之间画一条线。代码如下:

      MAX_SLOPE = 45
      MIN_SLOPE = -45
      MIN_HEIGHT = 0
      def dist_squared(P1,P2):
          return (P1[0]-P2[0])**2 + (P1[1]-P2[1])**2
      
      def mountain(P1,P2):
          if dist_squared(P1,P2) < 1:
              turtle.goto(P2)
              return
          x1,y1 = P1
          x2,y2 = P2
          x3 = random.uniform(x1,x2)
          y3_max = min((x3-x1)*math.tan(math.radians(MAX_SLOPE)) + y1, (x2-x3)*math.tan(-math.radians(MIN_SLOPE)) + y2)
          y3_min = max((x3-x1)*math.tan(math.radians(MIN_SLOPE)) + y1, (x2-x3)*math.tan(-math.radians(MAX_SLOPE)) + y2)
          y3_min = max(y3_min, MIN_HEIGHT)
          y3 = random.uniform(y3_min,y3_max)
          P3 = (x3, y3)
          mountain(P1,P3)
          mountain(P3,P2)
          return
      
      turtle.up()
      turtle.goto(-400,0)
      turtle.down()
      mountain((-400,0),(400,0))
      

      【讨论】:

        【解决方案3】:

        我知道这是 3 个月前发布的,但希望这对在决赛前 5 天也被分配了这个可怕问题的人有所帮助!哈!

        我在这个问题上的挣扎是没有意识到你只需要通过一个点。要获得海龟的起点,您只需使用海龟库中包含的 .xcor() 和 .ycor() 即可。

        import turtle
        import random
        
        def mountain (x, y, complexity, turtleName):
            if complexity == 0:
                turtleName.setposition(x, y)
            else: 
                x1 = (turtleName.xcor() + x)/2
                y1 = (turtleName.ycor() + y)/2
                y1 = y1 + (random.uniform(0, complexity) - 0.5) * (turtleName.xcor() - x)
                complexity = complexity - 1
                mountain(x1, y1, complexity, turtleName)
                mountain(x, y, complexity, turtleName)
        
        
        def main ():
            #Gets input for first coordinate pair, splits, and assigns to variables
            coordinate = str(input("Enter the coordinate pair, separated by a comma: "))
            x, y = coordinate.split(',')
            x = int(x)
            y = int(y)
        
            complexity = int(input("Enter the complexity: "))
            while complexity < 0:
                complexity = int(input("Input must be positive. Enter the complexity: "))
        
            Bob = turtle.Turtle()
            mountain(x, y, complexity, Bob)
        
        main ()
        
        

        【讨论】:

          猜你喜欢
          • 2019-04-14
          • 2016-01-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-16
          • 1970-01-01
          相关资源
          最近更新 更多