public BSPNode(IntRect area) { area_ = area; }
public BSPNode() { area_ = new IntRect(); }
void Split( int minSize, SplitDirection dir, int position ) { int axis = dir == SplitDirection.Random ? Random.Range(0, 2) : (int)dir; int otherAxis = 1 - axis; if (position == -1) position = Random.Range(minSize, area_.size_[otherAxis] - minSize); // Debug.Log("Position: " + position); // Size of the "left over" area var remainingAreaOtherAxisSize = area_.size_[otherAxis] - position; // Size of the area inside the slice var slicedAreaOtherAxisSize = area_.size_[otherAxis] - remainingAreaOtherAxisSize; // Size of both new nodes along slice axis var axisSize = area_.size_[axis]; var remainingAreaSize = new IntVector2(); remainingAreaSize[axis] = axisSize; remainingAreaSize[otherAxis] = remainingAreaOtherAxisSize; var slicedAreaSize = new IntVector2(); slicedAreaSize[axis] = axisSize; slicedAreaSize[otherAxis] = slicedAreaOtherAxisSize; if (axis == 0) { IntRect remainingArea = new IntRect(area_.xMin_, area_.yMin_ + position, remainingAreaSize.x, remainingAreaSize.y); IntRect slicedArea = new IntRect(area_.xMin_, area_.yMin_, slicedAreaSize.x, slicedAreaSize.y); childNodes_[0] = new BSPNode(remainingArea); childNodes_[1] = new BSPNode(slicedArea); } if (axis == 1) { IntRect slicedArea = new IntRect(area_.xMin_, area_.yMin_, slicedAreaSize.x, slicedAreaSize.y); IntRect remainingArea = new IntRect(area_.xMin_ + slicedArea.w_, area_.yMin_, remainingAreaSize.x, remainingAreaSize.y); childNodes_[0] = new BSPNode(slicedArea); childNodes_[1] = new BSPNode(remainingArea); } childNodes_[0].parent_ = this; childNodes_[0].sibling_ = childNodes_[1]; childNodes_[1].sibling_ = childNodes_[0]; childNodes_[1].parent_ = this; }