Python 海龟图形:如何创建标尺

Python 海龟图形:如何创建标尺,python,recursion,graphics,turtle-graphics,Python,Recursion,Graphics,Turtle Graphics,最高的刻度线是一个半英寸的刻度线,接下来的两个最高的刻度线是四分之一英寸的刻度线,甚至更短的刻度线用于标记八分之一和十六分之一,等等。编写以下递归函数:drawRulerx,y,width,height $import turtle ''' def height(range(4)): for i in ''' Runs = 10 def drawRuler(x,y,width,height): if Runs == 0: print("End.") else:

最高的刻度线是一个半英寸的刻度线,接下来的两个最高的刻度线是四分之一英寸的刻度线,甚至更短的刻度线用于标记八分之一和十六分之一,等等。编写以下递归函数:drawRulerx,y,width,height

$import turtle

'''
def height(range(4)):
  for i in

'''

Runs = 10

def drawRuler(x,y,width,height):

  if Runs == 0:

    print("End.")

  else:

    #turtle.goto(x,y)

    turtle.forward(width)

    turtle.left(90)

    turtle.forward(height)

    turtle.right(180)

    turtle.forward(height)

    turtle.left(90)

    drawRuler(0,0,width,height/4)

    drawRuler(-50,0,15,100)

    drawRuler(0,0,15,50)

    Runs == Runs - 1

    drawRuler(0,0,15,100)

$

我的问题是如何让它创建不同的高度,然后在标尺下创建相同的高度。我觉得我的函数很接近,但仍然很遥远。

递归背后的思想是使用函数将问题分解成越来越小的块,直到这些块无法再被分割

在上面的标尺示例中,您可以这样想

drawRuler(0, 0, 10, 1) // draw a ruler at 0,0 with a length of 10 and a width of 1
  drawRuler(0, 0, 5, 1) // draw the first half of the 10" ruler starting at 0
    drawRuler(2.5, 0, 2.5, 1) // draw the first half of the 5" ruler starting at 0
      // keep dividing the ruler until you reach the 1/16 size
      // once you reach the one sixteenth size, draw the correct size tick mark for passed in length
    drawRuler(2.5, 0, 2.5, 1) // draw the second half of the 5" ruler starting at 2.5 
      // same as above
  drawRuler(5, 0, 5, 1) // draw the second half of the 10" ruler starting at 10
    // same as above
drawRuler(x, y, length, width)
{
   if (length > 1/16) then
      drawRuler(x, y, length / 2, width) // draw the left side
      drawRuler(x + (width * 1/2), y, length / 2, width) // draw the right side
   end if;

   // draw the tick mark here using Turtle Graphics.  The length of the tick mark 
   //   is based on the length parameter pass into the function.
   // For example,
   //   if the length is 10 or any even inch mark the length would be very long
   //   if the length is 5.5 or any 1/2 inch value the length would be long
   //   if the length is 2.25 or any 1/4 inch value the length would be medium
   //   if the length is 0.125 or any 1/8 inch value the length would be medium-short
   //   if the length is 0.0625 or any 1/16 inch value then the length would be short
}
最小的块将是尺子中最小的分区。在你的情况下,是1/16。 然后需要递归调用函数,直到它将标尺划分为1/16的块。一旦调用drawRuler中的长度参数为1/16英寸,问题就很简单了。你只需要画出正确的长度记号。您可以根据函数调用中的length参数来决定如何绘制记号。如果长度为1/16,则绘制1/16记号标记。如果长度为1/2英寸,则画一个1/2英寸的刻度线,以此类推

调用堆栈看起来像这样

drawRuler(0, 0, 10, 1) // draw a ruler at 0,0 with a length of 10 and a width of 1
  drawRuler(0, 0, 5, 1) // draw the first half of the 10" ruler starting at 0
    drawRuler(2.5, 0, 2.5, 1) // draw the first half of the 5" ruler starting at 0
      // keep dividing the ruler until you reach the 1/16 size
      // once you reach the one sixteenth size, draw the correct size tick mark for passed in length
    drawRuler(2.5, 0, 2.5, 1) // draw the second half of the 5" ruler starting at 2.5 
      // same as above
  drawRuler(5, 0, 5, 1) // draw the second half of the 10" ruler starting at 10
    // same as above
drawRuler(x, y, length, width)
{
   if (length > 1/16) then
      drawRuler(x, y, length / 2, width) // draw the left side
      drawRuler(x + (width * 1/2), y, length / 2, width) // draw the right side
   end if;

   // draw the tick mark here using Turtle Graphics.  The length of the tick mark 
   //   is based on the length parameter pass into the function.
   // For example,
   //   if the length is 10 or any even inch mark the length would be very long
   //   if the length is 5.5 or any 1/2 inch value the length would be long
   //   if the length is 2.25 or any 1/4 inch value the length would be medium
   //   if the length is 0.125 or any 1/8 inch value the length would be medium-short
   //   if the length is 0.0625 or any 1/16 inch value then the length would be short
}
函数看起来像这样

drawRuler(0, 0, 10, 1) // draw a ruler at 0,0 with a length of 10 and a width of 1
  drawRuler(0, 0, 5, 1) // draw the first half of the 10" ruler starting at 0
    drawRuler(2.5, 0, 2.5, 1) // draw the first half of the 5" ruler starting at 0
      // keep dividing the ruler until you reach the 1/16 size
      // once you reach the one sixteenth size, draw the correct size tick mark for passed in length
    drawRuler(2.5, 0, 2.5, 1) // draw the second half of the 5" ruler starting at 2.5 
      // same as above
  drawRuler(5, 0, 5, 1) // draw the second half of the 10" ruler starting at 10
    // same as above
drawRuler(x, y, length, width)
{
   if (length > 1/16) then
      drawRuler(x, y, length / 2, width) // draw the left side
      drawRuler(x + (width * 1/2), y, length / 2, width) // draw the right side
   end if;

   // draw the tick mark here using Turtle Graphics.  The length of the tick mark 
   //   is based on the length parameter pass into the function.
   // For example,
   //   if the length is 10 or any even inch mark the length would be very long
   //   if the length is 5.5 or any 1/2 inch value the length would be long
   //   if the length is 2.25 or any 1/4 inch value the length would be medium
   //   if the length is 0.125 or any 1/8 inch value the length would be medium-short
   //   if the length is 0.0625 or any 1/16 inch value then the length would be short
}