不知道有没有人有这样的java源码,就是输入希尔伯特曲线的阶数,能返回一个希尔伯特曲线每个编号所在的x、y的二维坐标,当然这个坐标是一个数据集。或者输入希尔伯特曲线的阶数和编号,得到一个二维坐标。希望大神有源码,这个我实在很难自己想出来。谢谢大家了!
有人知道么?????
import java.util.Scanner;
class Point {
public int x; // X坐标
public int y; // X坐标
public Point() {
}
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public class 希尔伯特曲线 {
private void rot(int n, Point pt, int rx, int ry) {
if (ry == 0) {
if (rx == 1) {
pt.x = n - 1 - pt.x;
pt.y = n - 1 - pt.y;
}
//Swap x and y
int temp = pt.x;
pt.x = pt.y;
pt.y = temp;
}
}
//Hilbert代码到XY坐标
public void d2xy(int n, int d, Point pt) {
int rx, ry, s, t = d;
pt.x = pt.y = 0;
for (s = 1; s < n; s *= 2) {
rx = 1 & (t / 2);
ry = 1 & (t ^ rx);
rot(s, pt, rx, ry);
pt.x += s * rx;
pt.y += s * ry;
t /= 4;
}
}
//XY坐标到Hilbert代码转换
public int xy2d(int n, Point pt) {
int rx, ry, s, d = 1;
for (s = n / 2; s > 0; s /= 2) {
rx = ((pt.x & s) > 0) ? 1 : 0;
ry = ((pt.y & s) > 0) ? 1 : 0;
d += s * s * ((3 * rx) ^ ry);
rot(s, pt, rx, ry);
}
return d;
}
public static void main(String[] args) {
希尔伯特曲线 hilbert = new 希尔伯特曲线();
Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
int n =1<<(m);
long p=sc.nextInt();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
//System.out.printf("%2d ", hilbert.xy2d(n, new Point(j, i)));
if(hilbert.xy2d(n, new Point(j,i))==p) {
System.out.println(j+" "+i);
}
}
//System.out.println();
}
//System.out.println(hilbert.xy2d(n, new Point(3,0)));
}
}