aboutsummaryrefslogtreecommitdiff
path: root/Week8 Quadtrees/src/qtrees/GreyNode.java
blob: 326b0af1c28a59e4939cb17805c74838c4860fcc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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;
    }

}