Catalogue
1 Problem Description
2 Solutions
2.1 Brute Force method
1 problem description
Given a set of n points on a plane , its convex hull is the smallest convex polygon containing all of these points, and all points satisfying this condition are obtained.
In addition, vivid description of the image:
(1) We can think of this problem as how to use the shortest fence to surround an n-headed, sleeping Tiger.
(2) This can also be seen: Please think of the points discussed as nails on the plywood, plywood on behalf of the plane. Open up a rubber band, surround all the nails, and release the hand. A convex hull is an area bounded by a rubber band. The specific schematic as shown in 1 :
Figure 1 using a rubber band to explain the convex hull
2 Solutions
2.1 Brute Force method
The use of brute force method to solve this problem is relatively simple, the specific idea: for a n -point set of two points p1 and p2, When and only if other points in the collection are on the same side of the line that crosses these two points, their connection is part of the convex hull boundary of the collection, in short,p1 and p2 Is the vertex of the smallest convex polygon in convex hull problem. Once the test is done for each pair of points, the segment that satisfies the condition forms the boundary of the convex hull.
At this point, according to the above formula, we only need to put each point into the formula Ax+by-c, to determine whether the formula calculates the result of the symbol is all greater than or equal to 0 or less than or equal to 0, If it is the point on the convex hull boundary, it is not. The time efficiency of the algorithm is 0(n^3). The specific code is as follows:
PackageCom.liuzhen.chapterThree; Public classConvexhull {//The brute force method solves convex hull problem, the point collection of convex polygon in the return point set Public Staticpoint[] Getconvexpoint (point[] A) {point[] result=NewPoint[a.length]; intlen = 0;//used to calculate the number of midpoints in the final return result for(inti = 0;i < a.length;i++){ for(intj = 0;j < a.length;j++){ if(j = = i)//Remove the first point selected as the OK line Continue; int[] Judge =New int[A.length];//The result of the judgment formula used to store the point to the straight line distance for(intK = 0;k < a.length;k++){ intA = A[j].gety ()-a[i].gety (); intb = A[i].getx ()-A[j].getx (); intc = (A[i].getx ()) * (A[j].gety ())-(A[i].gety ()) *(A[j].getx ()); JUDGE[K]= A * (A[k].getx ()) + b* (a[k].gety ())-C;//calculate the results of a specific judgment based on a formula } if(Judgearray (judge)) {//If the point is on one side of the line, the corresponding a[i] is the point in the convex hullresult[len++] =A[i]; Break; }}} point[] Result1=NewPoint[len]; for(intm = 0;m < len;m++) Result1[m]=Result[m]; returnRESULT1; } //determines whether the elements in the array are all greater than or equal to 0 or less than or equal to 0, if yes returns True, otherwise false Public Static BooleanJudgearray (int[] Array) { BooleanJudge =false; intLen1 = 0, len2 = 0; for(inti = 0;i < array.length;i++){ if(Array[i] >= 0) Len1++; } for(intj = 0;j < array.length;j++){ if(Array[j] <= 0) Len2++; } if(len1 = = Array.Length | | len2 = =array.length) Judge=true; returnjudge; } Public Static voidMain (string[] args) {point[] A=NewPoint[8]; a[0] =NewPoint (1,0); a[1] =NewPoint (0,1); a[2] =NewPoint (0,-1); a[3] =NewPoint ( -1,0); a[4] =NewPoint (2,0); a[5] =NewPoint (0,2); a[6] =NewPoint (0,-2); a[7] =NewPoint ( -2,0); Point[] Result=Getconvexpoint (A); System.out.println ("The set of points in collection A that satisfies the convex hull is:"); for(inti = 0;i < result.length;i++) System.out.println ("(" +result[i].getx () + "," +result[i].gety () + ")"); }}
The point class code defined above is as follows:
PackageCom.liuzhen.chapterThree; Public classPoint {Private intx; Private inty; Point () {x= 0; Y= 0; } Point (intXinty) { This. x =x; This. y =y; } Public voidSetX (intx) { This. x =x; } Public intGetX () {returnx; } Public voidSety (inty) { This. y =y; } Public intGetY () {returny; }}
Operation Result:
The set of points in set a that satisfies the convex hull is: (2,0) (0,2)(0,-2) (-2,0)
Algorithm Note _016: Convex hull problem (Java)