hmardirosian

BSP

Sep 8th, 2020
695
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1.                 internal static BSPTree Split(int i_NumberOfIterations, RectInt i_Container)
  2.                 {
  3.                     var t_node = new BSPTree(i_Container);
  4.                     if (i_NumberOfIterations == 0) return t_node;
  5.  
  6.                     var splittedContainers = SplitContainer(i_Container);
  7.                     t_node.m_left = Split(i_NumberOfIterations - 1, splittedContainers[0]);
  8.                     t_node.m_right = Split(i_NumberOfIterations - 1, splittedContainers[1]);
  9.  
  10.                     return t_node;
  11.                 }
  12.  
  13.                 private static RectInt[] SplitContainer(RectInt i_container)
  14.                 {
  15.                     RectInt t_c1, t_c2;
  16.                     if (UnityEngine.Random.Range(0f, 1f) > 0.5f)
  17.                     {
  18.                         // vertical
  19.                         t_c1 = new RectInt(i_container.x, i_container.y, i_container.width, (int)UnityEngine.Random.Range(i_container.height * 0.3f, i_container.height * 0.5f));
  20.                         t_c2 = new RectInt(i_container.x, i_container.y + t_c1.height, i_container.width, i_container.height - t_c1.height);
  21.                     }
  22.                     else
  23.                     {
  24.                         // horizontal
  25.                         t_c1 = new RectInt(i_container.x, i_container.y, (int)UnityEngine.Random.Range(i_container.width * 0.3f, i_container.width * 0.5f), i_container.height);
  26.                         t_c2 = new RectInt(i_container.x + t_c1.width, i_container.y, i_container.width - t_c1.width, i_container.height);
  27.                     }
  28.                     return new RectInt[] { t_c1, t_c2 };
  29.                 }
Advertisement
Add Comment
Please, Sign In to add comment