public BspTreeNode(int x, int y, int width, int height, BspTreeNode parent = null) { X = x; Y = y; Width = width; Height = height; Parent = parent; }
public void Split(int where, bool verticaly) { if (verticaly) { where = Mathf.Clamp(where, 0, Width); Item0 = new BspTreeNode(X, Y, where, Height, this); Item1 = new BspTreeNode(X + Item0.Width, Y, Width - Item0.Width, Height, this); } else { where = Mathf.Clamp(where, 0, Height); Item0 = new BspTreeNode(X, Y, Width, Height - where, this); Item1 = new BspTreeNode(X, Y + Item0.Height, Width, Height - Item0.Height, this); } }
private static void BuildLeaf(BspTreeNode parent, int depth, Random rnd) { if (depth == 0) { return; } var gotoCount = 100; start: if (gotoCount == 0) { return; } gotoCount--; var vecticaly = rnd.Pick(true, false); var where = rnd.Next(vecticaly ? parent.Width / 4 : parent.Height / 4, vecticaly ? parent.Width - parent.Width / 4 : parent.Height - parent.Height / 4); parent.Split(where, vecticaly); if (vecticaly && (parent.Item0.Width / (float)parent.Item0.Height < 0.45 || parent.Item1.Width / (float)parent.Item1.Height < 0.45)) { parent.Clear(); goto start; } if (!vecticaly && (parent.Item0.Height / (float)parent.Item0.Width < 0.45 || parent.Item1.Height / (float)parent.Item1.Width < 0.45)) { parent.Clear(); goto start; } BuildLeaf(parent.Item0, depth - 1, rnd); BuildLeaf(parent.Item1, depth - 1, rnd); }
public BspTree(int width, int height) { Root = new BspTreeNode(0, 0, width, height); }
public BspTree(int x, int y, int width, int height) { Root = new BspTreeNode(x, y, width, height); }
public void Clear() { Item0 = null; Item1 = null; }