Exemplo n.º 1
0
    void Divide(BinaryNode node)
    {
        if (node.Area() < opts.MAX_LEAF_AREA)
        {
            return;
        }

        int direction = 0;

        if (node.Width < node.Height)
        {
            direction = 1;
        }

        if (direction == 0) // split vertically
        {
            int x = Random.Range(opts.MIN_LEAF_SIDE, node.Width - opts.MIN_LEAF_SIDE);
            node.Children[0] = new BinaryNode(Point.Zero, new Point(node.GlobalPos), x, node.Height);
            node.Children[1] = new BinaryNode(new Point(x, 0), new Point(node.GlobalPos.X + x, node.GlobalPos.Y), node.Width - x, node.Height);
        }
        else // split horizontally
        {
            int y = Random.Range(opts.MIN_LEAF_SIDE, node.Height - opts.MIN_LEAF_SIDE);
            node.Children[0] = new BinaryNode(Point.Zero, new Point(node.GlobalPos), node.Width, y);
            node.Children[1] = new BinaryNode(new Point(0, y), new Point(node.GlobalPos.X, node.GlobalPos.Y + y), node.Width, node.Height - y);
        }

        Divide(node.Children[0]);
        Divide(node.Children[1]);
    }