# Hilbert curve program in C

In this article, we will discuss the Hilbert curve program in C. But before discussing the program, we must know about the Hilbert curve.

## Hilbert Curve:

The Hilbert Curve is a fractal space-filling curve that passes through each point in a square in a predetermined order. David Hilbert, a mathematician, was introduced it for the first time in 1891. Recursively breaking the square into smaller squares and then connecting these smaller squares in a way that creates a continuous curve is the main notion behind the Hilbert Curve.

## Order of the Curve:

The order of the Hilbert Curve controls how many recursive steps must be taken to produce the curve. The curve gets increasingly complex and convoluted as the sequence increases. The curve's order is often represented by the positive integer "n".

## Construction of the Curve:

The Hilbert Curve is built via recursive subdivision and rotation. Four smaller curves, one for each of the square's four quadrants, make up each level of recursion. The order in which these quadrants are visited is carefully considered to ensure that the curve stays continuous.

### Example:

Let's take an example of step-by-step Hilbert Curve point generation and presentation in C:

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:

1. Header File Inclusions:

The code starts by incorporating the required header files, including "stdio.h" for common input and output functions and "math.h" for mathematical operations like "pow()".

2. 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).

3. Base Case:

When n decreases to 0, it is the recursion's default scenario. Here, the function outputs the current location (x, y) as a point on the curve.

4. 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).

5. main Function:

The user is invited to enter the Hilbert Curve's order (n) in the main() function.

• The formulas pow(2, n * 2) and pow(2, n), respectively, are used to determine the number of points on the curve (numPoints) and the length of the square's sides (sideLength).
• After that, the hilbertCurve function is invoked with the given arguments. The generated curve points are printed to the console.

In this C program, the Hilbert Curve concept is illustrated via recursion. By the user-supplied order, the hilbertCurve function recursively divides a square to produce points on the curve. The application is an excellent illustration of how recursive algorithms may be utilized to produce intricate geometric patterns.

For Videos Join Our Youtube Channel: Join Now