暑假集训-计算几何

晚上别打游戏了。。

交了两天的半平面交的模版题终于过了,,,原来是 OnLeft 里面少抄了一个等于号......

半平面交 poj3335

暑假集训-计算几何
  1 #include <iostream>

  2 #include <cstdio>

  3 #include <fstream>

  4 #include <algorithm>

  5 #include <cmath>

  6 #include <deque>

  7 #include <Vector>

  8 #include <queue>

  9 #include <string>

 10 #include <cstring>

 11 #include <map>

 12 #include <stack>

 13 #include <set>

 14 #define LL long long

 15 #define INF 0x3f3f3f3f

 16 #define eps 1e-10

 17 #define OPEN_FILE

 18 using namespace std;

 19 int n;

 20 

 21 int dcmp(double x){

 22     if (fabs(x) < eps) return 0;

 23     return x < 0 ? -1 : 1;

 24 }

 25 struct Point{

 26     double x, y;

 27     Point(double p = 0, double q = 0){

 28         x = p;

 29         y = q;

 30     }

 31 };

 32 struct Node{

 33     int p;

 34     Point A, B;

 35     Node(Point a1, Point a2, int t){

 36         A = a1;

 37         B = a2;

 38         p = t;

 39     }

 40 };

 41 typedef Point Vector;

 42 

 43 Vector operator + (Vector A, Vector B){

 44     return Vector(A.x + B.x, A.y + B.y);

 45 }

 46 Vector operator - (Vector A, Vector B){

 47     return Vector(A.x - B.x, A.y - B.y);

 48 }

 49 Vector operator * (Vector A, double p){

 50     return Vector(A.x * p, A.y * p);

 51 }

 52 Vector operator / (Vector A, double p){

 53     return Vector(A.x / p, A.y / p);

 54 }

 55 bool operator == (Vector A, Vector B){

 56     return dcmp(A.x - B.x) == 0 && dcmp(A.y - B.y) == 0;

 57 }

 58 bool operator > (Vector A, Vector B){

 59     return A.x > B.x && A.y > B.y;

 60 }

 61 bool operator <(Vector A, Vector B){

 62     return A.x < B.x && A.y < B.y;

 63 }

 64 //点积

 65 double  Dot(Vector A, Vector B){

 66     return A.x * B.x + A.y * B.y;

 67 }

 68 //

 69 double Length(Vector A){

 70     return sqrt(Dot(A, A));

 71 }

 72 //夹角

 73 double Angle(Vector A, Vector B){

 74     return acos(Dot(A, B) / Length(A) / Length(B));

 75 }

 76 //叉积

 77 double Cross(Vector A, Vector B){

 78     return A.x * B.y - A.y*B.x;

 79 }

 80 //三角形面积

 81 double Area2(Point A, Point B, Point C){

 82     return Cross(B - A, C - A);

 83 }

 84 struct Line

 85 {

 86     Point p;

 87     Vector v;

 88     double angle;

 89     Line(){}

 90     Line(Point p, Vector v) :p(p), v(v){

 91         angle = atan2(v.y, v.x);

 92     }

 93     bool operator < (const Line & L) const{

 94         return angle<L.angle;

 95     }

 96 };

 97 //点p在直线L的左边

 98 bool OnLeft(Line L, Point p) { return Cross(L.v, p - L.p)>=0; }

 99 //直线A,B的交点

100 Point GetIntersection(Line A, Line B)

101 {

102     Vector u = A.p - B.p;

103     double t = Cross(B.v, u) / Cross(A.v, B.v);

104     return A.p + A.v*t;

105 }

106 

107 int HalfplaneIntersection(Line *L, int n, Point *poly)

108 {

109     sort(L, L + n);

110 

111     int first, last;

112     Point *p = new Point[n];

113     Line *q = new Line[n];

114     q[first = last = 0] = L[0];

115     for (int i = 1; i < n; i++)

116     {

117         while (first < last && !OnLeft(L[i], p[last - 1])) last--;

118         while (first < last && !OnLeft(L[i], p[first])) first++;

119         q[++last] = L[i];

120         if (fabs(Cross(q[last].v, q[last - 1].v)) < eps)

121         {

122             last--;

123             if (OnLeft(q[last], L[i].p)) q[last] = L[i];

124         }

125         if (first < last) p[last - 1] = GetIntersection(q[last - 1], q[last]);

126     }

127     while (first < last && !OnLeft(q[first], p[last - 1])) last--;

128     if (last - first <= 1) return 0;

129     p[last] = GetIntersection(q[last], q[first]);

130     int m = 0;

131     for (int i = first; i <= last; i++) poly[m++] = p[i];

132     return m;

133 }

134 

135 Line L[105];

136 Point P[105], poly[105];

137 Vector v[105];

138 int main()

