Determining The Most Left Path From Current Path for Fractal-esque Random Generation

Hey everyone, unity noob with a decent amount of programming and game making experience.
I currently have a grid generator that generates a grid with a random angle within a polygon, in this case a square but could be any polygon that has no opening. I’ve chopped all the lines up but they intersections as you can see here:
2942405--217794--unityforumposttwo.JPG
So in order to turn each enclosed box into individual boundaries and repeat the process (think fractal) I’ve written code that goes crawls through a list of LineSegment (two Vectors, start and end, other stuff too), goes to one end of the line, finds other LineSegments that share that point, and picks the leftmost one. It repeats that process until it gets back to the original, thus creating an enclosed box. I’ve added ways of determining if the line has been used twice and the other lines that a line has shared boxes with to prevent repeats from happening.
This is my result:
2942405--217795--unityforumpostone.JPG
(ignore the blue red and green)
Those are all enclosed boxes, but unfortunately my code is leaving out a lot of them.
After about 12 hours of fixing things that made the code better but didn’t fix this problem i’m thinking that my problem has to do with my DetermineMostLeft function.
I have another function that works well called GetAngle which returns a float that is the angle in radians. I didnt want to use included Unity functions because they’re overly complicated for my 2d grid. Here is that function:

    static float GetAngle(Vector3 start, Vector3 end) {
        float unfixedSlope = (start.z - end.z) / (start.x - end.x);
        float angle = Mathf.Abs(Mathf.Atan(unfixedSlope));

        if (angle == Mathf.PI / 2 || angle == 0) {
            if (start.x == end.x) {
                if (start.z > end.z) {
                    angle = Mathf.PI * 1.5f;
                } else {
                    angle = Mathf.PI / 2;
                }
            } else if (start.z == end.z) {
                if (start.x < end.x) {
                    angle = 0;
                } else {
                    angle = Mathf.PI;
                }
            }
        } else {
            if ((start.x > end.x && start.z < end.z)) {

                angle += Mathf.PI / 2;
            } else if ((start.x > end.x && start.z > end.z)) {

                angle += Mathf.PI;
            } else if ((start.x < end.x && start.z > end.z)) {

                angle += Mathf.PI * 1.5f;
            } else {

            }
        }
        //Debug.Log (angle * 57.2958f);

        return angle;
    }

So I’m confident that the GetAngle function works correctly as bad/verbose as it may be.
Here’s the DetermineMostLeft function that I’m not confident about. It returns the index of the linesegment in the iLines list. Here it is:

  static int[] TempMostLeft(List<LineSegment> iLines, int[,] range, LineSegment currentLine, int startOrEnd)
    {
        List<IntFloat> angles = new List<IntFloat>();
        Vector3 endPos;
        float angle;
        if (startOrEnd == 1) {
            angle = GetAngle(currentLine.Start, currentLine.End);
            endPos = currentLine.End;
        } else
        {
            angle = GetAngle(currentLine.End, currentLine.Start);
            endPos = currentLine.Start;
        }

        for (int i = 0; i < range.GetLength(0); i++)
        {
            float testAngle;
            bool boo;
            if (range[i, 1] == 0)
            {
                testAngle = GetAngle(iLines[range[i, 0]].Start, iLines[range[i, 0]].End);
                boo = false;
            } else
            {
                testAngle = GetAngle(iLines[range[i, 0]].End, iLines[range[i, 0]].Start);
                boo = true;
            }
            float newAngle = testAngle - angle;
            if (newAngle < 0f)
            {
                newAngle += Mathf.PI * 2;
            }
            angles.Add(new IntFloat(range[i, 0], newAngle, boo));
        }

        angles.Sort((x, y) => x.floating.CompareTo(y.floating));
        Debug.Log("brrr");
        int[] left = new int[2];

        if (angles.Count - 1 < 0)
        {
            Debug.Log("test");
        }
        if (angles[angles.Count - 1].floating < Mathf.PI)
        {

            left[0] = angles[angles.Count - 1].integer;
            if (angles[angles.Count - 1].boo == true)
            {
                left[1] = 0;
            } else
            {
                left[1] = 1;
            }
            return left;
        } else
        {
            for (int i = 0; i < angles.Count - 1; i++)
            {
                if (angles[i].floating < Mathf.PI && angles[i + 1].floating >= Mathf.PI && angles[i].floating != 0f)
                {
                    left[0] = angles[i].integer;
                    if (endPos == iLines[angles[i].integer].Start)
                    {
                        left[1] = 1;
                    } else
                    {
                        left[1] = 0;
                    }
                    return left;
                }
            }
        }
        Debug.Log("no left turn");
        left[0] = -1;
        left[1] = -1;
        return left;
    }

