Andrew algorithm for two-dimensional convex hull - Study Notes

 

The convex hull concept

First, the introduction of the concept of the convex hull:

 

 

 Probably long like this:

 

 

 So, given some of the scatter, how to quickly find the convex hull of it (with a point on the convex hull to represent the convex hull)

Andrew thought processes and algorithms

Seeking common convex hull algorithm $ Graham $ and $ Andrew $, $ Andrew $ var is $ Graham $ scanning algorithm, and compared to $ Graham $, $ Andrew $ faster, more stable, so the main talk about $ Andrew $.

 

First of all points $ x $ coordinates of the first keyword, $ y $ coordinates of the second sort key from small to large, to be sure first point of the last point and answer.

Next, use the following examples to help understand the algorithmic process:

 First, the $ 1 $ and $ 2 $ join answers

 

 

Try to $ 3 $ added, found the side in, so to lose $ 2 $, $ 3 $ to go into

 

 

 

 

 Let's look at the reasons (reasons recessed) $ 2 $ is lost, $ 3 $ Host success: the
discovery is the slope (or ... can be summarized as the cross product?

FIG slope below $ 1-> 2 which are greater than $ 1- $> $ 3, and because before sorting incremented by $ X $, $ 2 $ can be described in the upper left $ 3 $, it is concave.

In other words, if added to the list at this point (i.e. the current point, referred to as a $ I $) and the slope number $ i-2 $ $ I $ point is less than a dot and the number of $ i-1 $ slope point, then they would the number $ i-1 $ I $ site $ removed and added to maintain the properties of the dot of the convex hull (not it is recessed)

 

 

 

 

 

Next was added 4 $ $, $ 1-> 4 slope is greater than $ 1- $> $ 3 slope, so $ 3 $ without being discarded.

 

 

 Add 5 $ $, $ 3-> 5 slope is less than $ 3- $> $ 4 slope, so lose $ $ 4, $ 5 $ added

 

 

 

 

 

 

 

Added $ 6 $, the same reason, the same operation. ($ 3-> 6 slope of less than $ 3- $> $ 5 slope, lose $ $ 5, $ 6 $ added)

 

 

 

 

 

 $ 3 $ and then found that the recessed places ($ 1-> 6 $ a slope of less than $ 1 -> inclination of $ 3)

So $ 3 $ should be dropped, then only two points:

(So ​​write the code to use $ while $)

 

 

 Then, the addition of $ 7 $:

 

 

 Then $ 8 $, find the slope ... just a little bit (not draw a good map, it's tricky angle, we will look at it ...), so you want to delete the $ 7 $

But also the way to solve the problem of a collinear, collinear Well, a good solution, anyway, two points on the convex hull, is not lost on it, if behind a line that does not belong to the convex hull, then use $ while $ lost two points when the point of the slope is the same, there will always be thrown out.

 

 

 

 

 

 Then $ 9 $, found a small slope, so losing $ 8 $:

 

 

 Aha, then find all the points have been traversed over, successfully reached the $ 9 $, but the convex hull of the remaining half of it.

You can find the backwards again covered with a lid that:

(Placed below flowchart, illustrates not one (tired), operation is the same )

 

 (The $ 7 $ quietly shifted his position) 

 

 

 

 (Lost $ 6 $, and found that the relationship between the slope of the positive with all of that time, are less than) 

 

 

 

 

 

 

 

 

 

 

 

 

 In this way, the convex hull calculated on coming!

According to the above ideas to write code to be all right.

Examples & board

 Example: find the circumference of the convex hull:

 1 /*
 2 ID: Starry21
 3 LANG: C++
 4 TASK: fc          
 5 */ 
 6 #include<cstdio>
 7 #include<algorithm>
 8 #include<vector>
 9 #include<cstring>
10 #include<queue>
11 #include<map>
12 #include<iostream>
13 #include<cmath>
14 using namespace std;
15 #define ll long long
16 #define INF 0x3f3f3f3f
17 #define N 10005
18 struct node{
19     double x,y;
20 };
21 node p[N],s[N]/*凸包上的点*/;
22 int n;
23 double dis(node a,node b)
24 {
25     return sqrt(((a.x-b.x)*(a.x-b.x))+((a.y-b.y)*(a.y-b.y)));
26 }
27 bool cmp(node a,node b)
28 {
29     if(a.x==b.x) return a.y<b.y;
30     return a.x<b.x;
31 }
32 doublegetk (Node A, Node B)
 33 is  {
 34 is      IF (== AX BX) return INF; // in a vertical line on the infinite slope as 
35      return (AY-by) / (BX- AX);
 36  }
 37 [  Double Andrew ()
 38 is  {
 39      Sort (P + . 1 , P + n-+ . 1 , CMP);
 40      int CNT = 0 , TOT = 0 ;
 41 is      Double SUM = 0.0 ;
 42 is      for ( int I = . 1 ; I <= n-; I ++ )
 43      {
44         s[++cnt]=p[i];
45         while(cnt>=3&&getk(s[cnt-2],s[cnt])<getk(s[cnt-2],s[cnt-1]))
46             s[cnt-1]=s[cnt],cnt--;
47     }
48     for(int i=1;i<=cnt-1;i++)
49         sum+=dis(s[i],s[i+1]);
50     tot=cnt;
51     cnt=0;
52     for(int i=n;i>=1;i--)
53     {
54         s[++cnt]=p[i];
55         while(cnt>=3&&getk(s[cnt-2],s[cnt])<getk(s[cnt-2],s[cnt-1]))
56             s[cnt-1]=s[cnt],cnt--;
57     }
58     for(int i=1;i<=cnt-1;i++)
59         sum+=dis(s[i],s[i+1]);
60     tot+=cnt;
61     tot-=2;//tot是凸包上点的个数 
62     //printf("%d\n",tot); 
63     return sum;
64 }
65 int main() 
66 {
67     //freopen("fc.in","r",stdin);
68     //freopen("fc.out","w",stdout);
69     scanf("%d",&n);
70     for(int i=1;i<=n;i++)
71         scanf("%lf %lf",&p[i].x,&p[i].y);
72     printf("%.2lf\n",Andrew());
73     return 0;
74 }
Code

 

Guess you like

Origin www.cnblogs.com/lyttt/p/11826321.html