-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKoch_curve_generator.py
31 lines (25 loc) · 987 Bytes
/
Koch_curve_generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def koch(n):
"""
Generate the instructions for drawing a Koch curve of order n.
Parameters:
n (int): The order of the Koch curve.
Returns:
str: A string of instructions representing the Koch curve.
"""
# Base case: when n is 0, the curve is just a single forward move
if n == 0:
return 'F' # 'F' means move forward
# Recursive case: generate the curve by combining parts of lower orders
previous = koch(n - 1)
# The Koch curve of higher order is formed by combining:
# - One copy of the previous curve
# - A left turn ('L')
# - Another copy of the previous curve
# - A right turn ('R')
# - One more copy of the previous curve
# - A left turn ('L')
# - A final copy of the previous curve
return previous + 'L' + previous + 'R' + previous + 'L' + previous
# Test the function with different values of n (from 0 to 9)
for i in range(10):
print(f"Koch curve of order {i}: {koch(i)}")