Any thoughts? I know this is a very loose question but I’m so stumped and having spent like 60 hours writing this fractal generator I really want it to work!
PS: I use Vector3s to represent 2d because I like to use the y variable to encode other information for failures etc. like .y == 1f means something is wrong.
PPS: I know I should have put the two points for a line in an array as opposed to .start or .end. I screwed that up awhile ago and its too late lol

the function that gives me range[ ] is here. it returns a array that has line segments with points that share the point “im at” as well as whether that point is a .start or .end:

   static int[,] TempVectorSearch(List<LineSegment> iLines, Vector3 currentPos, int index) {
        List<int[]> preTurnable = new List<int[]>(); 
        for (int j = 0; j < iLines.Count; j++)
        {
            if (index != j)
            {
                if ( ApproximateVector(iLines[j].Start, currentPos) )
                {
                    int[] candidate = new int[2];
                    candidate[0] = j;
                    candidate[1] = 0;
                    preTurnable.Add(candidate);

                    //test
                    if (iLines[j].Start != currentPos)
                    {
                        Debug.Log("test");
                        Debug.DrawLine(iLines[j].Start + Vector3.up, iLines[j].End + Vector3.up, Color.green, 200000, true);
                    }
                }
                if ( ApproximateVector(iLines[j].End, currentPos) )
                {
                    int[] candidate = new int[2];
                    candidate[0] = j;
                    candidate[1] = 1;
                    preTurnable.Add(candidate);
                }
            }
        }
        int[,] returnable = new int[preTurnable.Count, 2];
        for (int j = 0; j < preTurnable.Count; j++)
        {
            returnable[j, 0] = preTurnable[j][0];
            returnable[j, 1] = preTurnable[j][1];
        }
        return returnable;
    }
    //below func must be fixed, until then use above function!
    static int[,] BinaryVectorSearch(List<VectorIndex>   positions, Vector3 currentPos, int currentIndex){
        int start = 0;
        int end = positions.Count;
        float posMag = currentPos.magnitude;
        int foundIndex = -1;
        Debug.Log ("BinaryVectorSearch:");
        Debug.Log (posMag);
        int testCounter = 0;

        while (start <= end && testCounter < 15) {
            testCounter++;
            Debug.Log ("testing:");
            int mid = start + ((end - start) / 2);
            Debug.Log (mid);
            Debug.Log (posMag - positions [mid].position.magnitude);
            if (positions [mid].position.magnitude == posMag) {
                start = mid;
                end = mid;
                foundIndex = mid;
                Debug.Log ("found!");
                break;
            }
            //below gotta figure out
            if (positions [mid].position.magnitude < posMag) {
                start = mid + 1;
            }
            if (positions [mid].position.magnitude > posMag) {
                end = mid - 1;
            }
        }

        Debug.Log (foundIndex);

        bool caseOpen = true;
        int[] range = new int[2];
        range [0] = foundIndex;
        range [1] = foundIndex;
        /// here at thing that goes down till its no longer magnitude, up doing the same using the new range[] array
        /// 
        /// 
        while (true){
            if (range [0] == 0) {
                break;
            }
            if (positions [range [0] - 1].position.magnitude == posMag) {
                range [0]--;
            } else {
                break;
            }
        }
        while (true) {
            if (range [1] == positions.Count - 1) {
                break;
            }
            if (positions [range [1] + 1].position.magnitude == posMag) {
                range [1]++;
            } else {
                break;
            }
        }

        Debug.Log ("-----____------");
        Debug.Log (range [0]);
        Debug.Log (range[1]);
        int length = (range [1] - range [0]) + 1;
        List<int[]> returnable = new List<int[]>();

        for (int i = range[0]; i < range[0] + length; i++){
            if (positions [i].position.x == currentPos.x) {
                int[] thisDex = new int[2];
                thisDex [0] = positions [    i].index;
                if (positions [i].isStart) {
                    thisDex [1] = 0;
                } else {
                    thisDex [1] = 1;
                }
                returnable.Add (thisDex);
            }
        }
        Debug.Log ("real return");
        int[,] realReturn = new int[returnable.Count, 2];
        for (int i = 0; i < returnable.Count; i++) {
            realReturn [i, 0] = returnable [i] [0];
            realReturn [i, 1] = returnable [i] [1];
            Debug.Log ("--");
            Debug.Log (realReturn [i, 0]);
        }
        return realReturn;
    }

oh whoops ignore BinaryVectorSearch(). Just the top one TempVectorSearch()

I figured out one of the errors! changed line 61 of the DetermineMostLeft to

   if (angles[i].floating < Mathf.PI && ( Mathf.Abs(angles[i+1].floating - Mathf.PI) < 0.001 || angles[i + 1].floating > Mathf.PI) && !Mathf.Approximately(angles[i].floating, 0f ) )

sucks that floating point numbers aren’t very accurate
i guess it just took looking at the code from a different source (ie on this forum) to solve the problem!
still missing like 2 or 3 boxes per grid, i’ll figure that one out though!