zoukankan      html  css  js  c++  java
  • 求多边形面积

    题目链接:UVALive 2419  Area

    题意:按顺时针或逆时针给出n个点,如果能构成多边形求面积,如果不能,输出impossible.

    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #define maxn 2010
    #include <math.h>
    using namespace std;
    
    const double eps = 1e-10;
    
    struct Point {
        double x, y;
    }p[maxn];
    
    double min(double a, double b) {
        return a < b ? a : b;
    }
    
    double max(double a, double b) {
        return a > b ? a : b;
    }
    
    bool inter(Point sta, Point eda, Point stb, Point edb) { // 判断线段非规范相交 相交返回true
        Point a = sta, b = eda, c = stb, d = edb;
        if (min(a.x,b.x) > max(c.x,d.x)) return 0;
        if (min(a.y, b.y) > max(c.y, d.y)) return 0;
        if (min(c.x, d.x) > max(a.x, b.x)) return 0;
        if (min(c.y, d.y) > max(a.y, b.y)) return 0;
    
        double h, i, j, k;
        h = (b.x-a.x)*(c.y-a.y) - (b.y-a.y)*(c.x-a.x);
        i = (b.x-a.x)*(d.y-a.y) - (b.y-a.y)*(d.x-a.x);
        j = (d.x-c.x)*(a.y-c.y) - (d.y-c.y)*(a.x-c.x);
        k = (d.x-c.x)*(b.y-c.y) - (d.y-c.y)*(b.x-c.x);
        return (h*i<=eps && j*k<=eps);
    }
    
    double area(int vcnt, Point p[]) { // 求凹多边形或者凸多边形面积
        if (vcnt <3) return 0;
        p[vcnt] = p[0];
        double s = 0;
        for (int i=0; i<vcnt; ++i) {
            s += (p[i+1].y+p[i].y)*(p[i+1].x-p[i].x);
        }
        return fabs(s/2);
    }
    
    int main() {
        int n, cas = 0;
        while(~scanf("%d", &n)) {
            if (n  == 0) break;
            for (int i=0; i<n; ++i) {
                scanf("%lf%lf", &p[i].x, &p[i].y);
            }
            bool poss = true;
            if (n < 3) poss = false;
    
            p[n] = p[0]; // 遍历所有不相邻的任意两条边,判断如果都不相交,说明能构成多边形。
            for (int i=0; i<n; ++i) {
                if (poss == false) break;
                for (int j=i+2; j<n; ++j) {
                    if (i == 0 && j == n-1) continue;
                    if (inter(p[i], p[i+1], p[j], p[j+1])) {
                        poss = false;
                        break;
                    }
                }
            }
    
            if (cas != 0) printf("
    ");
            if (poss == false) {
                printf("Figure %d: Impossible
    ", ++cas);
                continue;
            }
    
           double ans = area(n, p);
           printf("Figure %d: %.2f
    ", ++cas, ans);
        }
        return 0;
    }
    View Code

    题目链接:UVALive 2057 Buggy Sat

    题意:给出几个视图,问哪个是outer region,定义outer region 包含其它所有的region。于是变成求最大的面积。

    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    struct Point {
        int x, y;
    }p[210];
    
    double det(Point a, Point b) {
        return a.x * b.y - a.y * b.x;
    }
    
    struct Poly {
        int n;
        Point a[];
    
        double area() {
            double sum = 0;
            a[n] = a[0];
            for (int i=0; i<n; ++i) {
                sum += det(a[i+1], a[i]);
            }
            return abs(sum/2);
        }
    
    }poly[210];
    
    double s[210];
    
    int main() {
        int t, n, m;
        cin >> t;
        while(t--) {
            cin >> n;
            for (int i=0; i<n; ++i) {
                cin >> p[i].x >> p[i].y;
            }
            cin >> m;
            int ans = 0;
            double maxm = -1;
            for (int i=0; i<m; ++i) {
                cin >> poly[i].n;
                for (int j=0; j<poly[i].n; ++j) {
                    int temp;
                    cin >> temp;
                    temp -= 1;
                    poly[i].a[j] = p[temp];
                }
                s[i] = poly[i].area();
                //cout << s[i] << "==
    ";
                if (maxm < s[i]) {
                    maxm = s[i];
                    ans = i+1;
                }
            }
            cout << ans << endl;
        }
        return 0;
    }
    View Code

    不知道为什么用叉积求第一题会WA。

  • 相关阅读:
    SharedPreferences
    Handler
    Gallery 和ImageSwitcher
    poj 1077 Eight (BFS)
    HDU 1208 Pascal's Travels( 记忆化搜索)
    HDU 1619 Unidirectional TSP (dp,dfs)
    HDU 3683 Gomoku (枚举+BFS)
    HDU 3647 Tetris (暴力DFS)
    poj 1020 Anniversary Cake (DFS)
    poj 1375 Intervals(解析几何 过圆外一点求与圆的切线)
  • 原文地址:https://www.cnblogs.com/icode-girl/p/5700353.html
Copyright © 2011-2022 走看看