/// <summary>
        ///  Writes results of search to window.
        /// </summary>
        public void WriteResults()
        {
            int CompareLimit = Convert.ToInt32(CompareTextBox.Text);
            int CorrectNum   = 0;
            int TotalCorrect = 0;
            int num          = MainData.NumTests();

            for (int i = 0; i < num; i++)
            {
                MainDataStructure.TestPoint tp = MainData.GetTestPoint(i);
                int    expectedPoint           = tp.ExpectedPoint;
                double minimum             = tp.CalculatedMin;
                int    calculatedPoint     = tp.CalculatedPoint;
                int    calculatedDirection = tp.CalculatedDirection;
                int    expectedDirection   = (testInd == -1) ? tp.ExpectedDirection : testInd;
                string ptcorrect           = (Math.Abs(calculatedPoint - expectedPoint) < CompareLimit) ? "TRUE" : "False";
                string dircorrect          = (calculatedDirection == expectedDirection) ? "TRUE" : "False";
                string bothcorrect         = (ptcorrect.Contains("TRUE") && dircorrect.Contains("TRUE")) ? "TRUE" : "False";
                rtb.AppendText("Test " + i + ":  Expected: " + expectedPoint + "   Got: " + calculatedPoint + "  with " + minimum + "  Expected Dir: " + expectedDirection + "  Got: " + calculatedDirection + "  Pt? " + ptcorrect + "  Dir? " + dircorrect + " Both? " + bothcorrect + Environment.NewLine);
                if (ptcorrect.Contains("TRUE"))
                {
                    CorrectNum++;
                }

                if (bothcorrect.Contains("TRUE"))
                {
                    TotalCorrect++;
                }
            }

            double ptpercent   = (double)CorrectNum * 100.0 / (double)num;
            double bothpercent = (double)TotalCorrect * 100.0 / (double)num;

            rtb.AppendText(Environment.NewLine + "Score: " + CorrectNum + "\\" + num + "   " + ptpercent + '%');
            rtb.AppendText(Environment.NewLine + "Score: " + TotalCorrect + "\\" + num + "   " + bothpercent + '%');
        }
        /// <summary>
        /// Performs a sort of K Nearest Neighbour.
        /// </summary>
        public void KNNSearch()
        {
            CurrentLocation = -1;
            limit           = Convert.ToInt32(UpperTextBox.Text);
            lowerlimit      = Convert.ToInt32(LowerTextBox.Text);
            testInd         = Convert.ToInt32(TestOrientBox.Text);

            int simNum = Convert.ToInt32(SimulateBox.Text);
            List <List <double> >         Tests     = MainData.GetFlattenedTests(testInd, simNum);
            List <List <double> >         map       = MainData.GetFlattenedMap(simNum, -1);
            List <List <List <double> > > Distances = new List <List <List <double> > >();

            // Get distance metrics
            for (int i = 0; i < Tests.Count; i++)
            {
                Distances.Add(GetDistWithDirec(map, Tests[i]));
            }

            // Perform search
            int currentTest = 0;

            foreach (List <List <double> > test in Distances)
            {
                // Get minimums
                List <int>    MinInds = new List <int>();
                List <double> Mins    = new List <double>();
                foreach (List <double> orientation in test)
                {
                    List <object> details = GetMin(limit, orientation.Count, orientation);
                    Mins.Add((double)details[0]);
                    MinInds.Add((int)details[1]);
                }

                // Find min orientation
                double TotalMin       = Mins.Min();
                int    OrientationInd = Mins.IndexOf(TotalMin);
                int    PointInd       = MinInds[OrientationInd];

                // First test must be 0 (?)
                if (currentTest == 0)
                {
                    PointInd = 0;
                }


                MainDataStructure.TestPoint point = MainData.GetTestPoint(currentTest);
                point.CalculatedPoint     = PointInd;
                point.CalculatedDirection = OrientationInd;
                point.CalculatedMin       = TotalMin;
                MainData.SetTest(currentTest, point);
                CurrentLocation = PointInd;


                Console.WriteLine("Test " + currentTest + ":  Expected: " + point.ExpectedPoint + "  Got: " + PointInd + " with " + TotalMin);
                Console.WriteLine("                Direction -> Expected: " + point.ExpectedDirection + "  Got: " + OrientationInd);
                Console.WriteLine("All mins");
                for (int j = 0; j < Mins.Count; j++)
                {
                    Console.WriteLine("Min:" + Mins[j] + "  at: " + MinInds[j]);
                }
                Console.WriteLine("---------------------------" + Environment.NewLine);
                currentTest++;
            }
        }