Preface:
First, what is the convex package.
Suppose the plane has a total of 13 points, p0~p12 some points as a polygon, so that the polygon can be "wrapped" all the points. When this polygon is a convex polygon, we call it "convex package". The following figure:
Then, what is the convex package problem.
We put these points in a two-dimensional coordinate system, so each point can be represented by (X,y).
Now give the number of points 13, and the coordinates of each point. Find the point that makes up the convex package.
Solution One: Exhaustive method (brute force method)
Complexity of Time: O (n³).
Train of thought: two points to determine a straight line, if the rest of the other points are on the same side of the line, then these two points are convex on the point of the package, otherwise it is not.
Step: Match all Point 22 in the point set to form N (n-1)/2 lines. For each line, check to see if the remaining (n-2) points are on the same side of the line.
How to tell if a point P3 is on the left or right of a straight p1p2. (Coordinates: P1 (x1,y1), p2 (x2,y2), p3 (X3,Y3))
When the upper-type result is positive, the P3 is on the left side of the line p1p2, and when the result is negative, the P3 is on the right side of the line p1p2.
Solution Two: Divide the rule of law
Complexity of Time: O (n㏒n).
Thinking: Applying the idea of dividing the law, a big problem is divided into several sub problems with the same structure, and the problem of the handle is divided into several smaller problems. Then we can use the recursive method to find out the solution of these sub problems separately. Finally, the solution of each sub problem is "assembled" into the solution of the original big problem.
Step: Put all the points in the two-dimensional coordinate system. So the minimum and maximum two points of the horizontal coordinate P1 and Pn must be the point on the convex package. It is easy to prove with contradiction, it is unknown here. The line P1PN divides the point set into two parts, the X axis above and the following two parts, which are called Upper and lower packets respectively. On the package: to find the distance line P1PN farthest point, that is, the point Pmax in the figure below. As a straight line P1pmax, Pnpmax, the line to the left side of the P1pmax as a package, the line Pnpmax the right side of the point also as a package. Repeat steps 2, 3. Similar operations are done on the next package.
But how to find the furthest point from a certain line. We still use the formula in solution one:
A point P3 and a straight line p1p2 are provided. (Coordinates: P1 (x1,y1), p2 (x2,y2), p3 (X3,Y3))
The result of the upper-style is absolute, the greater the absolute value, the farther away the line.
Note: In step one, if the horizontal axis is the smallest point more than one, then these points are convex packets on the point, at this time the packet and the next packet division is a little different, need to pay attention to.
solution Three: Jarvis Step method
Complexity of Time: O (NH). (where n is the total number of points, H is the number of points on the convex package)
Train of thought: the point with the smallest ordinate must be the point on the convex package, such as the P0 on the graph. Starting from the P0, according to the direction of the counterclockwise, one after another to find the point of the convex bag, each before further found a point, so called as a stepping method. How to find the next point. Use the angle. Suppose you have now found {P0,P1,P2}, and you need to find the next point: the remaining points, respectively, and the P2 component vectors, the angle between the vector and the vector p1p2 is beta. When the beta is the smallest is the requested next point, here is P3.
Note: When looking for the second point P1, because the only point that has been found is P0, the vector can only be the angle of the horizontal line α, and the second point when alpha is minimal. Collinear situation: If the line p2p3 there is a point P4, that is, three points collinear, at this time by the vector p2p3 and vector p2p4 produced two beta is the same. We should take P3, P4 as the point on the convex package, and the point that is farthest from the P2 (that is, the P4 in the picture) as the last search point, continue to find its next connection point.
Solution Four: Graham Scanning method
Complexity of Time: O (n㏒n)
Train of thought: Graham scans the idea and the Jarris step method similar, is also first to find a convex package on a point, and then start from that point in a counterclockwise direction to find the point on the convex bag, but it is not the use of the angle.
Step: Put all the points in the two-dimensional coordinate system, then the minimum ordinate point must be convex package point, such as the P0 in the figure. Translate the coordinates of all the points to make the P0 as the origin, as shown above. Calculates each point relative to the P0 angle α, sorts each point in the order from small to large. When Alpha is at the same time, the distance from the P0 is more near the front. For example, the results obtained from the above diagram are P1,P2,P3,P4,P5,P6,P7,P8. We can know by geometry that the first point P1 and the last point in the result P8 must be the point on the convex package.
(The above is the preparation step, the following starts to ask the convex package)
Above, we already know the first point on the convex package P0 and the second point P1, we put them in the stack. Now from the result of step 3, take the point behind the P1 to do the current point , that is, P2. Then start looking for a third point: Connect the P0 and the top of the stack, and get the straight L. See if the current point is on the right or left side of the line L. If you are performing step 5 on the right side of the line, follow step 6 if you are on a line or on the left side of a line. If on the right, the top of the stack is not the point on the convex package, and the top element of the stack is out of the stack. Perform step 4. The current point is the point on the convex package, push it into the stack, and perform step 7. Check that the current point P2 is not the last element of the result of step 3. Is the last element of the sentence to end. If not, then do the P2 behind the current point, and return to step 4.
Finally, the element in the stack is the point on the convex package.
The following is the process of dynamic solution using the Graham scanning method:
Solution five: Melkman algorithm
To be honest, this algorithm I have not yet see. The information on the Internet is also very poor, I will be the explanation of the online section of the map here, later to understand and then come back to fill.
Or someone can read, hope feel free, not very grateful.
Extensions:
The above discussion is only two-dimensional convex package, if the Yensen is the three-dimensional, multidimensional convex package problem. How to solve.
But first of all, two-dimensional convex package can be used to solve the fence problem, urban planning problems, clustering analysis and so on. But what are the possible areas of use for three-dimensional, multidimensional convex packages?
Appendix: Fast Package algorithm code (C language):
#include <stdio.h> #include <stdlib.h> int g_result[240][2];
/*getresult () Implementation function: in the coordinate P0 (x1,y1) and PN (X2,y2) as a straight line, find the line in the pack the farthest point Pmax * and find the line P0pmax and PMAXPN on the packet, for recursion.
* Note: pack[0][0] The number of storage points, pack[1] start the location of the coordinates. * global variable g_result[][] is used to store the point on the convex package, which is the final answer.
The same g_result[0][0] stores the number of points that have been found.
**/void GetResult (int pack[240][2], int x1, int y1, int x2, int y2) {int i,t,x3,y3,r,rmax,tmax;
int resultpack[240][2];
Resultpack[0][0] = 0;
if (Pack[0][0] <= 1) return;
x3 = pack[1][0];
Y3 = pack[1][1];
R = x1*y2 + x3*y1 + x2*y3-x3*y2-x2*y1-x1*y3;
Rmax = R;
Tmax = 1;
for (i=2;i<=pack[0][0];i++) {x3 = pack[i][0];
Y3 = pack[i][1];
R = x1*y2 + x3*y1 + x2*y3-x3*y2-x2*y1-x1*y3;
if (R >= 0) {t = ++resultpack[0][0];
Resultpack[t][0] = x3;
RESULTPACK[T][1] = y3;
} if (R > Rmax) {rmax = R;
Tmax = i; } if (Rmax <= 0) {for (i=1;i<resultpack[0][0];i++) {x3 = resultpack[i][0];
Y3 = resultpack[i][1];
R = x1*y2 + x3*y1 + x2*y3-x3*y2-x2*y1-x1*y3; if (R = = 0 &&! ( (x3==x2&&y3==y2) | |
(x3==x1&&y3==y1)))
{t = ++g_result[0][0];
G_result[t][0] = resultpack[i][0];
G_RESULT[T][1] = resultpack[i][1];
} return;
else {t = ++g_result[0][0];
G_result[t][0] = pack[tmax][0];
G_RESULT[T][1] = pack[tmax][1];
if (resultpack[0][0] = = 0) return;
} getresult (resultpack,x1,y1,pack[tmax][0],pack[tmax][1]);
GetResult (RESULTPACK,PACK[TMAX][0],PACK[TMAX][1],X2,Y2);
void Main () {int point[240][2];//point save all points.
int i=1;
int x1,y1,x2,y2,x3,y3; g_result[0][0]=0; The first row of Point[0][0]=0;//point the first column contains a few points inside the package.
Initialized to 0.
printf ("Please enter a bit of coordinates: \ n"); while (scanf ("%d,%d ", &point[i][0],&point[i][1])!= EOF) i++;
Point[0][0] = i-1;
x1 = point[1][0];
y1 = point[1][1];
x2 = x1;
y2 = y1;
for (i=2;i<=point[0][0];i++) {x3 = point[i][0];
Y3 = point[i][1];
if (x3 < x1) {x1 = x3;
y1 = y3;
else if (x3 > x2) {x2 = x3;
y2 = y3;
}} g_result[1][0] = x1;
G_result[1][1] = y1;
G_result[2][0] = x2;
G_result[2][1] = y2;
G_result[0][0] + + 2;
GetResult (Point, x1, y1, x2, y2);
GetResult (Point, X2, y2, x1, y1);
printf ("\ n \ nthe point in which the convex package is formed: \ n");
for (i=1;i<=g_result[0][0];i++) printf ("(%d,%d) \ n", g_result[i][0],g_result[i][1]);
System ("pause"); }
Reprint Please indicate the source, thank you. (Original link: http://blog.csdn.net/bone_ace/article/details/46239187)