Skip to content

Instantly share code, notes, and snippets.

@stuartpb
Created September 9, 2014 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuartpb/d811fcc25acf36bb3a0d to your computer and use it in GitHub Desktop.
Save stuartpb/d811fcc25acf36bb3a0d to your computer and use it in GitHub Desktop.
#include <stdio.h>
int main(void) {
int i, x, y, e, o;
for (i=0; i < 64; i++){
//d2xy(8,i,&x,&y);
e =((i & 0x20) >> 3)
|((i & 0x08) >> 2)
|((i & 0x02) >> 1);
o =((i & 0x10) >> 2)
|((i & 0x04) >> 1)
|(i & 0x01);
printf("%d%d%d\n",
((e^o) & 0x04 ? 1 : 0),
((e^o) & 0x02 ? 1 : 0),
((e^o) & 0x01 ? 1 : 0) );
}
return 0;
}
//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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment