Hilbert curve added

This commit is contained in:
Tanmay 2023-11-02 11:53:35 +05:30
parent f6d253bd0e
commit 93248064d2
2 changed files with 68 additions and 5 deletions

View File

@ -60,11 +60,7 @@ int main()
cin>>n; cin>>n;
moveto(x,y); moveto(x,y);
//1. d,r,u; hilbert(d,r,u,l,n,h,x,y);
//hilbert(d,r,u,l,n,h,x,y);
//2. r,d,l;
hilbert(r,d,l,u,n,h,x,y);
getch(); getch();
closegraph(); closegraph();
return 0; return 0;

67
HilbertCurve.cpp~ Normal file
View File

@ -0,0 +1,67 @@
#include<iostream>
#include<graphics.h>
#include<math.h>
#include<cstdlib>
#include<stdlib.h>
using namespace std;
void move(int j, int h, int &x, int &y)
{
if(j == 1)
{
y = y - h;
}
else if(j == 2)
{
x = x + h;
}
else if(j == 3)
{
y = y + h;
}
else if(j == 4)
{
x = x - h;
}
setcolor(BROWN);
lineto(x,y);
}
void hilbert(int d, int r, int u, int l, int i, int h, int &x, int &y)
{
if(i > 0)
{
i--;
hilbert(r,d,l,u,i,h,x,y);
move(d,h,x,y);
hilbert(d,r,u,l,i,h,x,y);
move(r,h,x,y);
hilbert(d,r,u,l,i,h,x,y);
move(u,h,x,y);
hilbert(l,u,r,d,i,h,x,y);
}
}
int main()
{
int gd = DETECT,gm;
initgraph(&gd,&gm,NULL);
int x,y,h,n,u = 1,r = 2,d = 3,l = 4;
cout<<"Enter x-co-ordinate of initial point: ";
cin>>x;
cout<<"Enter y-co-ordinate of initial point: ";
cin>>y;
cout<<"Enter length of line segment: ";
cin>>h;
cout<<"Enter order of curve: ";
cin>>n;
moveto(x,y);
hilbert(d,r,u,l,n,h,x,y);
getch();
closegraph();
return 0;
}