计算几何 2017.4.12

来源:互联网 发布:淘宝店铺没有访客 编辑:程序博客网 时间:2024/05/21 01:52

1、POJ 2242 The Circumference of the Circle

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#include <queue>#include <vector>#include <stack>#include <map>#include <set>#include <cmath>#include <cctype>#include <ctime>#include <cassert>using namespace std;#define REP(i, n) for (int i = 0; i < (n); ++i)struct Point {  double x, y;  Point(double x = 0.0, double y = 0.0):x(x),y(y) {}};struct Seg { Point a, b; };typedef Point Vector;typedef Seg Line;struct Circle {    Point c; double r;    Point point(double a) { return Point{c.x + cos(a) * r, c.y + sin(a) * r}; }};const double INF = 1e20;const double eps = 1e-10;const double PI = acos(-1.0);Point operator + (const Point& a, const Vector& b) { return Point(a.x + b.x, a.y + b.y); }Vector operator - (const Point& a, const Point& b) { return Vector(a.x - b.x, a.y - b.y); }Vector operator * (const Vector& a, double k) { return Vector(a.x * k, a.y * k); }Vector operator / (const Vector& a, double k) { return Vector{a.x / k, a.y / k}; }int dcmp(double x) { if (fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; }bool operator == (const Point& a, const Point& b) {    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;}bool operator < (const Point& a, const Point& b) {    return a.x < b.x || (a.x == b.x && a.y < b.y);}Point read_point() { double x, y; scanf("%lf %lf", &x, &y); return Point{x, y}; }Seg read_seg() { return Seg{read_point(), read_point()}; }Line read_line() { return Line{read_point(), read_point()}; }double dot(const Vector& a, const Vector& b) { return a.x * b.x + a.y * b.y; }double cross(const Vector& a, const Vector& b) { return a.x * b.y - a.y * b.x; }double length(const Vector& a) { return sqrt(dot(a, a)); }Circle circum_circle(const Point& p1, const Point& p2, const Point& p3) {    double Bx = p2.x - p1.x, By = p2.y - p1.y;    double Cx = p3.x - p1.x, Cy = p3.y - p1.y;    double D = 2.0 * (Bx * Cy - By * Cx);    double cx = (Cy * (Bx * Bx + By * By) - By * (Cx * Cx + Cy * Cy)) / D + p1.x;    double cy = (Bx * (Cx * Cx + Cy * Cy) - Cx * (Bx * Bx + By * By)) / D + p1.y;    Point p = Point(cx, cy);    return Circle{p, length(p1 - p)};}int main() {#ifdef __AiR_H    freopen("in.txt", "r", stdin);//    freopen("out.txt", "w", stdout);#endif // __AiR_H    Point a, b, c; Circle ans;    while (scanf("%lf %lf", &a.x, &a.y) != EOF) {        b = read_point(); c = read_point(); ans = circum_circle(a, b, c);        printf("%.2f\n", 2.0 * PI * ans.r);    }#ifdef __AiR_H    printf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);#endif // __AiR_H    return 0;}


2、ZOJ 1453 Surround the Trees

