How to resolve the algorithm Peano curve step by step in the Python programming language
How to resolve the algorithm Peano curve step by step in the Python programming language
Table of Contents
Problem Statement
Produce a graphical or ASCII-art representation of a Peano curve of at least order 3.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Peano curve step by step in the Python programming language
This Python code uses the turtle graphics library to generate a Peano curve, which is a space-filling curve that fills a square in a continuous path.
The code starts by importing the turtle library and defines a global variable called stack
to keep track of the current recursion stack depth. The peano()
function takes an optional parameter iterations
that specifies the number of iterations to perform.
Inside the peano()
function, a turtle named ivan
is created and an app window is set up. The size of each step walked by the turtle is set to walk
, and a function screenlength()
is defined to calculate the appropriate screen size for the given number of iterations.
The "magic" of the Peano curve is implemented in two recursive functions, step1()
and step2()
. These functions use a combination of forward and turn commands to guide the turtle through the curve, and they call each other recursively to create the complex pattern.
The stack
variable is used to track the depth of the recursion stack, which provides a visual representation of the curve's complexity.
Finally, the peano()
function calls step2()
with the given number of iterations, and the turtle graphics window is displayed using tt.done()
.
After the drawing window is closed, the code uses the pylab
library to plot the stack
variable, which shows the stack depth at each step of the recursion. This plot provides insights into the recursive nature of the Peano curve.
Source code in the python programming language
import turtle as tt
import inspect
stack = [] # Mark the current stacks in run.
def peano(iterations=1):
global stack
# The turtle Ivan:
ivan = tt.Turtle(shape = "classic", visible = True)
# The app window:
screen = tt.Screen()
screen.title("Desenhin do Peano")
screen.bgcolor("#232323")
screen.delay(0) # Speed on drawing (if higher, more slow)
screen.setup(width=0.95, height=0.9)
# The size of each step walked (here, named simply "walk"). It's not a pixel scale. This may stay still:
walk = 1
def screenlength(k):
# A function to make the image good to see (without it would result in a partial image).
# This will guarantee that we can see the the voids and it's steps.
if k != 0:
length = screenlength(k-1)
return 2*length + 1
else: return 0
kkkj = screenlength(iterations)
screen.setworldcoordinates(-1, -1, kkkj + 1, kkkj + 1)
ivan.color("#EEFFFF", "#FFFFFF")
# The magic \(^-^)/:
def step1(k):
global stack
stack.append(len(inspect.stack()))
if k != 0:
ivan.left(90)
step2(k - 1)
ivan.forward(walk)
ivan.right(90)
step1(k - 1)
ivan.forward(walk)
step1(k - 1)
ivan.right(90)
ivan.forward(walk)
step2(k - 1)
ivan.left(90)
def step2(k):
global stack
stack.append(len(inspect.stack()))
if k != 0:
ivan.right(90)
step1(k - 1)
ivan.forward(walk)
ivan.left(90)
step2(k - 1)
ivan.forward(walk)
step2(k - 1)
ivan.left(90)
ivan.forward(walk)
step1(k - 1)
ivan.right(90)
# Making the program work:
ivan.left(90)
step2(iterations)
tt.done()
if __name__ == "__main__":
peano(4)
import pylab as P # This plot, after closing the drawing window, the "stack" graphic.
P.plot(stack)
P.show()
You may also check:How to resolve the algorithm Update a configuration file step by step in the TXR programming language
You may also check:How to resolve the algorithm Main step of GOST 28147-89 step by step in the Julia programming language
You may also check:How to resolve the algorithm Symmetric difference step by step in the jq programming language
You may also check:How to resolve the algorithm Maze solving step by step in the Action! programming language
You may also check:How to resolve the algorithm Bitmap/Midpoint circle algorithm step by step in the Nim programming language