Ejemplo n.º 1
0
 public double Distance(GSColor color)
 {
     return(color.Distance(new GSColor(
                               (byte)(Begin.R + (Begin.Distance(color) / Length * (End.R - Begin.R))),
                               (byte)(Begin.G + (Begin.Distance(color) / Length * (End.G - Begin.G))),
                               (byte)(Begin.B + (Begin.Distance(color) / Length * (End.B - Begin.B))))));
 }
Ejemplo n.º 2
0
        public List <Pallet> GetKeyColorList()
        {
            List <GSColor> queue         = new List <GSColor>(ColorList);
            List <GSColor> buffer        = new List <GSColor>();
            List <GSColor> lineColorList = new List <GSColor>();

            GSColor begin = new GSColor();
            GSColor end   = new GSColor();

            List <ColorLine> lineList = new List <ColorLine>();

            int i;

            Stopwatch stopwatch = new Stopwatch();

            Console.WriteLine("Getting key color data...");

            stopwatch.Start();

            ColorLine dummy = new ColorLine(queue[0], queue[1]);

            for (i = 0; i < queue.Count && !lineColorList.Any(); i++)
            {
                //1番目に数の多い色と2番目に数の多い色を通る直線を引く
                dummy = new ColorLine(queue[0], queue[i]);

                //その直線に近い点をすべてlineColorListに追加,queueから削除
                lineColorList.AddRange(queue.FindAll(color => dummy.Distance(color) < CLOSENESS));
            }

            queue.RemoveAll(color => lineColorList.Contains(color));

            //その直線に近い点の中で,直線の端に近い点(影が一番長い)を始点,終点にする
            //影の長さでソート 降順?
            lineColorList = new List <GSColor>(lineColorList.OrderBy(color => dummy.Begin.Projection(dummy.End, color)));

            begin = lineColorList.First();
            end   = lineColorList.Last();

            //直線をlineListに追加
            lineList.Add(new ColorLine(begin, end));

            int count = 0;

            while (queue.Any())
            {
                //終点を始点にしてqueueの中で1番に遠い色を終点とする直線を引く
                dummy = new ColorLine(end, queue.OrderBy(color => end.Distance(color)).First());

                //lineのBeginよりEnd側をlineColorListに追加
                queue.OrderBy(color => dummy.Begin.Projection(dummy.End, color));
                for (i = 0; i < queue.Count - 1; i++)
                {
                    if (dummy.Begin.Projection(dummy.End, queue[i]) < 0)
                    {
                        break;
                    }
                }
                lineColorList = new List <GSColor>(queue.GetRange(0, i));

                Console.WriteLine("count: " + lineColorList.Count);

                //lineに近い点のみwaitListに追加,queueから削除
                lineColorList = new List <GSColor>(lineColorList.OrderBy(color => - dummy.Distance(color)));

                for (i = 0; i < lineColorList.Count - 1; i++)
                {
                    if (dummy.Distance(lineColorList[i]) >= CLOSENESS)
                    {
                        break;
                    }
                }
                lineColorList.RemoveRange(i, lineColorList.Count - i);
                queue.RemoveAll(color => lineColorList.Contains(color));

                Console.WriteLine("Count: " + lineColorList.Count);

                //その直線に近い点の中で,直線の端に近い点(影が一番長い)を終点にする始点は直線の始点
                //影の長さでソート 降順?
                lineColorList = new List <GSColor>(lineColorList.OrderBy(color => dummy.Begin.Projection(dummy.End, color)));

                begin = dummy.Begin;
                try
                {
                    end = lineColorList.Last();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                //直線をlineListに追加
                lineList.Add(new ColorLine(begin, end));

                count++;
            }

            //lineListの直線の長さの合計とそれぞれの直線の長さから長さの割合を出す
            //その割合に合わせてKeyColorListに追加

            stopwatch.Stop();

            Console.WriteLine("Key colors data was gotten successfully!");
            Console.WriteLine("Elapsed: " + stopwatch.Elapsed.ToString());
            Console.WriteLine("CaluculateTime: " + count.ToString());

            double LengthSum = lineList.Sum(line => line.Length);

            List <Pallet> palletList = new List <Pallet>();

            palletList.Add(new Pallet(lineList.First().Begin, 0));
            lineList.ForEach(line =>
            {
                Console.WriteLine((int)(line.Length / LengthSum * 255));
                palletList.Add(new Pallet(line.End, (byte)(line.Length / LengthSum * 255)));
            });
            return(palletList);
        }