Advertisement
here2share

# t_random_2D_mountain.py

Aug 5th, 2020
1,943
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. # t_random_2D_mountain.py
  2.  
  3. import turtle
  4. import math
  5. import random
  6.  
  7. screen = turtle.Screen()
  8. screen.setup(1200,600)
  9. screen.title("Random 2D Mountain")
  10.  
  11. turtle.hideturtle()
  12. turtle.speed(0)
  13. turtle.pensize(2)
  14. turtle.color('dark green')
  15. MAX_SLOPE = 45
  16. MIN_SLOPE = -45
  17. MIN_HEIGHT = -200
  18. def dist_squared(P1,P2):
  19.     return (P1[0]-P2[0])**2 + (P1[1]-P2[1])**2
  20.  
  21. def mountain(P1,P2):
  22.     turtle.up()
  23.     turtle.goto(P1)
  24.     turtle.down()
  25.     def mtnDraw(P1,P2):
  26.         if dist_squared(P1,P2) < 9:
  27.             turtle.goto(P2)
  28.             return
  29.         x1,y1 = P1
  30.         x2,y2 = P2
  31.         x3 = random.uniform(x1,x2)
  32.         y3_max = min((x3-x1)*math.tan(math.radians(MAX_SLOPE)) + y1, (x2-x3)*math.tan(-math.radians(MIN_SLOPE)) + y2)
  33.         y3_min = max((x3-x1)*math.tan(math.radians(MIN_SLOPE)) + y1, (x2-x3)*math.tan(-math.radians(MAX_SLOPE)) + y2)
  34.         y3_min = max(y3_min, MIN_HEIGHT)
  35.         y3 = random.uniform(y3_min,y3_max)
  36.         P3 = (x3, y3)
  37.         mtnDraw(P1,P3)
  38.         mtnDraw(P3,P2)
  39.     mtnDraw(P1,P2)
  40.     return
  41.  
  42. mountain((-500,MIN_HEIGHT),(500,MIN_HEIGHT))
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement