Beispiel #1
0
        private void btn_closestPair_Click(object sender, RoutedEventArgs e)
        {
            ClosestPair closestPair = new ClosestPair(Points);

            closestPair.Step += ClosestPair_Step;
            closestPair.Run();
        }
        /// <summary>
        /// Finds an approximately closest pair of points (one of each color) by using the ordering found in SortedPoints.
        ///
        /// This compares points in two of the clusters, ignoring points in all other clusters.
        ///
        /// NOTE: This was a good idea, but yields results too poor to be used.
        /// </summary>
        /// <param name="color1">Label of the first cluster.</param>
        /// <param name="color2">Label of the second cluster.</param>
        /// <returns>The point with Color1, the point with Color2 and the square distance between them.
        /// </returns>
        public ClosestPair FindPairApproximately(TLabel color1, TLabel color2)
        {
            var           shortest  = new ClosestPair();
            UnsignedPoint prevP     = null;
            TLabel        prevColor = default(TLabel);

            foreach (var pc in SortedPoints
                     .Select(p => new { Point = p, Color = Clusters.GetClassLabel(p) })
                     .Where(pc => pc.Color.Equals(color1) || pc.Color.Equals(color2)))
            {
                if (prevP != null && !prevColor.Equals(pc.Color))
                {
                    var d = pc.Point.Measure(prevP);
                    if (d < shortest.SquareDistance)
                    {
                        shortest.SquareDistance = d;
                        shortest.Color1         = prevColor;
                        shortest.Point1         = prevP;
                        shortest.Color2         = pc.Color;
                        shortest.Point2         = pc.Point;
                    }
                }
                prevP     = pc.Point;
                prevColor = pc.Color;
            }
            return(shortest.Swap(color1));
        }
        /// <summary>
        /// Finds exactly the cluster closest to the cluster whose label matches color1 and the points
        /// in each cluster that are closest, along with the square distance between them.
        /// </summary>
        /// <param name="color1">Label of the cluster whose nearest neighbor is being sought.</param>
        /// <returns>A point in the cluster corresponding to color1, the closest point to it
        /// from another cluster, the square distance between the points, and the label of the other cluster.
        /// NOTE: ClosestPair.Color1 will equal color1.
        /// </returns>
        public ClosestPair FindClusterExhaustively(TLabel color1)
        {
            var shortest = new ClosestPair();

            foreach (var p1 in Clusters.PointsInClass(color1))
            {
                foreach (var pc in Clusters.Points()
                         .Select(p => new { Point = p, Color = Clusters.GetClassLabel(p) })
                         .Where(pc => !color1.Equals(pc.Color)))
                {
                    var d = p1.Measure(pc.Point);
                    if (d < shortest.SquareDistance)
                    {
                        shortest.SquareDistance = d;
                        shortest.Color1         = color1;
                        shortest.Point1         = p1;
                        shortest.Color2         = pc.Color;
                        shortest.Point2         = pc.Point;
                    }
                }
            }
            //TODO: If there is only one cluster, the if statement above will not be triggered and shortest will
            //      be ill-defined and cause a Null Pointer exception in Swap.
            return(shortest.Swap(color1));
        }
Beispiel #4
0
 /**
   * Unit tests the {@code ClosestPair} data type.
   * Reads in an integer {@code n} and {@code n} points (specified by
   * their <em>x</em>- and <em>y</em>-coordinates) from standard input;
   * computes a closest pair of points; and prints the pair to standard
   * output.
   *
   * @param args the command-line arguments
   */
  public static void main(String[] args) {
      int n = StdIn.readInt();
      Point2D[] points = new Point2D[n];
      for (int i = 0; i < n; i++) {
          double x = StdIn.readDouble();
          double y = StdIn.readDouble();
          points[i] = new Point2D(x, y);
      }
      ClosestPair closest = new ClosestPair(points);
      StdOut.println(closest.distance() + " from " + closest.either() + " to " + closest.other());
  }