139 {

140 #ifdef OPEN_FILE

141     //freopen("in.txt", "r", stdin);

142     //freopen("out.txt", "w", stdout);

143 #endif // OPEN_FILE

144     int T;

145     scanf("%d", &T);

146     double x, y;

147     for (int cas = 1; cas <= T; cas++){

148         scanf("%d", &n);

149         for (int i = 0; i < n; i++)

150         {

151             scanf("%lf%lf", &x, &y);

152             P[i] = Point(x, y);

153         }

154         for (int i = 0; i < n; i++){

155             v[i] = P[i] - P[(i + 1) % n];

156         }

157         for (int i = 0; i < n; i++)

158         {

159             L[i] = Line(P[i], v[i]);

160         }

161         if (HalfplaneIntersection(L, n, poly) != 0){

162             printf("YES\n");

163         }

164         else{

165             printf("NO\n");

166         }

167     }

168 }
View Code

 

凸包加旋转卡壳 poj2187

暑假集训-计算几何
  1 #include <iostream>

  2 #include <cstdio>

  3 #include <fstream>

  4 #include <algorithm>

  5 #include <cmath>

  6 #include <deque>

  7 #include <vector>

  8 #include <queue>

  9 #include <string>

 10 #include <cstring>

 11 #include <map>

 12 #include <stack>

 13 #include <set>

 14 #define LL long long

 15 #define INF 0x3f3f3f3f

 16 #define OPEN_FILE

 17 using namespace std;

 18 

 19 #define eps 1e-8

 20 

 21 struct Point{

 22     int x, y;

 23     Point(int p = 0, int q = 0){

 24         x = p;

 25         y = q;

 26     }

 27 };

 28 typedef Point Vector;

 29 

 30 Vector operator + (Vector A, Vector B){

 31     return Vector(A.x + B.x, A.y + B.y);

 32 }

 33 Vector operator - (Vector A, Vector B){

 34     return Vector(A.x - B.x, A.y - B.y);

 35 }

 36 Vector operator * (Vector A, int p){

 37     return Vector(A.x * p, A.y * p);

 38 }

 39 Vector operator / (Vector A, int p){

 40     return Vector(A.x / p, A.y / p);

 41 }

 42 bool operator == (Vector A, Vector B){

 43     return A.x - B.x == 0 && A.y - B.y == 0;

 44 }

 45 bool operator > (Vector A, Vector B){

 46     return A.x > B.x ||(A.x == B.x && A.y > B.y);

 47 }

 48 bool operator <(Vector A, Vector B){

 49     return A.x < B.x ||(A.x == B.x && A.y < B.y);

 50 }

 51 

 52 int Cross(Vector A, Vector B){

 53     return A.x * B.y - A.y*B.x;

 54 }

 55 int Distance(Point a, Point b){

 56     return  (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);

 57 }

 58 int ConvexHull(Point *p, int n, Point* ch){

 59     sort(p, p + n);

 60     int m = 0;

 61     for (int i = 0; i < n; i++){

 62         while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0){

 63             m--;

 64         }

 65         ch[m++] = p[i];

 66     }

 67     int k = m;

 68     for (int i = n - 2; i >= 0; i--){

 69         while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0){

 70             m--;

 71         }

 72         ch[m++] = p[i];

 73     }

 74     if (n > 1){

 75         m--;

 76     }

 77     return m;

 78 }

 79 int RotateCaliper(int m, Point *ch){

 80     if (m == 2) return Distance(ch[0], ch[1]);

 81 

 82     int i, j;

 83     i = j = 0;

 84     for (int k = 0; k<m; k++){

 85         if (ch[i].x>ch[k].x){

 86             i = k;

 87         }

 88         if (ch[j].x < ch[k].x){

 89             j = k;

 90         }

 91     }

 92     int res = 0, si = i, sj = j;

 93 

 94     while (i != sj || j != si)

 95     {

 96         res = max(res, Distance(ch[i], ch[j]));

 97         if (Cross(ch[(i + 1) % m] - ch[i], ch[(j + 1) % m] - ch[j]) < 0){

 98             i = (i + 1) % m;

 99         }

100         else {

101             j = (j + 1) % m;

102         }

103     }

104     return  res;

105 }

106 const int MAXN = 50005;

107 int n;

108 Point p[MAXN], ch[MAXN];

109 int main()

110 {

111 #ifdef OPEN_FILE

112     //freopen("in.txt", "r", stdin);

113     //freopen("out.txt", "w", stdout);

114 #endif // OPEN_FILE

115     while (~scanf("%d", &n)){

116         for (int i = 0; i < n; i++){

117             scanf("%d%d", &p[i].x, &p[i].y);

118         }

119         int m = ConvexHull(p, n, ch);    

120         int ans = RotateCaliper(m, ch);

121         printf("%d\n", ans);

122     }

123 }
View Code

 

你可能感兴趣的:(计算)