Example #1
0
        public static PointList RoughScan(PointList inputList)
        {
            int rowCount = (int)Math.Round(Math.Sqrt(inputList.Count) / 2f) * 2;

            decimal lowestY = inputList.Min(p => p.Y);
            decimal highestY = inputList.Max(p => p.Y);
            decimal rowHeight = (highestY - lowestY) / rowCount;

            List<int> rowIndices = new List<int>();
            for (int i = 0; i < rowCount; i++)
                rowIndices.Add(i);

            List<List<Point>> rows = rowIndices.ConvertAll(rowIndex => inputList.FindAll(point => point.Y >= (lowestY + rowHeight * rowIndex) &&
                                                                                                  point.Y < (lowestY + rowHeight * (rowIndex + 1))));
            rows[rows.Count - 1].AddRange(inputList.FindAll(point => point.Y == highestY));
            rows = rows.Where(r => r.Count > 0).ToList();

            PointList path = new PointList();
            for (int i = 0; i < rows.Count; i++)
            {
                PointList rowPath = new PointList(rows[i].OrderBy(point => point.X * (decimal)Math.Pow(-1f, i)));
                rowPath = nearestNeighbor(rowPath, rowPath[0]);
                rowPath.RemoveAt(rowPath.Count - 1);
                path.AddRange(rowPath);
            }

            path.Add(path[0]);
            return path;
        }