The Hilbert Curve represents a fractal curve that fills space and traverses every point within a square in a specified sequence. Mathematician David Hilbert first presented this concept in 1891. The fundamental idea behind the Hilbert Curve involves iteratively dividing the square into smaller squares and then linking these smaller squares to form a seamless curve.
Order of the Curve:
The level of the Hilbert Curve influences the number of iterations needed to generate the curve. As the value of the sequence grows, the curve becomes more intricate and intricate. The curve's level is typically denoted by the integer "n".
Construction of the Curve:
The Hilbert Curve is created through a process of recursive partitioning and rotation. At each recursion level, the curve is composed of four smaller curves, each representing one of the square's quadrants. The sequence in which these quadrants are traversed is meticulously planned to maintain the curve's continuity.
Example:
Let's consider an instance of a detailed process for generating and displaying Hilbert Curve points in the C programming language:
#include <stdio.h>
#include <math.h>
// Function to generate Hilbert Curve points
void hilbertCurve(int n, int x, int y, int xi, int xj, int yi, int yj)
{
if (n <= 0)
{
printf("(%d, %d) ", x, y);
return;
}
hilbertCurve(n - 1, x + xi * pow(2, n - 1), y + yi * pow(2, n - 1), xi, xj, yi, yj);
hilbertCurve(n - 1, x + xj * pow(2, n - 1), y + yj * pow(2, n - 1), xi, xj, yi, yj);
hilbertCurve(n - 1, x + xj * pow(2, n - 1) + xi, y + yj * pow(2, n - 1) + yi, xi, xj, yi, yj);
hilbertCurve(n - 1, x + xi * pow(2, n - 1) - xj, y + yi * pow(2, n - 1) - yj, -xi, -xj, -yi, -yj);
}
int main()
{
int n; // Order of the curve
printf("Enter the order of Hilbert Curve: ");
scanf("%d", &n);
int numPoints = pow(2, n * 2);
int sideLength = pow(2, n);
printf("Points on the Hilbert Curve:\n");
hilbertCurve(n, 0, 0, 1, 0, 0, 1);
return 0;
}
Output:
Enter the order of Hilbert Curve: 3
Points on the Hilbert Curve:
(7, 0) (6, 1) (7, 1) (7, -1) (5, 2) (4, 3) (5, 3) (5, 1) (6, 2) (5, 3) (6, 3) (6, 1) (5, -1) (6, -2) (5, -2) (5, 0) (3, 4) (2, 5) (3, 5) (3, 3) (1, 6) (0, 7) (1, 7) (1, 5) (2, 6) (1, 7) (2, 7) (2, 5) (1, 3) (2, 2) (1, 2) (1, 4) (4, 4) (3, 5) (4, 5) (4, 3) (2, 6) (1, 7) (2, 7) (2, 5) (3, 6) (2, 7) (3, 7) (3, 5) (2, 3) (3, 2) (2, 2) (2, 4) (1, -1) (2, -2) (1, -2) (1, 0) (3, -3) (4, -4) (3, -4) (3, -2) (2, -3) (3, -4) (2, -4) (2, -2) (3, 0) (2, 1) (3, 1) (3, -1)
Explanation:
- Header File Inclusions:
The code commences by including necessary header files, such as "stdio.h" for fundamental input and output functionalities, and "math.h" for mathematical computations such as the "pow function".
- The hilbertCurve Function:
This function uses recursion to produce the Hilbert Curve's points. There are numerous parameters:
- n: The level of the current recursion.
- (x,y): The location on the plane at the moment.
- (xi, xj): Unit vectors for the positive and negative directions along the x-axis are (xi, xj) .
- (yi, yj): Unit vectors for the positive and negative directions along the y-axis are (yi, yj) .
- Base Case:
When n reaches 0, it represents the base case of the recursion. In this scenario, the function will return the current coordinates (x, y) as a point on the curve.
- Recursive Steps:
The function uses four recursive calls for each level of recursion to represent the four quadrants of the current square. The position (x, y) and direction vectors are updated by these calls based on the quadrant and recursion level that are currently in use.
- Quadrant A is represented by the first recursive call , where the location changes in the direction of (xi, xj) .
- Quadrant B is represented by the second recursive call , where the position changes in the direction of (yi, yj) .
- Quadrant C is represented by the third recursive call , where the location shifts in the direction of both (xi, xj) and (yi, yj) .
- Quadrant D is represented by the fourth recursive call, where the location moves in the direction of (-xi, -xj) and (-yi, -yj) .
- main Function:
The individual is prompted to input the value of 'n', representing the order of the Hilbert Curve, within the main function.
To calculate the total number of points on the curve (numPoints) and the size of each side of the square (sideLength), the expressions pow(2, n * 2) and pow(2, n) are applied, correspondingly.
Subsequently, the hilbertCurve function is called with the specified parameters, and the resulting points of the curve are displayed on the console.
In this C program, the concept of the Hilbert Curve is demonstrated using recursion. The hilbertCurve function divides a square recursively based on the user-provided order to generate points along the curve. This example showcases how recursive algorithms can be employed to create complex geometric designs.