标签:
题意:在一个矩形空间内,左下角坐标(0,0),右上角坐标(10,10),然后两个小孩子玩游戏,乙让甲猜一个物体的位置,初始必须猜(0,0),然后之后甲说出的位置,乙都会根据上一次猜的位置和这一次猜的位置距离正确位置的远近给出一个答案,Colder说明这次猜远了,Hotter说明猜近了,Same说明猜的是正确位置。输出每次询问后,所有可能位置占的总面积。
题解:用切割多边形的方式求半平面交多边形面积。当前的点和之前的点的中点找到,然后之前点到当前点的有向连线逆时针旋转90°得到切割向量,中点加切割向量是另一个点,根据这两个点确定有向直线,如果Colder半平面交取左边,Hotter取右边,Same后面积都是0表示已找到。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
const double eps = 1e-9;
const double PI = acos(-1);
int dcmp(double x) {
if (fabs(x) < eps)
return 0;
return x > 0 ? 1 : -1;
}
struct Point {
double x, y;
Point (double a = 0, double b = 0): x(a), y(b) {}
};
typedef Point Vector;
typedef vector<Point> Polygon;
Vector operator + (const Vector& a, const Vector& b) { return Vector(a.x + b.x, a.y + b.y); }
Vector operator - (const Vector& a, const Vector& b) { return Vector(a.x - b.x, a.y - b.y); }
Vector operator * (const Vector& a, double b) { return Vector(a.x * b, a.y * b); }
Vector operator / (const Vector& a, double b) { return Vector(a.x / b, a.y / b); }
bool operator == (const Vector& a, const Vector& b) { return !dcmp(a.x - b.x) && !dcmp(a.y - b.y); }
bool operator < (const Vector& a, const Vector& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }
double Dot(const Vector& a, const Vector& b) { return a.x * b.x + a.y * b.y; }
double Length(const Vector& a) { return sqrt(Dot(a, a)); }
double Cross(const Vector& a, const Vector& b) { return a.x * b.y - a.y * b.x; }
double Angle(const Vector& a, const Vector& b) { return acos(Dot(a, b) / Length(a) / Length(b)); }
//向量A旋转rad弧度,rad负值为顺时针旋转
Point Rotate(Point A, double rad) {
return Point(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
}
double PolygonArea(Polygon& res, int m) {
double area = 0;
for (int i = 1; i < m - 1; i++)
area += Cross(res[i] - res[0], res[i + 1] - res[0]);
return area / 2;
}
Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) {
Point u = P - Q;
double t = Cross(w, u) / Cross(v, w);
return P + v * t;
}
bool OnSegment(const Point& p, const Point& a1, const Point& a2) {
return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0;
}
//切割多边形,返回有向直线A->B左边的多边形
Polygon CutPolygon(Polygon poly, Point A, Point B) {
Polygon newpoly;
int n = poly.size();
for (int i = 0; i < n; i++) {
Point C = poly[i];
Point D = poly[(i + 1) % n];
if (dcmp(Cross(B - A, C - A)) >= 0)
newpoly.push_back(C);
if (dcmp(Cross(B - A, C - D)) != 0) {
Point ip = GetLineIntersection(A, B - A, C, D - C);
if (OnSegment(ip, C, D))
newpoly.push_back(ip);
}
}
return newpoly;
}
Point pre, now;
char str[10];
Polygon poly;
int main() {
pre = Point(0, 0);
poly.push_back(pre);
poly.push_back(Point(10, 0));
poly.push_back(Point(10, 10));
poly.push_back(Point(0, 10));
int flag = 0;
while (scanf("%lf%lf%s", &now.x, &now.y, str) == 3) {
if (flag) {
printf("0.00\n");
continue;
}
Point mid = Point((pre.x + now.x) * 0.5, (pre.y + now.y) * 0.5);
Point temp = mid + Rotate(now - pre, PI * 0.5);
if (str[0] == ‘C‘)
poly = CutPolygon(poly, mid, temp);
else if (str[0] == ‘H‘)
poly = CutPolygon(poly, temp, mid);
else {
printf("0.00\n");
flag = 1;
continue;
}
printf("%.2lf\n", PolygonArea(poly, poly.size()));
pre = now;
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/hyczms/article/details/47315367