Header Ads

Hilbert's curve

The Hilbert's curve can be constructed by following successive approximations. A square
is divided into four quadrants.
We can draw the first approximation to the Hilbert's curve by connecting center points of
each quadrant .
The second approximation to the Hilbert's curve can be drawn by further subdividing
each of the quadrant and connecting their centers before moving to next major quadrant.
The third approximation subdivides the quadrants again. We can draw third
approximation to Hilbert's curve by connecting the centers of finest level of quadrants
before stepping to the next level of the quadrant.

From the above three figures, we can easily note following points about hilbert's curve
1. If we infinitely extend the approximations to the hilbert's curve, the curve fills the
smaller quadrants but never crosses itself.
2. The curve is arbitarilty close to every point in the square.
3. The curve passes thorugh a point on grid, which becomes twice as fine with each
subdivision.
4. There is no limit to subdivisions and therefore length of curve is infinite.
5. With each subdivision length of curve increases by factor of 4.
6. A each subdivision the scale changes by 2 but length changes by 4 therefore for
Hilbert's curve topological dimension is one but the fractal dimension is 2.

Algorithm:


1. Given an input value of n
2. call move function

3. call Hilbert's curve function

Output:


Display Hibert Curve.

Program:


#include<iostream>
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
using namespace std;
void move(int j,int h,int &x,int &y)
{
if(j==1)
y-=h;
else if(j==2)
x+=h;
else if(j==3)
y+=h;
else if(j==4)
x-=h;
lineto(x,y);
}
void hilbert(int r,int d,int l,int u,int i,int h,int &x,int &y)
{
if(i>0)
{
i--;
hilbert(d,r,u,l,i,h,x,y);
move(r,h,x,y);
hilbert(r,d,l,u,i,h,x,y);
move(d,h,x,y);
hilbert(r,d,l,u,i,h,x,y);
move(l,h,x,y);
hilbert(u,l,d,r,i,h,x,y);
}
}
int main()
{
int n,x1,y1;
int x0=50,y0=150,x,y,h=10,r=2,d=3,l=4,u=1;
cout<<"\n Enter the n:";
cin>>n;
x=x0;y=y0;
int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);
moveto(x,y);
hilbert(r,d,l,u,n,h,x,y);
getch();
closegraph();
return 0;
}

Learn how to run C/C++ graphics programs on Ubuntu here

No comments:

Powered by Blogger.