2.2.5 Distance to the nearest point

xiaoxiao2021-03-06  82

A given N points (xi, yi) (1 ≤ i ≤ N), requires the two points in which the distance is nearest. Example 14-7 Assume that in a piece of metal, the n, the same hole, if the cave is too close, the metal may be broken. If you know the minimum distance of any two holes, the probability of metal break can be estimated. This minimum distance problem is actually the nearest point. By checking all N (N- 1) / 2 pairs, and calculate the distance of each pair point, you can find a nearest point of the distance. The time required for this method is (N2). We call this method as a direct method. The pseudo code for solving the solving algorithm for separation and rules is given in Fig. 1 4 - 1 3. This algorithm is solved by a direct method for small issues, and for large problems, it first divides it into two smaller problems, one of the problems (called a) is "n / 2ù, another problem (said The size of B) is "N / 2ù. Initial, the most recent point may belong to one of the three situations: 1) Two points are in A (ie, the nearest point is falling in A); 2) Two Click in B; 3) Little in A, a little bit in B. Assume that the nearest point is determined according to these three cases, then the nearest point is a pair of points in all three sites. In the first case The A can be recreated in the second case, and in the second case, the B is recursively solved. IF (N smaller) {Learning the close point to R et}} // n to be divided into roughly equalization Two parts A and B determine that the nearest point pair in A and B is determined in A, the close point of the other in B, in the three-pair point obtained from the above, to find a pair of points of the minimum distance 14 -13 Search for the most recent point to determine the nearest point pair in the third case, there is a need to adopt a different method. This method depends on how the point set is divided into A, B. A reasonable division method is From the XI (intermediate value), a vertical line is disposed, the point on the left side belongs to A, the point on the right side belongs to B. Points located on the vertical line can be allocated between A and B to meet the size of A, B. 2-8 Investigating 1 4 points from A to N in Figure 14-14A. These points are drawn in Figures 14-14B. Middle point = 1, vertical line x = 1 as shown in Figure 14-14B Show. Points on the left side of the dashed line (such as B, C, H, N, I) belong to a point (such as A, E, F, J, K, L) belong to B.D, G, and m fall in the vertical line On, two of them can be added to A and the other to join B, so that the same points are included in A, B. Suppose D, M joins A, g Joining B. Set is the nearest point of the nearest point of I of i A pair of points in the distance. If the nearest point contrast in the third case is small. The distance from the vertical line must be less than, so that you can eliminate those points ≥ ≥ ≥ from the vertical line. Figure 1 4 - The dashed line in 1 5 is the division line. The shadow portion is divided into a midline, width is 2. The boundary line and the points other than it are eliminated, only the points in the shadow are retained, in order to determine if there is a third category For (corresponding to the third case), its distance is less than. Use RA, RB to represent the remaining points in A and B. If there is a point pair (p, q), p? A, q? B, P, Q The distance is less than, then P? Ra, q? Rb. You can find such a point correctly by checking the RA. Suppose the P point in the RA, the Y coordinate of the P is PY, then only check RB satisfaction PY-

