Tuesday, June 9, 2015

Google : QuadNode

public class QuadNode{
  private QuadNode[] children;
  private Color color;
  final Rectangle rectangle;
  
  public QuadNode(boolean boolColor, int x, int y, int w, int h){
    this.color = colorFor(boolColor);
    rectangle = new Rectangle(x, y, w, h);
  }

  public void setColor(int x, int y, boolean boolColor){
    if (!rectangle.contains(x,y)) throw new IllegalArgumentException(“index out of range”);;

    if (isLeaf()){
      if (colorMatch(boolColor)){
        return;
      } else if (rectangle.area() == 1){
        setColor(boolColor);
        return;
      } else {
        //adding 4 new children
        split();
      }
    }

    QuadNode child = childFor(x,y);
    child.setColor(x, y, boolColor);

    //all kids have same color
    if (isCompressable()){
      removeChildren();
      setColor(boolColor);
    }
  }

  public int numberOfPixelsGivenColor(boolean boolColor){
    if (isLeaf()){
       if (colorMatch(boolColor)){
         return rectangle.area();
       } else {
         return 0;
       }
    }

    int ret = 0;
    for(QuadNode child : children){
      ret += child.numberOfPixelsGivenColor(boolColor);
    }
    return ret;
  }
  ...
}
class Rectangle{
  int x, y, w, h;
  ....
}

No comments:

Post a Comment