码迷,mamicode.com
首页 > 其他好文 > 详细

求多边形面积

时间:2016-07-24 12:01:00      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

题目链接: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("\n");
        if (poss == false) {
            printf("Figure %d: Impossible\n", ++cas);
            continue;
        }

       double ans = area(n, p);
       printf("Figure %d: %.2f\n", ++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);
    }

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

        }
    }
}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] << "==\n";
            if (maxm < s[i]) {
                maxm = s[i];
                ans = i+1;
            }
        }
        cout << ans << endl;
    }
    return 0;
}
View Code

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

求多边形面积

标签:

原文地址:http://www.cnblogs.com/icode-girl/p/5700353.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!