Example 2-9 Investigating 1 4 points in Example 2 - 8. The nearest point in A is (B, H), which is about 0. 3 1 6. The nearest point (f, j) in B is (f, j), which is 0. 3, thus = 0. 3. When there is a third category, the points other than D, G, I, L, M were eliminated because they were ≥ ≥ from the divided line x = 1. Ra = {D, I, M}, RB = {G, L}, because there is no point in the comparison zone of D and M, just examine I. I contain only point L in the comparison zone. The distance between I and L is calculated, and it is found that it is less than, and therefore, (i, l) is the nearest point. In order to determine a third category of a smaller distance, each point in RA is only compared with 6 points in RB, as shown in Figure 1 4 - 1 6. 1. Select the data structure In order to implement the division of Figure 1 4 - 1 3, it is necessary to determine what is "small problem" and how to expire. Since there is less than two points in the collection, there is no recent point pair, so it is necessary to ensure that the decomposition process does not produce less than two points. If you use less than four points to be "small problems", you can avoid less than two points. There are three parameters per point: label, X coordinate, Y coordinate. Assume that the label is an integer, each point can be represented by the P O I n TI (see program 1 4 - 8). In order to facilitate the sorting of each point according to the X coordinate, the operator <=. Mental sorting procedures are shown in 1 4 -3. Program 14-8 Class 4 Class Point1 {Friend Float Dist (Const Point1 &, Const Point1 &); Friend Void Close (Point1 *, Point2 *, Point2 *, Int, Int, Point1 &, Point1 &, Float &); Friend Bool Closest (Point1 * , INT, POINT1 &, POINT1 &, FLOAT &; Friend Void Main (); Public: Int Operator <= (Point1 A) const {return (x <= ax);} private: int ID; // point number float x, Y; // point coordinate}; Class Point2 {Friend Float Dist (Const Point2 &, Const Point2 &); Friend Void Close (Point1 *, Point2 *, Point2 *, Int, Int, Point1 &, Point1 &, Float &); Friend Bool Closest Point1 *, Int, Point1 &, Point1 &, Float &, Friend Void Main (); Public: Int Operator <= (Point2 a) const {return (y <= aY);} private: int P; // array x Click index float x, y; // point coordinate}; input N points can be represented by array x. Assuming that the points in X have been sorted according to the X coordinate, if the current inspection during the segmentation process is X [L: R], then the point in M ​​= (L R) / 2, X [L: M] is first calculated. It belongs to A, the remaining point belongs to B. After calculating the nearest point pairs in A and B, RA and RB are also needed to determine if there is a closer point pair, where a little is RA, and the other is RB. If the point has been sorted by Y coordinate, you can use a very simple way to test Figure 1 4 - 1 6.

Press the Y coordinate sorting point to save in another array using class P O I N T 2 (see program 14-8). Note that in the P O i N T 2 class, the operator <= is overloaded for easy Y coordinate. Member P is used to point to the corresponding point in X. After determining the necessary data structure, then take a look at the code you want. First define a template function D I S (see program 1 4-9) to calculate the distance between points A, B. T may be P O i N N N or P O I N T 2, so D i s T must be a friend of P O i N T 1 and P O i N T 2 classes. Program 14-9 Calculate two-point distance Templateinline Float Dist (Const T & U, Const T & V)

{/ / Calculation point u and v

FLOAT DX = U.x-V. x;

Float Dy = u.y-v. y;

Return SQRT (DX * DX DY * DY);

}

If the number of points is less than two, the function C L O S e s T (see program 1 4 - 1 0) returns F A L S E, and the function returns T R u e when the function is successful. When the function is successful, the nearest point nearest is returned in parameters a and b, and the distance is returned in the parameter d. The code first verifies at least two points, then use the M e RG E S O R T function (see programs 14-3) to sort the points in X in X. Next, copy these points into the array y and sort it according to Y coordinates. When the sort is completed, the position of any I, y [i]. Y≤y [i 1]. Y, and y [i] .p gives the position in X in X. After the above preparation work, the function close is called (see programs 1 4 - 11), which actually solves the nearest point.

Program 14-10 Preprocessing and Call C L O S

Bool Closest (Point1 X [], Int N, Point1 & A, Point1 & B, Float & D)

{// Looking for the closest point in n> = 2 points

// If less than 2 points, return F a L S e

/ / Otherwise, returning the nearest point in A and B

IF (n <2) Return False;

/ / Sort by X coordinate

M E R G E S O R T (X, N);

// Create a point group in the y coordinate

Point2 * y = new point2 [n];

For (int i = 0; i

// Copy point i from x to Y

Y [i] .p = i;

Y [i] .x = x [i] .x;

Y [i] .y = x [i] .y;

}

M E R g e s o R t (y, n); // Sort Y coordinate

// Create a temporary array

Point2 * z = new point2 [n];

// Looking for the closest point

C l O S e (x, y, z, 0, n - 1, a, b, d);

/ / Delete an array and return

DELETE [] Y;

Delete [] z;

Return True;

}

Program 1 4 - 11 Calculates the nearest point

Void Close (Point1 x [], Point2 Y [], Point2Z [], Int L, Int r, Point1 & A, Point1 & B, Float & D)

{// x [l: r] Sort by X coordinate

// y [l: r] Sort Y coordinate

IF (r-l == 1) {// two points

A = x [l];

B = x [r];

D = dist (x [l], x [r]);

R e t u R n;}

IF (r-L == 2) {// three points

/ / Calculate the distance between all points

FLOAT D1 = Dist (x [l], x [l 1]);

FLOAT D2 = Dist (x [l 1], x [r]);

Float D3 = dist (x [l], x [r]);

// Looking for the closest point

IF (D1 <= D2 && D1 <= D3) {

A = x [l];

B = x [l 1];

D = D1;

R e t u R n;}

IF (D2 <= D3) {a = x [l 1];

B = x [r];

D = D2;}

Else {a = x [l];

B = x [r];

D = D3;}

R e t u R n;}

/ / More than three points, divided into two parts

INT M = (L R) / 2; // x [l: m] In A, the remaining in B

// Create a table in Z [L: M] and Z [M 1: R]

INT f = l, // z [l: m] cursor

G = m 1; // z [m 1: r] cursor

For (int i = L; i <= r; i )

IF (y [i] .p> m) z [g ] = y [i];

ELSE Z [f ] = y [i];

/ / Solve the above two parts

C L o S e (X, Z, Y, L, M, A, B, D);

Float D;

Point1 AR, BR;

C L O S E (X, Z, Y, M 1, R, A R, B R, D R);

// (a, b) is a more close point in both

IF (DR

B = Br;

D = DR;}

M E R G E (Z, Y, L, M, R); // Reconstruct Y

/ / Put the point of less than D in Z

INT K = L; // Z cursor

For (i = L; i <= r; i )

IF (Fabs (Y [M] .X - Y [i] .x)

// Check all the points in Z [L: K - 1], look for closer points

For (i = L; i

For (int J = i 1; j

J ) {

FLOAT DP = Dist (z [i], z [j]);

IF (DP

D = DP;

A = x [z [i] .p];

B = x [z [j] .p];

}

}

}

Function C L O S e (see program 1 4 - 11) is used to determine the nearest point pair in X [1: R]. Suppose these points are sorted by X coordinate. These points are sorted by Y coordinate in Y [1: R]. Z [1: r] is used to store intermediate results. After the recent point is found, the nearest point pair will be returned in A, B. The distance is returned in D, and the array y is restored to the input state. The function does not modify the array X.

First investigate "small problem", that is, less than four points. Because the segmentation process does not produce less than two points, only two points and three points are required. For these two situations, you can try all the possibilities. When the number of points exceeds three, the point set is divided into two groups A and B, X [1: M] belongs to A, X [M 1: R] belongs to B by calculating M = (1 r) / 2. By scanning from left to right and determined which points belong to A, which points belong to B, can create Z [1: M] and Z [M , which are sorted by Y coordinate, ", respectively, in groups B," 1: r]. At this time, Y and Z 's roles are exchanged, and two recursive calls are sequentially executed to acquire the nearest point pairs in A and B. After returning twice twice, it is necessary to ensure that Z does not change, but this requirement is not available. However, only Y [l: r] may change. By merge operation (see program 1 4 - 5) can be reconstructed as Z [1: R]. To implement the strategy of Figure 1 4 - 1 6, first scan Y [1: r], and collect points smaller from the split line, and store these points in Z [1: K - 1]. It can be paired with all points in the comparison zone of RA in the comparison zone of the RA in two ways: 1) Point pairing with Y coordinate ≥P.y in RB; 2) Point pairing with Y coordinate ≤p.y. This can be used by each point z [i] (1 ≤ i

Still in RB) and Z [J] pair, where i

2. Complexity analysis

Let T (N) represent the time required for the function close when processing N points. When n <4, t (n) is equal to a constant d. When N ≥ 4, take the time to complete the following work: divide the point set into two parts, refactor Y, the removal of the call from the split line, looking for better third Class pair. Two recursive calls take time T ("N / 2ù" and T (? N / 2?).

This recursive is exactly the same as that of the merged sort, and the result is T (N) = (NL O GN). In addition, the function C L O S e S T requires time (NL O GN) to accomplish additional operation: sorting X, creates Y and Z, sort Y. Therefore, the most recent point for the solving algorithm is (NL O Gn).

Reprinted from Shada Studio

转载请注明原文地址:https://www.9cbs.com/read-108020.html

New Post(0)