凸包模板题

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#include <queue>#include <vector>#include <stack>#include <map>#include <set>#include <cmath>#include <cctype>#include <ctime>#include <cassert>using namespace std;#define REP(i, n) for (int i = 0; i < (n); ++i)struct Point { double x, y; Point(double x = 0.0, double y = 0.0):x(x),y(y){} };struct Seg { Point a, b; };typedef Point Vector;typedef Seg Line;struct Circle {    Point c; double r;    Point point(double a) { return Point{c.x + cos(a) * r, c.y + sin(a) * r}; }};const double INF = 1e20;const double eps = 1e-10;const double PI = acos(-1.0);Point point[110], ans[110];int N;Point operator + (const Point& a, const Vector& b) { return Point(a.x + b.x, a.y + b.y); }Vector operator - (const Point& a, const Point& b) { return Vector(a.x - b.x, a.y - b.y); }Vector operator * (const Vector& a, double k) { return Vector(a.x * k, a.y * k); }Vector operator / (const Vector& a, double k) { return Vector{a.x / k, a.y / k}; }int dcmp(double x) { if (fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; }bool operator == (const Point& a, const Point& b) {    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;}bool operator < (const Point& a, const Point& b) {    int t1 = dcmp(a.x - b.x), t2 = dcmp(a.y - b.y); return t1 < 0 || (t1 == 0 && t2 < 0);}Point read_point() { double x, y; scanf("%lf %lf", &x, &y); return Point{x, y}; }Seg read_seg() { return Seg{read_point(), read_point()}; }Line read_line() { return Line{read_point(), read_point()}; }double dot(const Vector& a, const Vector& b) { return a.x * b.x + a.y * b.y; }double cross(const Vector& a, const Vector& b) { return a.x * b.y - a.y * b.x; }double length(const Vector& a) { return sqrt(dot(a, a)); }int convex_hull(Point* p, int n, Point* ch) {    sort(p, p + n); int m = 0;    REP(i, n) {        while (m > 1 && dcmp(cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2])) < 0) { --m; }        ch[m++] = p[i];    }    int k = m;    for (int i = n - 2; i >= 0; --i) {        while (m > k && dcmp(cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2])) < 0) { --m; }        ch[m++] = p[i];    }    if (n > 1) { --m; }    return m;}int main() {#ifdef __AiR_H    freopen("in.txt", "r", stdin);//    freopen("out.txt", "w", stdout);#endif // __AiR_H    while (scanf("%d", &N) && N) {        REP(i, N) { point[i] = read_point(); }        int num = convex_hull(point, N, ans);        double sum = 0.0;        REP(i, num - 1) { sum += length(ans[i + 1] - ans[i]); }        if (num > 1) { sum += length(ans[num - 1] - ans[0]); }        printf("%.2f\n", sum);    }#ifdef __AiR_H    printf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);#endif // __AiR_H    return 0;}


3、POJ 1113 Wall

解题思路:

凸包的周长+2*π*L

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#include <queue>#include <vector>#include <stack>#include <map>#include <set>#include <cmath>#include <cctype>#include <ctime>#include <cassert>using namespace std;#define REP(i, n) for (int i = 0; i < (n); ++i)struct Point { double x, y; Point(double x = 0.0, double y = 0.0):x(x),y(y){} };struct Seg { Point a, b; };typedef Point Vector;typedef Seg Line;struct Circle {    Point c; double r;    Point point(double a) { return Point{c.x + cos(a) * r, c.y + sin(a) * r}; }};typedef long long ll;const double INF = 1e20;const double eps = 1e-10;const double PI = acos(-1.0);Point point[1010], ans[1010];int N, L;Point operator + (const Point& a, const Vector& b) { return Point(a.x + b.x, a.y + b.y); }Vector operator - (const Point& a, const Point& b) { return Vector(a.x - b.x, a.y - b.y); }Vector operator * (const Vector& a, double k) { return Vector(a.x * k, a.y * k); }Vector operator / (const Vector& a, double k) { return Vector{a.x / k, a.y / k}; }int dcmp(double x) { if (fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; }bool operator == (const Point& a, const Point& b) {    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;}bool operator < (const Point& a, const Point& b) {    int t1 = dcmp(a.x - b.x), t2 = dcmp(a.y - b.y); return t1 < 0 || (t1 == 0 && t2 < 0);}Point read_point() { double x, y; scanf("%lf %lf", &x, &y); return Point{x, y}; }Seg read_seg() { return Seg{read_point(), read_point()}; }Line read_line() { return Line{read_point(), read_point()}; }double dot(const Vector& a, const Vector& b) { return a.x * b.x + a.y * b.y; }double cross(const Vector& a, const Vector& b) { return a.x * b.y - a.y * b.x; }double length(const Vector& a) { return sqrt(dot(a, a)); }int convex_hull(Point* p, int n, Point* ch) {    sort(p, p + n); int m = 0;    REP(i, n) {        while (m > 1 && dcmp(cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2])) < 0) { --m; }        ch[m++] = p[i];    }    int k = m;    for (int i = n - 2; i >= 0; --i) {        while (m > k && dcmp(cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2])) < 0) { --m; }        ch[m++] = p[i];    }    if (n > 1) { --m; }    return m;}int main() {#ifdef __AiR_H    freopen("in.txt", "r", stdin);//    freopen("out.txt", "w", stdout);#endif // __AiR_H    scanf("%d %d", &N, &L);    REP(i, N) { point[i] = read_point(); }    int num = convex_hull(point, N, ans);    double sum = 0.0;    REP(i, num - 1) { sum += length(ans[i + 1] - ans[i]); }    sum += length(ans[num - 1] - ans[0]); sum += 2.0 * PI * L;    printf("%.0f\n", sum);#ifdef __AiR_H    printf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);#endif // __AiR_H    return 0;}


