半平面交,套模板。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
const double eps = 1e-8;
const double PI = acos(-1.0);
const int maxn = 220;
int sgn(double x) { return (x < -eps) ? -1 : (x > eps);}
struct Point
{
double x,y;
Point(){}
Point(double _x,double _y) : x(_x), y(_y) {};
Point operator -(const Point &b)const { return Point(x - b.x, y - b.y);}
double operator ^(const Point &b)const { return x*b.y - y*b.x;}
double operator *(const Point &b)const { return x*b.x + y*b.y;}
};
struct Line
{
Point s,e;
double k;
Line(){}
Line(Point _s,Point _e)
{
s = _s; e = _e;
k = atan2(e.y - s.y,e.x - s.x);
}
Point operator &(const Line &b)const
{
Point res = s;
double t = ((s - b.s)^(b.s - b.e))/((s - e)^(b.s - b.e));
res.x += (e.x - s.x)*t;
res.y += (e.y - s.y)*t;
return res;
}
};
Line line[maxn];
Point poly[maxn];
int cnt, scnt;
//半平面交,直线的左边代表有效区域
bool HPIcmp(Line a,Line b)
{
if(fabs(a.k - b.k) > eps)return a.k < b.k;
return ((a.s - b.s)^(b.e - b.s)) < 0;
}
Line Q[maxn];
void HPI(Line line[], int n, Point res[], int &resn)
{
int tot = n;
sort(line,line+n,HPIcmp);
tot = 1;
for(int i = 1;i < n;i++)
if(fabs(line[i].k - line[i-1].k) > eps)
line[tot++] = line[i];
int head = 0, tail = 1;
Q[0] = line[0];
Q[1] = line[1];
resn = 0;
for(int i = 2; i < tot; i++)
{
if(fabs((Q[tail].e-Q[tail].s)^(Q[tail-1].e-Q[tail-1].s)) < eps || fabs((Q[head].e-Q[head].s)^(Q[head+1].e-Q[head+1].s)) < eps)
return;
while(head < tail && (((Q[tail]&Q[tail-1]) - line[i].s)^(line[i].e-line[i].s)) > eps)
tail--;
while(head < tail && (((Q[head]&Q[head+1]) - line[i].s)^(line[i].e-line[i].s)) > eps)
head++;
Q[++tail] = line[i];
}
while(head < tail && (((Q[tail]&Q[tail-1]) - Q[head].s)^(Q[head].e-Q[head].s)) > eps)
tail--;
while(head < tail && (((Q[head]&Q[head-1]) - Q[tail].s)^(Q[tail].e-Q[tail].e)) > eps)
head++;
if(tail <= head + 1)return;
for(int i = head; i < tail; i++)
res[resn++] = Q[i]&Q[i+1];
if(head < tail - 1)
res[resn++] = Q[head]&Q[tail];
}
double CalcArea(Point p[],int n)
{
double res = 0;
for(int i = 0;i < n;i++)
res += (p[i]^p[(i+1)%n]);
return fabs(res/2);
}
double dist(Point a,Point b)
{
return sqrt((a-b)*(a-b));
}
void change(Point a,Point b,Point &c,Point &d,double p)//将线段ab往左移动距离p
{
double len = dist(a,b);
double dx = (a.y - b.y)*p/len;
double dy = (b.x - a.x)*p/len;
c.x = a.x + dx; c.y = a.y + dy;
d.x = b.x + dx; d.y = b.y + dy;
}
int main()
{
Point cur(0.0, 0.0), tar;
char cmd[10] = "";
cnt = 4, scnt = 0;
line[0] = Line(Point(0,0), Point(10,0));
line[1] = Line(Point(10,0), Point(10,10));
line[2] = Line(Point(10,10), Point(0,10));
line[3] = Line(Point(0,10), Point(0,0));
bool zero = false;
while (~scanf("%lf%lf%s", &tar.x, &tar.y, cmd)) {
if (zero) {
puts("0.00");
continue;
}
Line ln(cur, tar), lf, rt;
change(cur, tar, lf.s, lf.e, 1.0);
change(cur, tar, rt.s, rt.e, -1.0);
Point p1(0.5*(cur.x+tar.x), 0.5*(cur.y+tar.y));
Point p2(0.5*(lf.s.x+lf.e.x), 0.5*(lf.s.y+lf.e.y));
Point p3(0.5*(rt.s.x+rt.e.x), 0.5*(rt.s.y+rt.e.y));
if (cmd[0] == 'S') {
puts("0.00");
zero = true;
continue;
}
else if (cmd[0] == 'H')
line[cnt++] = Line(p1, p3);
else
line[cnt++] = Line(p1, p2);
bool ok = false;
int resn;
HPI(line, cnt, poly, resn);
if (resn > 2) {
if (CalcArea(poly, resn) > eps) {
ok = true;
printf("%.2lf\n", CalcArea(poly, resn));
}
}
if (!ok) {
puts("0.00");
zero = true;
}
cur = tar;
}
return 0;
}