Gamedev Framework (gf)  0.9.0
A C++14 framework for 2D games
Classes | Public Types | Public Member Functions | List of all members
gf::RandomBinaryTree Class Reference

A random binary space partionning tree. More...

#include <gf/RandomBinaryTree.h>

Classes

class  Node
 A node of the random binary space partionning tree. More...
 

Public Types

using Callback = std::function< bool(const RandomBinaryTree &, const Node &)>
 A callback function for traversing the tree. More...
 

Public Member Functions

 RandomBinaryTree (const RectI &area)
 Constructor. More...
 
void create (Random &random, int levelMax, Vector2i minSize, Vector2i maxSize, float maxRatio=1.5f)
 Create the BSP tree. More...
 
const NodegetRoot () const
 Get the root node of the tree. More...
 
const NodegetLeftChild (const Node &node) const
 Get the left child of a node. More...
 
const NodegetRightChild (const Node &node) const
 Get the right child of a node. More...
 
const NodegetParent (const Node &node) const
 Get the parent of the node. More...
 
bool contains (Vector2i position) const
 Check if the tree contains a position. More...
 
const Nodefind (Vector2i position) const
 Find the deepest node containing a position. More...
 
bool traversePreOrder (Callback callback) const
 Traverse the nodes in pre-order. More...
 
bool traverseInOrder (Callback callback) const
 Traverse the nodes in in-order. More...
 
bool traversePostOrder (Callback callback) const
 Traverse the nodes in post-order. More...
 
bool traverseLevelOrder (Callback callback) const
 Traverse the nodes in level-order. More...
 
bool traverseInvertedLevelOrder (Callback callback) const
 Traverse the nodes in inverted level-order. More...
 

Detailed Description

A random binary space partionning tree.

Member Typedef Documentation

◆ Callback

using gf::RandomBinaryTree::Callback = std::function<bool(const RandomBinaryTree&, const Node&)>

A callback function for traversing the tree.

The function returns a boolean indicating if the traversal can continue.

Constructor & Destructor Documentation

◆ RandomBinaryTree()

gf::RandomBinaryTree::RandomBinaryTree ( const RectI area)

Constructor.

Parameters
areaThe area to split for this node

Member Function Documentation

◆ contains()

bool gf::RandomBinaryTree::contains ( Vector2i  position) const
inline

Check if the tree contains a position.

Parameters
positionThe position to check
Returns
True if the tree contains the position

◆ create()

void gf::RandomBinaryTree::create ( Random random,
int  levelMax,
Vector2i  minSize,
Vector2i  maxSize,
float  maxRatio = 1.5f 
)

Create the BSP tree.

Parameters
randomA random generator
levelMaxThe maximum level of a node
minSizeThe minimum size under which a node cannot be split
maxSizeThe maximum size over which a node must be split
maxRatioThe maximum ratio between the width and height

◆ find()

const Node& gf::RandomBinaryTree::find ( Vector2i  position) const

Find the deepest node containing a position.

An exception is thrown if the position is outside the tree.

Parameters
positionThe position to find
Returns
A node containig the position
See also
contains()

◆ getLeftChild()

const Node& gf::RandomBinaryTree::getLeftChild ( const Node node) const

Get the left child of a node.

Returns
The left child if it exists or the root node

◆ getParent()

const Node& gf::RandomBinaryTree::getParent ( const Node node) const

Get the parent of the node.

The root ot the tree is its own parent.

Returns
The father of the node

◆ getRightChild()

const Node& gf::RandomBinaryTree::getRightChild ( const Node node) const

Get the right child of a node.

Returns
The right child if it exists or the root node

◆ getRoot()

const Node& gf::RandomBinaryTree::getRoot ( ) const

Get the root node of the tree.

The root node always exist.

◆ traverseInOrder()

bool gf::RandomBinaryTree::traverseInOrder ( Callback  callback) const

Traverse the nodes in in-order.

Parameters
callbackA function to call on each node
See also
Tree traversal - Wikipedia

◆ traverseInvertedLevelOrder()

bool gf::RandomBinaryTree::traverseInvertedLevelOrder ( Callback  callback) const

Traverse the nodes in inverted level-order.

Parameters
callbackA function to call on each node
See also
Tree traversal - Wikipedia

◆ traverseLevelOrder()

bool gf::RandomBinaryTree::traverseLevelOrder ( Callback  callback) const

Traverse the nodes in level-order.

Level-order is also known as breadth first search

Parameters
callbackA function to call on each node
See also
Tree traversal - Wikipedia

◆ traversePostOrder()

bool gf::RandomBinaryTree::traversePostOrder ( Callback  callback) const

Traverse the nodes in post-order.

Parameters
callbackA function to call on each node
See also
Tree traversal - Wikipedia

◆ traversePreOrder()

bool gf::RandomBinaryTree::traversePreOrder ( Callback  callback) const

Traverse the nodes in pre-order.

Parameters
callbackA function to call on each node
See also
Tree traversal - Wikipedia