作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
<分区>
我找到了下面的绘图代码Hilbert Curves在维基百科上:
//convert (x,y) to d
int xy2d (int n, int x, int y) {
int rx, ry, s, d=0;
for (s=n/2; s>0; s/=2) {
rx = (x & s) > 0;
ry = (y & s) > 0;
d += s * s * ((3 * rx) ^ ry);
rot(s, &x, &y, rx, ry);
}
return d;
}
//convert d to (x,y)
void d2xy(int n, int d, int *x, int *y) {
int rx, ry, s, t=d;
*x = *y = 0;
for (s=1; s<n; s*=2) {
rx = 1 & (t/2);
ry = 1 & (t ^ rx);
rot(s, x, y, rx, ry);
*x += s * rx;
*y += s * ry;
t /= 4;
}
}
//rotate/flip a quadrant appropriately
void rot(int n, int *x, int *y, int rx, int ry) {
if (ry == 0) {
if (rx == 1) {
*x = n-1 - *x;
*y = n-1 - *y;
}
//Swap x and y
int t = *x;
*x = *y;
*y = t;
}
}
我把它放进了c2rust.com将 rot() 的定义作为第一个函数并得到以下代码:
#![allow(dead_code,
mutable_transmutes,
non_camel_case_types,
non_snake_case,
non_upper_case_globals,
unused_mut)]
#![feature(libc)]
extern crate libc;
// rotate/flip a quadrant appropriately
#[no_mangle]
pub unsafe extern "C" fn rot(mut n: libc::c_int,
mut x: *mut libc::c_int,
mut y: *mut libc::c_int,
mut rx: libc::c_int,
mut ry: libc::c_int)
-> () {
if ry == 0i32 {
if rx == 1i32 {
*x = n - 1i32 - *x;
*y = n - 1i32 - *y
}
// Swap x and y
let mut t: libc::c_int = *x;
*x = *y;
*y = t
};
}
// convert (x,y) to d
#[no_mangle]
pub unsafe extern "C" fn xy2d(mut n: libc::c_int,
mut x: libc::c_int,
mut y: libc::c_int)
-> libc::c_int {
let mut rx: libc::c_int = 0;
let mut ry: libc::c_int = 0;
let mut s: libc::c_int = 0;
let mut d: libc::c_int = 0i32;
s = n / 2i32;
while s > 0i32 {
rx = (x & s > 0i32) as libc::c_int;
ry = (y & s > 0i32) as libc::c_int;
d += s * s * (3i32 * rx ^ ry);
rot(s, &mut x, &mut y, rx, ry);
s /= 2i32
}
return d;
}
// convert d to (x,y)
#[no_mangle]
pub unsafe extern "C" fn d2xy(mut n: libc::c_int,
mut d: libc::c_int,
mut x: *mut libc::c_int,
mut y: *mut libc::c_int)
-> () {
let mut rx: libc::c_int = 0;
let mut ry: libc::c_int = 0;
let mut s: libc::c_int = 0;
let mut t: libc::c_int = d;
*y = 0i32;
*x = *y;
s = 1i32;
while s < n {
rx = 1i32 & t / 2i32;
ry = 1i32 & (t ^ rx);
rot(s, x, y, rx, ry);
*x += s * rx;
*y += s * ry;
t /= 4i32;
s *= 2i32
}
}
fn main() {
let mut x: libc::c_int = 0;
let mut y: libc::c_int = 0;
let d: libc::c_int = 10;
unsafe {
for n in 1..10 {
d2xy(n, d, &mut x, &mut y);
println!("n={}, x={}, y={}", n, x, y);
}
}
}
问题是结果似乎没有意义:
Compiling x v0.1.0 (file:///tmp/x)
warning: value assigned to `rx` is never read ] 0/1: x
--> src/main.rs:34:13
|
34 | let mut rx: libc::c_int = 0;
| ^^
|
= note: #[warn(unused_assignments)] on by default
warning: value assigned to `ry` is never read
--> src/main.rs:35:13
|
35 | let mut ry: libc::c_int = 0;
| ^^
warning: value assigned to `s` is never read
--> src/main.rs:36:13
|
36 | let mut s: libc::c_int = 0;
| ^
warning: value assigned to `rx` is never read
--> src/main.rs:55:13
|
55 | let mut rx: libc::c_int = 0;
| ^^
warning: value assigned to `ry` is never read
--> src/main.rs:56:13
|
56 | let mut ry: libc::c_int = 0;
| ^^
warning: value assigned to `s` is never read
--> src/main.rs:57:13
|
57 | let mut s: libc::c_int = 0;
| ^
Finished dev [unoptimized + debuginfo] target(s) in 0.25s
Running `target/debug/x`
n=1, x=0, y=0
n=2, x=1, y=1
n=3, x=3, y=3
n=4, x=3, y=3
n=5, x=3, y=3
n=6, x=3, y=3
n=7, x=3, y=3
n=8, x=3, y=3
n=9, x=3, y=3
我绝对没想到不同的N会重复坐标。可能出了什么问题?下面是用于比较的C测试代码:
int main() {
int n=32, d=0, x=0, y=0;
for (d=0; d<10; d++) {
d2xy(n, d, &x, &y);
printf("d=%d, x=%d, y=%d\n", d, x, y);
}
}
以及预期的输出(由 C 生成):
d=0, x=0, y=0
d=1, x=0, y=1
d=2, x=1, y=1
d=3, x=1, y=0
d=4, x=2, y=0
d=5, x=3, y=0
d=6, x=3, y=1
d=7, x=2, y=1
d=8, x=2, y=2
d=9, x=3, y=2
我从面向 Java 和报表开发人员的 Eclipse Luna SR2 IDE 设计了非常简单的 Student.rptdesign 文件。它的数据源是BIRT POJO Data Source。从那
我是一名优秀的程序员,十分优秀!