diff options
author | Camil Staps | 2015-04-18 13:44:44 +0200 |
---|---|---|
committer | Camil Staps | 2015-04-18 13:44:44 +0200 |
commit | 6a44b074f0169a1b0f9e92347af929c5e471746e (patch) | |
tree | ae5663fe7c69881bf4ecfedbef99c2505f8ec964 /Week8 Quadtrees/src/qtrees/GreyNode.java | |
parent | Added copyright to docs (diff) |
Reorganised projects
Diffstat (limited to 'Week8 Quadtrees/src/qtrees/GreyNode.java')
-rw-r--r-- | Week8 Quadtrees/src/qtrees/GreyNode.java | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/Week8 Quadtrees/src/qtrees/GreyNode.java b/Week8 Quadtrees/src/qtrees/GreyNode.java new file mode 100644 index 0000000..326b0af --- /dev/null +++ b/Week8 Quadtrees/src/qtrees/GreyNode.java @@ -0,0 +1,59 @@ +package qtrees; + +import java.io.IOException; +import java.io.Writer; + +/** + * A "grey" node (contains both black and white leaves) + * + * @author Camil Staps, s4498062 + */ +public class GreyNode extends QTNode { + + /** The four children, starting at North West, counting clockwise */ + private final QTNode children[]; + + public GreyNode() { + children = new QTNode[4]; + } + + @Override + public void fillBitmap(int x, int y, int width, Bitmap bitmap) { + children[0].fillBitmap(x, y, width / 2, bitmap); + children[1].fillBitmap(x + width / 2, y, width / 2, bitmap); + children[2].fillBitmap(x + width / 2, y + width / 2, width / 2, bitmap); + children[3].fillBitmap(x, y + width / 2, width / 2, bitmap); + } + + @Override + public void writeNode(Writer out) throws IOException { + out.write("1"); + for (QTNode child : children) + child.writeNode(out); + } + + /** + * Set one of the children nodes + * @param index the position of the child: 0 = NW; 1 = NE; 2 = SE; 3 = SW + * @param child the child node + */ + public void setChild(int index, QTNode child) { + assert (index >= 0 && index <= 3); + + children[index] = child; + } + + /** + * Compress the current node + * @return a BlackLeaf if all the children are black, a WhiteLeaf if all the + * children are white, or itself otherwise. + */ + QTNode compress() { + if (children[0].getClass() == children[1].getClass() + && children[1].getClass() == children[2].getClass() + && children[2].getClass() == children[3].getClass()) + return children[0]; + return this; + } + +} |