ZOJ 1010

// C++Exercise.cpp : 定义控制台应用程序的入口点。
//

#include<iostream>
#include<string>
#include<stack>
#include<map>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstdio>
#define eps 1e-9
using namespace std;
int n;
typedef struct{
	double x, y;
}point;

bool Intersect(vector<point>& vp,int i,int j){
	point A = vp[i];
	point B = vp[i + 1];
	point C = vp[j];
	point D = vp[j+1];
	if (min(A.x, B.x) > max(C.x, D.x) || min(A.y, B.y) > max(C.y, D.y)
		|| min(C.x, D.x) > max(A.x, B.x) || min(C.y, D.y) > max(A.y, B.y)) return false;
	double a = (C.y - A.y)*(B.x - A.x) - (C.x - A.x)*(B.y - A.y);
	double b = (D.y - A.y)*(B.x - A.x) - (D.x - A.x)*(B.y - A.y);
	double c = (C.x - D.x)*(A.y - D.y) - (C.y - D.y)*(A.x - D.x);
	double d = (C.x - D.x)*(B.y - D.y) - (C.y - D.y)*(B.x - D.x);
	return a*b <=eps&& c*d <=eps;
}

double Cross(point a,point b){
	return a.x*b.y - a.y*b.x;
}

double Cal(vector<point>& vp){
	double sum = 0;
	for (int i = 0; i < vp.size(); i++){
		sum += Cross(vp[i],vp[(i+1)%vp.size()]);
	}
	return fabs(sum)/2;
}

bool judge(double x){
	if (fabs(x) < eps) return 0;
	else return 1;
}

int main(){
	int Figure = 0;
	while (cin >> n){
		if (n == 0) break;
		Figure++;
		vector<point> vp;
		for (int i = 0; i < n; i++){
			point temp;
			cin >> temp.x >> temp.y;
			vp.push_back(temp);
		}
		bool flag = true;
		for (int i = 0; i < vp.size() && flag; i++){
			for (int j = i + 2; j+1 < vp.size() && flag; j++){
				if (Intersect(vp, i, j)) flag = false;
			}
		}
		if (Figure > 1) cout << endl;
		cout << "Figure " << Figure << ": ";
		if (!flag){
			cout << "Impossible" << endl;
			continue;
		}
		double area = Cal(vp);
		if (judge(area) == 0) cout << "Impossible" << endl;
		else printf("%.2f\n", area);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值