Beispiel #5
0
        private ClosestPair FindClosestPair(Point[] points, int right, int left, int v3)
        {
            if (right - left <= 3)
            {
                AlgorithmN2();
            }
            int         mid = left + (right - left) / 2; //중앙점
            ClosestPair CPL = FindClosestPair(points, left, mid);
            ClosestPair CPR = FindClosestPair(points, mid + 1, right);
            double      d   = Math.Min(CPL.dist, CPR.dist);
            ClosestPair CPC = FindMidRange(points, d);

            return(MinCP(CPL, CPR, CPC));
        }
        /// <summary>
        /// Approximates the closest distance between every cluster and every other cluster.
        ///
        /// If there are currently K clusters, this will return at most K(K-1)/2 ClosestPairs, unsorted.
        /// If an upper limit on the square distance is supplied, fewer may be returned.
        /// </summary>
        /// <param name="maxSquareDistance">If omitted, no restriction on distance is applied.
        /// If supplied, no measurement of the closest distance between two colors will
        /// be returned if those two colors are farther apart than this distance.
        /// </param>
        /// <returns>ClosestPairs for every pair of colors, unsorted.
        /// If a distance is returned for colors "A" and "B", one will not be returned for colors "B" and "A",
        /// since the distance is symmetric.</returns>
        public IEnumerable <ClosestPair> FindAllClustersApproximately(long maxSquareDistance = long.MaxValue)
        {
            var colors = Clusters.ClassLabels().ToArray();

            for (var i = 0; i < colors.Length; i++)
            {
                for (var j = i + 1; j < colors.Length; j++)
                {
                    var closest  = NearestPointFinder.FindNearestWithLabel(colors[i], colors[j]);
                    var shortest = new ClosestPair(closest.SearchLabel, closest.SearchPoint, closest.NearLabel, closest.NearPoint, closest.Measure);
                    yield return(shortest.Swap(colors[i]));
                }
            }
        }
    /**/ public static void main(string[] strarr)
    {
        int num = StdIn.readInt();

        Point2D[] array = new Point2D[num];
        for (int i = 0; i < num; i++)
        {
            double d  = StdIn.readDouble();
            double d2 = StdIn.readDouble();
            array[i] = new Point2D(d, d2);
        }
        ClosestPair closestPair = new ClosestPair(array);

        StdOut.println(new StringBuilder().append(closestPair.distance()).append(" from ").append(closestPair.either()).append(" to ").append(closestPair.other()).toString());
    }
        /// <summary>
        /// Less efficient way to do the same thing as FindClusterApproximately.
        /// </summary>
        /// <param name="color1">Cluster id for the first cluster to compare.</param>
        /// <returns>Results that identify which other cluster is closest to the given cluster.</returns>
        public ClosestPair FindClusterIteratively(TLabel color1)
        {
            var shortestPair = new ClosestPair();

            foreach (var color2 in Clusters.ClassLabels().Where(c => !c.Equals(color1)))
            {
                var closestForColor = FindPairApproximately(color1, color2);
                if (shortestPair.SquareDistance > closestForColor.SquareDistance)
                {
                    shortestPair = closestForColor;
                }
            }

            return(shortestPair);
        }
    private double closest(Point2D[] array, Point2D[] array2, Point2D[] array3, int num, int num2)
    {
        if (num2 <= num)
        {
            return(double.PositiveInfinity);
        }
        int     num3    = num + (num2 - num) / 2;
        Point2D point2D = array[num3];
        double  a       = this.closest(array, array2, array3, num, num3);
        double  b       = this.closest(array, array2, array3, num3 + 1, num2);
        double  num4    = java.lang.Math.min(a, b);

        ClosestPair.merge(array2, array3, num, num3, num2);
        int num5 = 0;

        for (int i = num; i <= num2; i++)
        {
            if (java.lang.Math.abs(array2[i].x() - point2D.x()) < num4)
            {
                int arg_86_1 = num5;
                num5++;
                array3[arg_86_1] = array2[i];
            }
        }
        for (int i = 0; i < num5; i++)
        {
            int num6 = i + 1;
            while (num6 < num5 && array3[num6].y() - array3[i].y() < num4)
            {
                double num7 = array3[i].distanceTo(array3[num6]);
                if (num7 < num4)
                {
                    num4 = num7;
                    if (num7 < this.bestDistance)
                    {
                        this.bestDistance = num4;
                        this.best1        = array3[i];
                        this.best2        = array3[num6];
                    }
                }
                num6++;
            }
        }
        return(num4);
    }
    private static void merge(IComparable[] array, IComparable[] array2, int num, int num2, int num3)
    {
        int i;

        for (i = num; i <= num3; i++)
        {
            array2[i] = array[i];
        }
        i = num;
        int num4 = num2 + 1;

        for (int j = num; j <= num3; j++)
        {
            if (i > num2)
            {
                int arg_30_1 = j;
                int arg_2F_1 = num4;
                num4++;
                array[arg_30_1] = array2[arg_2F_1];
            }
            else if (num4 > num3)
            {
                int arg_41_1 = j;
                int arg_40_1 = i;
                i++;
                array[arg_41_1] = array2[arg_40_1];
            }
            else if (ClosestPair.less(array2[num4], array2[i]))
            {
                int arg_5A_1 = j;
                int arg_59_1 = num4;
                num4++;
                array[arg_5A_1] = array2[arg_59_1];
            }
            else
            {
                int arg_66_1 = j;
                int arg_65_1 = i;
                i++;
                array[arg_66_1] = array2[arg_65_1];
            }
        }
    }
Beispiel #11
0
        private void btnFind_Click(object sender, RoutedEventArgs e)
        {
            //double min = double.MaxValue;
            //for (int i = 0; i < p; i++)

            //    for (int j = i + 1; j < p; j++)
            //    {
            //        double d = Dist(i, j);
            //        //d가 min보다 작으면
            //        //d, i, j를 저장
            //        if(d < min)
            //        {
            //            d = min;

            //        }
            //    }
            //points[] 배열에 있는 점들을
            //x좌표를 기준으로 정렬하여 출력하시오
            //int[] a = new int[100];
            //Random r = new Random();
            //for (int i = 0; i < 100; i++)
            //    a[i] = r.Next(1000);

            //foreach (var v in a)
            //    Console.WriteLine(v);
            //Console.WriteLine("...After Sort");
            //Array.Sort(a);
            //foreach (var v in a)
            //    Console.WriteLine(v);

            IComparer xComp = new XComparer();

            Array.Sort(points, xComp);
            //Array.Sort(points, new YComparer());
            PrintPoint();

            ClosestPair CP = FindClosestPair(points, 0, 100, -1);
        }
        /// <summary>
        /// Finds approximately the cluster nearest to the given cluster.
        /// </summary>
        /// <param name="color1">Identifies the cluster being queried.</param>
        /// <returns>The closest cluster and the points from each cluster that were closest.</returns>
        public ClosestPair FindClusterApproximately(TLabel color1)
        {
            if (EqualityComparer <TLabel> .Default.Equals(color1, default(TLabel)))
            {
                throw new ArgumentNullException(nameof(color1));
            }
            // A contiguous segment of points that are not color1 is sandwiched between
            // two points that are color1, except possibly at the beginning and end of the list of SortedPoints.
            // If multiple points from the same second color are found in the segment,
            // only compare the nearest point in sequence to each end of the segment.
            // For example, if the sequence of colors (1,2,3) for points A thru J is this:
            //     Points:  A B C D E F G H I J
            //     Colors:  1 2 2 2 3 3 3 2 2 1
            // We will compare the distances between A & B, A & E, G & J, and I & J,
            // because they are likeliest to be the closest points to one or the other endpoints (A & J).
            // Thus at most one point of each color found in the segment will be compared to the
            // first point of color1, and at most one point of each color will be compared to the last
            // point of color1.
            var closest  = this.NearestPointFinder.FindNearest(color1);
            var shortest = new ClosestPair(closest.SearchLabel, closest.SearchPoint, closest.NearLabel, closest.NearPoint, closest.Measure);

            return(shortest.Swap(color1));
        }
Beispiel #13
0
        private void BtnFind_Click(object sender, RoutedEventArgs e)
        {
            //// 초기최솟값
            //double min = double.MaxValue;

            //for (int i = 0; i < P; i++)
            //{
            //    for (int j = i + 1; j < P; j++)
            //    {
            //        Point a = points[i];
            //        Point b = points[j];

            //        //점 사이의 거리
            //        double d = Dist(a, b);

            //        // d가 min 보다 작을 때
            //        if (d < min)
            //        {
            //            // d, i, j를 저장
            //            min = d;

            //            myline.X1 = points[i].X;
            //            myline.Y1 = points[i].Y;
            //            myline.X2 = points[j].X;
            //            myline.Y2 = points[j].Y;
            //        }
            //    }

            //    }


            //myline.Stroke = Brushes.Red;
            //canvas1.Children.Add(myline);
            //MessageBox.Show("최솟값은 " + min);

            //Points[] 배열에 있는 점들을 x좌표 기준으로 정렬하여 출력하시오
            //Array.sort() Test
            //int[] a = new int[100];
            //Random r = new Random();
            //for(int i = 0; i < 100; i++)
            //{
            //    a[i] = r.Next(1000);
            //}

            //foreach (var v in a)
            //{
            //    Console.WriteLine(v);
            //}
            //Console.WriteLine("...After sort...");
            //Array.Sort(a);
            //foreach (var v in a)
            //{
            //    Console.WriteLine(v);
            //}
            IComparer xComp = new XComparer();

            Array.Sort(points, xComp);
            PrintPoints();
            //Array.Sort(points, new YComparer());
            //PrintPoints();
            ClosestPair CP = FindClosestPair(points, 0, 100 - 1);
        }