0

I need your advice.

Okay, so I am having a problem with this algorithm. I'm using the recursive method of adding to and maintaining the binary tree for this program, but when Split Once is done 3 times, the code breaks.

For the sake of example, lets say we're going to force the algorithm's methods to always perform vertical splits and only vertical splits, and thus it only calls the VertiSplit method I have. The method is as follows. I am doing this from memory, so there may be a thing or two wrong... I am going for the simplest way as possible for this.

public void VertiSplit(Node curNode) //The node that called the method is passed in.
{
 int vRatio = curNode.height / curNode.width;
 //if( vRatio <=2) // Commented out so it never does a Horizontal Split.
 //{
     //Calculate the Split Point Data for the Parent node that called the method.
     curNode.splitX = curNode.xCoor + (curNode.width / 2);
     curNode.splitY = curNode.yCoor;
     curNode.splitW = 1; // Is 1 because it is passed into the draw call for the line.
     curNode.splitH = curNode.height;
     // Calculate Data for Children...
     curNode.child1.xCoor = curNode.xCoor;
     curNode.child2.xCoor = curNode.splitX;
     curNode.child1.yCoor = curNode.yCoor;
     curNode.child2.yCoor = curNode.yCoor;
     curNode.child1.width = curNode.splitX;
     curNode.child2.width = curNode.splitX;
     curNode.child1.height = curNode.height;
     curNode.child2.height = curNode.height; //At this point, calculations and assignment of the children's variables is completed. So I'll stop writing the code here..

Assume that below this point is a Report method, a closing bracket, and an else statement for the false condition of the above if statement that calls the Horizontal Split (HoriSplit) method. Going through this logically. The calculations should go like this. The dungeon size is 512 x 512 square. Split X is the object of interest here.

1st iteration: Node ID: 0 Split X: 256

2nd iteration: Node ID: 1 Split X: 128 Node ID: 2 Split X: 384

3rd iteration: Node ID: 3 Split X: 64 Node ID: 4 Split X: 192 Node ID: 5 Split X: 320 Node ID: 6 Split X: 448

However, for iteration #3, I instead get this:

3rd iteration: Node ID: 3 Split X: 64 Node ID: 4 Split X: 192 Node ID: 5 Split X: 448 Node ID: 6 Split X: 448

The way the code is written, it should not be doing this. Yet why is it that Node #5 i broken, while the others are okay? I really don't know at this point, and I am at my wit's end about it. Could someone help me?

Also, there's another thing that isn't working. I am trying to convert a temp string variable into 32-bit integer for use with a method called SplitCreateAll that takes the value of numSplits (which is supposed to be an integer) which is assigned the value of temp, which is controlled by a textbox in the the GUI for Unity. For some reason, every time I try to have temp parsed so that numSplits can be assigned it's value, after changing it from 0 by typing in the textbox, an Error occurs where it says that temp is not in the correct format. Can someone tell me what went wrong there too?

EDIT:

public void GrowBranches(Node curNode)
{
  int randomval = randNum.Next();

  if(curNode.child1 != null)
     {
       findLeaves(curNode.child1);
       findLeaves(curNode.child2);
     }
  if(curNode.child1 == null)
    {
      curNode.child1 = new Node();
      curNode.child2 = new Node();
      curNode.child1.parent = curNode;
      curNode.child2.parent = curNode;
      curNode.child1.sister = curNode.child2;
      curNode.child2.sister = curNode.child1;
    }
    if(randomval % 2 == 0)
    {
     VertiSplit(curNode);
    }
    else
    {
      HoriSplit(curNode);
    }

There is my GrowBranches method. This is where the Split Methods are called in the recursion chain. GrowBranches is also called by the methods SplitCreateOnce and SplitCreateAll, but the latter is non-functional due to the above stated reason involving the parsing error.

  • Without seeing your call site it's hard to help. Your recursive right-split might not be correct, you might have an incorrect offset somewhere, etc. – Jerdak Apr 18 '13 at 22:17
  • Call site? Please elaborate. I'm an absolute beginner here. – user2296141 Apr 19 '13 at 01:48
  • Apologies. You mention using a recursive method, it would be helpful to see where in your code you recursively call Vertisplit. – Jerdak Apr 19 '13 at 01:54
  • Oh. I see. Well. Lemme make a quick edit. It's in a function called GrowBranches that's part of the recursion chain this code is built on. – user2296141 Apr 19 '13 at 01:59
  • That's part of it, does Vertisplit ever recursively call itself or HoriSplit? – Jerdak Apr 19 '13 at 02:52
  • They do. It's dependant on the respective ratio variables in either function and the if else statements related to them. As of right now, the code is being forced to do Vertical Splits only in order to find the logic or calculation errors that are causing it to break. It always breaks on the 3rd iteration, and, as shown above, Node 5 for some reason thinks it SplitX is 448 when it should be 320. I dunno what would cause it, but since HoriSplit and VertiSplit's logic are mirrors of each other, they both will have the same problem... – user2296141 Apr 19 '13 at 02:56

1 Answers1

0

If I had to guess I'd say it was these 2 lines:

 curNode.child1.width = curNode.splitX;
 curNode.child2.width = curNode.splitX;

Looking at a node whose xCoord=256, width=256, splitX would be 386 (xCoord + 256/2) but that's not the width of the child node, the width is 256/2. With the code above you would have a child with xCoord 386, width 386.

As for the second part of your question about the text error, if you are using a Unity text box it's likely when you clear the existing value to start typing you end up passing an empty string to Convert.ToInt32. Just use a Try/Catch and ignore the error.

Jerdak
  • 3,997
  • 1
  • 24
  • 36
  • Another thing. This program I'm working on is for visualization purposes. The width being 1, as shown above, is for the DrawTexture call used to draw the lines that mark where the splits are. – user2296141 Apr 19 '13 at 03:37
  • Actually, I just did the math manually here at home on paper. The answers come out as follows: Node 5 splitX = 448 Node 6 splitX = 576 This means the code's doing something impossible... – user2296141 Apr 20 '13 at 16:23