예제 #1
0
        public LineParams Search(IReadOnlyList <Dot> dots)
        {
            double     minSum        = int.MaxValue;
            int        middleX       = dots.Sum(d => d.X) / dots.Count;
            int        middleY       = dots.Sum(d => d.Y) / dots.Count;
            var        middleDot     = new Dot(middleX, middleY);
            LineParams besLineParams = default;

            for (double a = 0; a <= Math.PI; a += Math.PI / 1000)
            {
                var    line = new LineParams(middleDot, a);
                double sum  = dots.Sum(dot => line.GetDistance(dot)) / dots.Count;

                if (minSum > sum)
                {
                    minSum        = sum;
                    besLineParams = line;
                }
            }

            return(besLineParams);
        }
예제 #2
0
        public LineParams Search(IReadOnlyList <Dot> dots)
        {
            var    e       = 0.1;
            var    minLine = new LineParams(0, 0, 0);
            double min     = int.MaxValue;

            Parallel.For(-255, 255, c =>
            {
                for (double a = -1; a < 1; a += e)
                {
                    for (double b = -1; b < 1; b += e)
                    {
                        var line   = new LineParams(a, b, c);
                        double sum = 0;
                        foreach (Dot dot in dots)
                        {
                            sum += line.GetDistance(dot);

                            if (sum > min)
                            {
                                break;
                            }
                        }

                        if (sum >= min)
                        {
                            continue;
                        }

                        minLine = new LineParams(a, b, c);
                        min     = sum;
                    }
                }
            });
            return(minLine);
        }