static class Rect{ static final int RIGHT = 0; static final int DOWN = 1; static final int LEFT = 2; static final int UP = 3; static final int DOWN_RIGHT = 4; static final int DOWN_LEFT = 5; static final int UP_LEFT = 6; static final int UP_RIGHT = 7; int x,y,width,height,cx,cy; Rect(int x, int y, int width, int height){ this.x = x; this.y = y; this.width = width; this.height = height; updateCenter(); } Rect(){ this(0,0,0,0); } void updateCenter(){ cx = x+(width>>1); cy = y+(height>>1); } boolean contains(int x, int y){ return x >= this.x && y >= this.y && x < this.x+width && y < this.y+height; } static boolean intersects(Rect a, Rect b){ return !(a.x > b.x+(b.width-1) || a.x+(a.width-1) < b.x || a.y > b.y+(b.height-1) || a.y+(a.height-1) < b.y); } // 0: right, 1: down, 2: left, 3: up int voronoi(int x, int y){ updateCenter(); int vx = x-cx; int vy = y-cy; if(x == cx && y == cy) return RIGHT; if(x >= this.x+width && y >= this.y && y < this.y+height) return RIGHT; if(x < this.x && y >= this.y && y < this.y+height) return LEFT; if(y >= this.y+height && x >= this.x && x < this.x+width) return DOWN; if(y < this.y && x >= this.x && x < this.x+width) return UP; if(x >= this.x+width && y >= this.y+height) return DOWN_RIGHT; if(x < this.x && y >= this.y+height) return DOWN_LEFT; if(x < this.x && y < this.y) return UP_LEFT; if(x >= this.x+width && y < this.y) return UP_RIGHT; //establish octant if (x > cx && y > cy && abs(vy) < abs(vx)) return RIGHT; if (x > cx && y > cy && abs(vy) > abs(vx)) return DOWN; if (x < cx && y > cy && abs(vy) > abs(vx)) return DOWN; if (x < cx && y > cy && abs(vy) < abs(vx)) return LEFT; if (x < cx && y < cy && abs(vy) < abs(vx)) return LEFT; // reversal of octant 0 if (x < cx && y < cy && abs(vy) > abs(vx)) return UP; // reversal of octant 1 if (x > cx && y < cy && abs(vy) > abs(vx)) return UP; // reversal of octant 2 if (x > cx && y < cy && abs(vy) < abs(vx)) return RIGHT; // reversal of octant 3 //line must be on division of octants if (y == cy && x > cx) return RIGHT; if (x == cx && y > cy) return DOWN; if (y == cy && x < cx) return LEFT; if (x == cx && y < cy) return UP; if(vx == abs(vx) && vy == -abs(vy)) return UP_RIGHT; if(vy == abs(vy) && vx == abs(vx)) return DOWN_RIGHT; if(vy == abs(vy) && vx == -abs(vx)) return DOWN_LEFT; if(vy == -abs(vy) && vx == -abs(vx)) return UP_LEFT; return -1; } int voronoi4(int x, int y){ updateCenter(); int vx = x-cx; int vy = y-cy; if(x == cx && y == cy) return RIGHT; if(x >= this.x+width && y >= this.y && y < this.y+height) return RIGHT; if(x < this.x && y >= this.y && y < this.y+height) return LEFT; if(y >= this.y+height && x >= this.x && x < this.x+width) return DOWN; if(y < this.y && x >= this.x && x < this.x+width) return UP; //establish octant if (x > cx && y > cy && abs(vy) < abs(vx)) return RIGHT; if (x > cx && y > cy && abs(vy) > abs(vx)) return DOWN; if (x < cx && y > cy && abs(vy) > abs(vx)) return DOWN; if (x < cx && y > cy && abs(vy) < abs(vx)) return LEFT; if (x < cx && y < cy && abs(vy) < abs(vx)) return LEFT; // reversal of octant 0 if (x < cx && y < cy && abs(vy) > abs(vx)) return UP; // reversal of octant 1 if (x > cx && y < cy && abs(vy) > abs(vx)) return UP; // reversal of octant 2 if (x > cx && y < cy && abs(vy) < abs(vx)) return RIGHT; // reversal of octant 3 //line must be on division of octants if (y == cy && x > cx) return RIGHT; if (x == cx && y > cy) return DOWN; if (y == cy && x < cx) return LEFT; if (x == cx && y < cy) return UP; if(vx == abs(vx) && vy == -abs(vy)) return RIGHT; if(vy == abs(vy) && vx == abs(vx)) return DOWN; if(vy == abs(vy) && vx == -abs(vx)) return LEFT; if(vy == -abs(vy) && vx == -abs(vx)) return UP; return -1; } } class DrawRect extends Rect{ DrawRect(int x, int y, int width, int height){ super(x, y, width, height); } void draw(){ rect(x, y, width, height); /*pushMatrix(); translate(x+(width>>1), y+(height>>1)); box(width, height, height); popMatrix();*/ } }