Need help making a Hilbert Curve using numbers in Python

2024/11/18 13:59:53

I want to make a function that will create a Hilbert Curve in python using numbers. The parameters for the function would be a number and that will tell the function how many times it should repeat. To make a Hilbert Curve you start with 'L', then that turns into '+RF-LFL-FR+', and then 'R' turns into '-LF+RFR+FL-' How should I do this?

#Here is what I've made so far
def hilbert(num):s = 'L'for i in range(num-1):s = s.replace('L','+RF-LFL-FR+')b = 'R'for i in range(num-1):b = b.replace('R','-LR+RFR+FL-')end = s + breturn end

It crashes completely when you enter 1, I tried to use to code I made for the Koch snowflake but I wasn't sure how to use the two variables.

#Here is the results for when I use the function
hilbert(1)
#It returns
a crash bruh
hilbert()
#It returns 
'+RF-+RF-LFL-FR+F+RF-LFL-FR+-FR+-L-LR+RFR+FL-+-LR+RFR+FL-F-LR+RFR+FL-+FL-'#Here is what I want it to return
hilbert(1)
'L'
hilbert(3)
'+-LF+RFR+FL-F-+RF-LFL-FR+F+RF-LFL-FR+-F-LF+RFR+FL-+'

I'm not that good at the range loop, how should I do this?

Answer

In the code you provided you are testing with # 1 as input. In this case:

for i in range(num-1):

is not fulfilled and your for-loop is never initialized since your i is already past that range.

Below you can see a sample code that you can use as reference when playing with Hilbert Curve:

import turtleturtle.speed(speed=10)  # Fastesthilbert_seq = "a"for _ in range(5):new_seq = ""for char in hilbert_seq:if char == "a":new_seq += "-bF+aFa+Fb-"elif char == "b":new_seq += "+aF-bFb-Fa+"else:new_seq += charhilbert_seq = new_seqfor char in hilbert_seq:if char == "F":turtle.forward(9)elif char == "+":turtle.right(90)elif char == "-":turtle.left(90)

Screenshot from the above sample code:

Screenshot of running turtle graphics implementation of the Hilbert Curve

https://en.xdnf.cn/q/118663.html

Related Q&A

How do I separate each list [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Why python doesnt see the members of quantumCircuit class qiskit

I`m trying to learn the programming on quantum computers. I have installed qiskit in VS Code (all qiskit extentions available in VS Code market) , python compilator (from Vs Code market "Python&qu…

Compare multiple lines in a text file using python

Im writing a program that is time tracking report that reports the time that was spent in each virtual machine, I`m having a problem comparing the txt file cause the only thing that changes are the num…

Error installing eomaps through conda and pip

I am getting the following error output when trying to install eomaps using Conda. I dont know how to solve this. I have also tried the same using pip but it didnt seem to solve the problem. Here is th…

DFS on a graph using a python generator

I am using a generator to do a full search on a graph, the real data set is fairly large, here is a portion of the code i wrote on a small data set:class dfs:def __init__(self):self.start_nodes = [1,2]…

Importing test libraries failed. No module named a

I have a folder /example which contains /Libs which further contains different folders /a, /b each containing python libraries. I am trying to run a robot framework code from /example.The error it show…

Sorting characters by count using PHP or Python

I have a string of charactersabcdefghijklmnopqrstuvwxyz_ I want to take this string of characters and sort them by the number of times they appear in a large block of characters. For example: cwrxwzb…

Save user first_name as default value for model django

I have an article model with author variable which I want to save as the users first and last name. I use custom user model called Account. author = models.CharField(author,max_length=50 default=User.f…

How to compare two imagefile from two different files in python

I would like to create a program that compares two images. I need to take images from two different folders and compare that images if they are same or not. Then I want to print out as same or differen…

Cant import from module despite presence of __init__.py

I have the following folder structureproject_folder/pyutils/__init__.pyscript1.pyscript2.py lambdas/__init__.pylambda_script1.pylambda_script2.pylambda_tests/__init__.pylambda_test1.pyWithin lambda_tes…