private static DirPoint binSearch(long f, int dir, int old_d) { int d = (int) (Math.log(f) / Math.log(2d)); long froof = (long) Math.pow(2d, (double) (d)); pos += froof; long f_diff = f - froof; // System.out.println("d: " + d + " froof: " + // froof+ " fdiff " + f_diff); if (f != froof) { froof *= 2; } System.out.println( "\nf " + (st - f) + " dir " + dir + " realdir: " + poss[(int) (st - f)] + " d " + d + " f_diff " + f_diff); // System.out.println("froof " + froof); if (froof != 0) { DirPoint dp = getXY(d, dir); if (old_d > d + 1) { direction = (direction + 4) % 8; } else { // direction = 2; } // System.out.println("dp "+dp +" poss " +poss[pos] ); DirPoint dp2 = binSearch(f_diff, dp.dir, d); dp.Add(dp2); return dp; } return null; }
private static DirPoint getXY(int d, int dir) { /* i 0 x 0 y 1 dir: 0 i 1 x 1 y 1 dir: 1 i 3 x 2 y 0 dir: 2 i 7 x 2 y -2 dir: 3 i 15 x 0 y -4 dir: 4 i 31 x -4 y -4 dir: 5 i 63 x -8 y 0 dir: 6 i 127 x -8 y 8 dir: 7 i 255 x 0 y 16 dir: 0 i 511 x 16 y 16 dir: 1 i 1023 x 32 y 0 dir: 2 0:y+1 6:x-1 dir 2:x+1 4:y-1 */ DirPoint p = new DirPoint(); long value = (long) Math.pow(2d, (double) (d / 2)); switch ((dir + d) % 8) { case 0: p.x = 0; p.y = value; break; case 1: p.x = value; p.y = value; break; case 2: p.x = value; p.y = 0; break; case 3: p.x = value; p.y = -value; break; case 4: p.x = 0; p.y = -value; break; case 5: p.x = -value; p.y = -value; break; case 6: p.x = -value; p.y = 0; break; case 7: p.x = -value; p.y = value; break; } p.dir = (dir + direction) % 8; return p; }