CG/HilbertCurve.cpp

68 lines
988 B
C++
Raw Normal View History

2023-11-02 11:47:33 +05:30
#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);
2023-11-02 11:53:35 +05:30
hilbert(d,r,u,l,n,h,x,y);
2023-11-02 11:47:33 +05:30
getch();
closegraph();
return 0;
}