4、POJ 3348 Cows

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#include <queue>#include <vector>#include <stack>#include <map>#include <set>#include <cmath>#include <cctype>#include <ctime>#include <cassert>using namespace std;#define REP(i, n) for (int i = 0; i < (n); ++i)struct Point { double x, y; Point(double x = 0.0, double y = 0.0):x(x),y(y){} };struct Seg { Point a, b; };typedef Point Vector;typedef Seg Line;struct Circle {    Point c; double r;    Point point(double a) { return Point{c.x + cos(a) * r, c.y + sin(a) * r}; }};typedef long long ll;const double INF = 1e20;const double eps = 1e-10;const double PI = acos(-1.0);const int maxn = 1e4 + 10;Point point[maxn], ans[maxn];int N;Point operator + (const Point& a, const Vector& b) { return Point(a.x + b.x, a.y + b.y); }Vector operator - (const Point& a, const Point& b) { return Vector(a.x - b.x, a.y - b.y); }Vector operator * (const Vector& a, double k) { return Vector(a.x * k, a.y * k); }Vector operator / (const Vector& a, double k) { return Vector{a.x / k, a.y / k}; }int dcmp(double x) { if (fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; }bool operator == (const Point& a, const Point& b) {    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;}bool operator < (const Point& a, const Point& b) {    int t1 = dcmp(a.x - b.x), t2 = dcmp(a.y - b.y); return t1 < 0 || (t1 == 0 && t2 < 0);}Point read_point() { double x, y; scanf("%lf %lf", &x, &y); return Point{x, y}; }Seg read_seg() { return Seg{read_point(), read_point()}; }Line read_line() { return Line{read_point(), read_point()}; }double dot(const Vector& a, const Vector& b) { return a.x * b.x + a.y * b.y; }double cross(const Vector& a, const Vector& b) { return a.x * b.y - a.y * b.x; }double length(const Vector& a) { return sqrt(dot(a, a)); }int convex_hull(Point* p, int n, Point* ch) {    sort(p, p + n); int m = 0;    REP(i, n) {        while (m > 1 && dcmp(cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2])) < 0) { --m; }        ch[m++] = p[i];    }    int k = m;    for (int i = n - 2; i >= 0; --i) {        while (m > k && dcmp(cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2])) < 0) { --m; }        ch[m++] = p[i];    }    if (n > 1) { --m; }    return m;}double poly_area(Point* p, int n) {    double area = 0.0;    for (int i = 1; i < n - 1; ++i) { area += cross(p[i] - p[0], p[i + 1] - p[0]); }    return area / 2.0;}int main() {#ifdef __AiR_H    freopen("in.txt", "r", stdin);//    freopen("out.txt", "w", stdout);#endif // __AiR_H    scanf("%d", &N);    REP(i, N) { point[i] = read_point(); }    int num = convex_hull(point, N, ans);    double area = poly_area(ans, num);    printf("%d\n", (int)(area / 50.0));#ifdef __AiR_H    printf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);#endif // __AiR_H    return 0;}

