简体   繁体   中英

drawing a jagged mountain curve using turtle-graphics and recursion

I am trying to create a function for a homework assignment which draws a jagged mountain curve using turtles and recursion. The function is called jaggedMountain(x,y,c,t) where x x,y are end coordinates, c is a complexity constant, and t is the turtle object. I am trying to create an image like this: 山曲线

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)

This crashes quickly as the base case never executes, the function is called 993 times, and the recursion depth is exceeded. I have been scratching my head with this for quite some time, are there any suggestions?

Initially, I see two issues with your code. The first is:

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

Turtles wander a floating point plane, the odds of these being exactly equal is small. You're likely better off doing something like:

def distance(x1, y1, x2, y2):
    return ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5

...

    if distance(x1, y1, x, y) < 1.0:

The second issue is that jaggedCurve() draws nothing nor returns anything that can be used for drawing. Somewhere you need to actually move the turtle to cause something to be drawn.

Finally, though it's hard to be certain without a value for c , my guess is even with the above changes you won't get you what you want. Good luck.

Very interesting problem!

My solution is to make a recursive function that draws a mountain curve given two end points. Randomly pick ax coordinate value that lies in between two end points and compute the range of possible y coordinate given the maximum possible slope and randomly pick ay value in between this range and do this recursively. When to end points are close enough, just draw the line between them. Here is the code:

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))

I know this was posted like 3 months ago, but hopefully this is helpful to someone that was also assigned this terrible problem 5 days before finals! Ha!

The struggle I had with this problem was not realizing that you only need to pass in one point. To get the point the turtle is starting at, you just use .xcor() and .ycor() that are included in the turtle library.

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 ()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM