Nearest point pair (Java implementation)

Source: Internet
Author: User
Tags pow


Recent point to question:

In n points on a two-dimensional plane, the distance of the nearest pair of points is fast.

Java implementations:

Package P2;import static Java.lang.math.*;import Java.util.arraylist;import Java.util.arrays;import Java.util.collections;import Java.util.comparator;import Java.util.scanner;public class CloestPair {//coordinate point static Class Point {public ' point () {}public point (Double x, double y) {this.x = X;this.y = y;} Double x;double Y;} public static void Main (string[] args) {Scanner Scanner = new Scanner (system.in), while (true) {int n = scanner.nextint ();P Oint[] ps = new Point[n];for (int i = 0; i < n; i++) {Double x = scanner.nextdouble ();d ouble y = scanner.nextdouble ();p S[i] = new Point (x, y);}  Sort by x-coordinate ascending arrays.sort (PS, New comparator<point> () {@Overridepublic int compare (point O1, point O2) {if (o1.x < o2.x) return-1;if (o1.x > o2.x) return 1;if (O1.y < O2.Y) return-1;if (O1.y > O2.y) return 1;return 0;}}); Double Mindis = mindistance (PS, 0, n-1); System.out.println (Mindis);}} /** * Minimum distance between point pairs * * @param PS * @param l * @param r * @return */public static double mindistance (point[]PS, int l, int r) {/** * The same point, there is no point pair, distance cannot take 0, return maximum value */if (L = = r) {return double.max_value;} if (l + 1 = = r) {return distance (Ps[l], ps[r]);} int center = l + (r-l)/2;double dis1 = Mindistance (PS, L, center);d ouble dis2 = mindistance (PS, center + 1, R);d ouble Mindis = min (dis1, DIS2);  arraylist<point> nearpoints = new arraylist<> ();//Select a point in the middle line less than Mindis for (points p:ps) {if (ABS (ps[center].x- p.x) <= Mindis) {nearpoints.add (P);}} Sort by y-axis ascending collections.sort (nearpoints, New comparator<point> () {@Overridepublic int compare (point O1, point O2) { if (O1.y < O2.Y) return-1;if (O1.y > O2.y) return 1;if (o1.x < o2.x) return-1;if (o1.x > o2.x) return 1;return 0 ;}}); for (int i = 0; i < nearpoints.size (), i++) {for (int j = i + 1, J < Nearpoints.size (); j + +) {if (Nearpoints.get (j). Y-nearpoints.get (i). Y > Mindis) {break;//element y+1 away from element I, no need to continue comparing}double D = Distance (Nearpoints.get (j), Nearpoints.get (i)); if (d < mindis) {Mindis = D;}}} return Mindis;} Public STATic double distance (point P1, point p2) {if (P1 = = p2) return 0;return sqrt (POW (p1.x-p2.x, 2) + POW (P1.Y-P2.Y, 2));}} 


Nearest point pair (Java implementation)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.