Skip to content

add Hilbert Curve #12273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions fractals/hilbert_curve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"""
Hilbert Curve

The Hilbert curve is a space-filling curve that recursively fills a square
with an intricate pattern. It is used in computer science, graphics, and various fields.

Reference:
https://en.wikipedia.org/wiki/Hilbert_curve

Requirements:
- matplotlib
- numpy
"""

import matplotlib.pyplot as plt


def hilbert_curve(order, x0=0, y0=0, xi=1, xj=0, yi=0, yj=1):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: hilbert_curve. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: order

Please provide type hint for the parameter: x0

Please provide type hint for the parameter: y0

Please provide type hint for the parameter: xi

Please provide type hint for the parameter: xj

Please provide type hint for the parameter: yi

Please provide type hint for the parameter: yj

"""
Generates the points for the Hilbert curve of the specified order.

The Hilbert curve is built using recursive rules. This function returns
a list of (x, y) points.

Parameters:
- order: the recursion depth or order of the curve
- x0, y0: the starting coordinates
- xi, xj: the transformation matrix for x coordinates
- yi, yj: the transformation matrix for y coordinates

>>> len(hilbert_curve(1))
5
>>> len(hilbert_curve(2))
17
>>> len(hilbert_curve(3))
65
"""
if order == 0:
return [(x0 + (xi + yi) / 2, y0 + (xj + yj) / 2)]

points = []
points += hilbert_curve(order - 1, x0, y0, yi / 2, yj / 2, xi / 2, xj / 2)
points += hilbert_curve(
order - 1, x0 + xi / 2, y0 + xj / 2, xi / 2, xj / 2, yi / 2, yj / 2
)
points += hilbert_curve(
order - 1,
x0 + xi / 2 + yi / 2,
y0 + xj / 2 + yj / 2,
xi / 2,
xj / 2,
yi / 2,
yj / 2,
)
points += hilbert_curve(
order - 1,
x0 + xi / 2 + yi,
y0 + xj / 2 + yj,
-yi / 2,
-yj / 2,
-xi / 2,
-xj / 2,
)

return points


def plot_hilbert_curve(points):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: plot_hilbert_curve. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file fractals/hilbert_curve.py, please provide doctest for the function plot_hilbert_curve

Please provide type hint for the parameter: points

"""
Plots the Hilbert curve using matplotlib.
"""
x, y = zip(*points)
plt.plot(x, y)
plt.title("Hilbert Curve")
plt.gca().set_aspect("equal", adjustable="box")
plt.show()


if __name__ == "__main__":
order = 5 # Order of the Hilbert curve
curve_points = hilbert_curve(order)
plot_hilbert_curve(curve_points)
Loading