5、POJ 1228 Grandpa's Estate

参考:http://blog.csdn.net/acdreamers/article/details/10023615

题意:

判断凸包是否稳定

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#include <queue>#include <vector>#include <stack>#include <map>#include <set>#include <cmath>#include <cctype>#include <ctime>#include <cassert>using namespace std;#define REP(i, n) for (int i = 0; i < (n); ++i)struct Point { double x, y; Point(double x = 0.0, double y = 0.0):x(x),y(y){} };struct Seg { Point a, b; };typedef Point Vector;typedef Seg Line;struct Circle {    Point c; double r;    Point point(double a) { return Point{c.x + cos(a) * r, c.y + sin(a) * r}; }};typedef long long ll;const double INF = 1e20;const double eps = 1e-10;const double PI = acos(-1.0);const int maxn = 1e3 + 10;Point point[maxn], ans[maxn];int T, N;Point operator + (const Point& a, const Vector& b) { return Point(a.x + b.x, a.y + b.y); }Vector operator - (const Point& a, const Point& b) { return Vector(a.x - b.x, a.y - b.y); }Vector operator * (const Vector& a, double k) { return Vector(a.x * k, a.y * k); }Vector operator / (const Vector& a, double k) { return Vector{a.x / k, a.y / k}; }int dcmp(double x) { if (fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; }bool operator == (const Point& a, const Point& b) {    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;}bool operator < (const Point& a, const Point& b) {    int t1 = dcmp(a.x - b.x), t2 = dcmp(a.y - b.y); return t1 < 0 || (t1 == 0 && t2 < 0);}Point read_point() { double x, y; scanf("%lf %lf", &x, &y); return Point{x, y}; }Seg read_seg() { return Seg{read_point(), read_point()}; }Line read_line() { return Line{read_point(), read_point()}; }double dot(const Vector& a, const Vector& b) { return a.x * b.x + a.y * b.y; }double cross(const Vector& a, const Vector& b) { return a.x * b.y - a.y * b.x; }double length(const Vector& a) { return sqrt(dot(a, a)); }int convex_hull(Point* p, int n, Point* ch) {    sort(p, p + n); int m = 0;    REP(i, n) {        while (m > 1 && dcmp(cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2])) < 0) { --m; }        ch[m++] = p[i];    }    int k = m;    for (int i = n - 2; i >= 0; --i) {        while (m > k && dcmp(cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2])) < 0) { --m; }        ch[m++] = p[i];    }    if (n > 1) { --m; }    return m;}double poly_area(Point* p, int n) {    double area = 0.0;    for (int i = 1; i < n - 1; ++i) { area += cross(p[i] - p[0], p[i + 1] - p[0]); }    return area / 2.0;}bool is_parallel(const Seg& p, const Seg& q) {    double t = (p.a.y - p.b.y) * (q.a.x - q.b.x) - (p.a.x - p.b.x) * (q.a.y - q.b.y);    return dcmp(t) == 0;}int main() {#ifdef __AiR_H    freopen("in.txt", "r", stdin);//    freopen("out.txt", "w", stdout);#endif // __AiR_H    scanf("%d", &T);    while (T--) {        scanf("%d", &N);        REP(i, N) { point[i] = read_point(); }        int num = convex_hull(point, N, ans); ans[num++] = ans[0];        bool flag = true; int cnt = 0, t = 0, i = 0, j;        while (i < num) {            j = i + 2; t = 0;            while (j < num && is_parallel(Seg{ans[i], ans[i + 1]}, Seg{ans[i], ans[j]})) { ++t; ++j; }            if (t == 0) { flag = false; break; }            ++cnt; if (j == num) { break; } i = j - 1;        }        if (flag && cnt > 1) { printf("YES\n"); }        else { printf("NO\n"); }    }#ifdef __AiR_H    printf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);#endif // __AiR_H    return 0;}


0 0