Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
hilbert
/* Hilbert Curve */ #include <iostream> #include <math.h> using namespace std; int hilbert(int x, int y, int iter){ if(iter ==0) return 1; int n = iter-1; int num = 1<<(2*n); // 4^n = 2^(2n) = 2^n * 2^n = 1<<(2*n) int len = 1<<n; // 2^n = 1<<n; int res = num; if(x >= len && y >=len) // The first quadrant res = num * 2 + hilbert(x-len, y-len, iter-1); // shape is the same else if (x<len && y>=len) // 2nd quadrant res = num + hilbert(x, y-len, iter-1); else if (x<len && y < len) // 3rd res = hilbert(y, x, iter-1); //clock-wise 90 degree else res = num*3 + hilbert( len-y, len-x+len, iter-1); // len-1-y, len-1-x return res; } int main(){ cout << "(0,1,1)=2: " << hilbert(0,1,1) << endl; cout << "(1,1,2)=3: " << hilbert(1,1,2) << endl; cout << "(2,2,2)=9: " << hilbert(2,2,2) << endl; return 0; }
run
|
edit
|
history
|
help
0
TIME
Ineritance
1234
Stream10
003#
RuntimeError
Test 4(2020)
543
Dictionary
sin_approximation