1 18 package org.apache.batik.ext.awt.image.rendered; 19 20 import java.util.Iterator ; 21 import java.util.Vector ; 22 23 29 public class TileBlock { 30 int occX, occY, occW, occH; 31 int xOff, yOff, w, h, benefit; 32 boolean [] occupied; 33 34 44 TileBlock(int occX, int occY, int occW, int occH, boolean [] occupied, 45 int xOff, int yOff, int w, int h) { 46 this.occX = occX; 47 this.occY = occY; 48 this.occW = occW; 49 this.occH = occH; 50 this.xOff = xOff; 51 this.yOff = yOff; 52 this.w = w ; 53 this.h = h ; 54 this.occupied = occupied; 55 56 57 58 for (int y=0; y<h; y++) 62 for (int x=0; x<w; x++) 63 if (!occupied[x+xOff+occW*(y+yOff)]) 64 benefit++; 65 } 66 67 72 public String toString() { 73 String ret = ""; 74 for (int y=0; y<occH; y++) { 75 for (int x=0; x<occW+1; x++) { 76 if ((x==xOff) || (x==xOff+w)) { 77 if ((y==yOff) || (y==yOff+h-1)) 78 ret += "+"; 79 else if ((y>yOff) && (y<yOff+h-1)) 80 ret += "|"; 81 else 82 ret += " "; 83 } 84 else if ((y==yOff) && (x> xOff) && (x < xOff+w)) 85 ret += "-"; 86 else if ((y==yOff+h-1) && (x> xOff) && (x < xOff+w)) 87 ret += "_"; 88 else 89 ret += " "; 90 91 if (x== occW) 92 continue; 93 94 if (occupied[x+y*occW]) 95 ret += "*"; 96 else 97 ret += "."; 98 } 99 ret += "\n"; 100 } 101 return ret; 102 } 103 104 107 int getXLoc() { return occX+xOff; } 108 111 int getYLoc() { return occY+yOff; } 112 115 int getWidth() { return w; } 116 119 int getHeight() { return h; } 120 121 124 int getBenefit() { return benefit; } 125 126 130 int getWork() { return w*h+1; } 131 132 135 static int getWork(TileBlock [] blocks) { 136 int ret=0; 137 for (int i=0; i<blocks.length; i++) 138 ret += blocks[i].getWork(); 139 return ret; 140 } 141 142 147 TileBlock [] getBestSplit() { 148 if (simplify()) 149 return null; 150 151 if (benefit == w*h) 153 return new TileBlock [] { this }; 154 155 return splitOneGo(); 156 } 157 158 public TileBlock [] splitOneGo() { 159 boolean [] filled = (boolean [])occupied.clone(); 160 Vector items = new Vector (); 161 for (int y=yOff; y<yOff+h; y++) 162 for (int x=xOff; x<xOff+w; x++) { 163 if (!filled[x+y*occW]) { 164 int cw = xOff+w-x; 167 for (int cx=x; cx<x+cw; cx++) 168 if (filled[cx+y*occW]) 169 cw = cx-x; 170 else 171 filled[cx+y*occW] = true; 173 int ch=1; 177 for (int cy=y+1; cy<yOff+h; cy++) { 178 int cx=x; 179 for (; cx<x+cw; cx++) 180 if (filled[cx+cy*occW]) 181 break; 182 183 if (cx != x+cw) 185 break; 186 187 for (cx=x; cx<x+cw; cx++) 189 filled[cx+cy*occW] = true; 190 ch++; 191 } 192 items.add(new TileBlock(occX, occY, occW, occH, 193 occupied, x, y, cw, ch)); 194 x+=(cw-1); 195 } 196 } 197 198 TileBlock [] ret = new TileBlock[items.size()]; 199 Iterator iter = items.iterator(); 200 int i=0; 201 while (iter.hasNext()) 202 ret[i++] = (TileBlock)iter.next(); 203 return ret; 204 } 205 206 public boolean simplify() { 207 for (int y=0; y<h; y++) { 208 int x; 209 for (x=0; x<w; x++) 210 if (!occupied[x+xOff+occW*(y+yOff)]) 211 break; 212 if (x!=w) break; 213 214 yOff++; 216 y--; 217 h--; 218 } 219 220 if (h==0) return true; 222 223 225 for (int y=h-1; y>=0; y--) { 226 int x; 227 for (x=0; x<w; x++) 228 if (!occupied[x+xOff+occW*(y+yOff)]) 229 break; 230 if (x!=w) break; 231 232 h--; 234 } 235 236 for (int x=0; x<w; x++) { 237 int y; 238 for (y=0; y<h; y++) 239 if (!occupied[x+xOff+occW*(y+yOff)]) 240 break; 241 if (y!=h) break; 242 243 xOff++; 245 x--; 246 w--; 247 } 248 249 for (int x=w-1; x>=0; x--) { 250 int y; 251 for (y=0; y<h; y++) 252 if (!occupied[x+xOff+occW*(y+yOff)]) 253 break; 254 if (y!=h) break; 255 256 w--; 258 } 259 260 return false; 261 } 262 } 263 264 265 | Popular Tags |