半平面交
裸的,注意事项在代码里。
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#define N 505
using namespace std;
namespace runzhe2000
{
typedef double db;
const db eps = 1e-5;
struct point
{
db x, y;
point operator + (const point &that) const {
return (point){
x + that.x, y + that.y};}
point operator - (const point &that) const {
return (point){
x - that.x, y - that.y};}
db operator * (const point &that) const {
return x*that.y-y*that.x;}
point operator * (const db &v) const {
return (point){
x*v, y*v};}
}p[N];
struct line
{
point p, v; db alpha;
bool operator < (const line &that) const {
return alpha < that.alpha;}
}l[N], q[N];
int n, lcnt, head, tail;
point get_cp(line l1, line l2)
{
/*
精妙的实现。考虑通过面积比得到高的比,再得到线段的比。注意叉乘别反了。
*/
point u = l1.p - l2.p; db base = (l2.v * u) / (l1.v * l2.v);
return l1.p + l1.v * base;
}
bool onlef(point p, line l)
{
point u = p-l.p; return l.v * u > 0;
}
void HP()
{
sort(l+1, l+1+lcnt);
for(int i = 1; i <= lcnt; i++)
{
for(; tail - head >= 2 && !onlef(get_cp(q[tail-1], q[tail-2]), l[i]); tail--);
if(tail - head >= 1 && fabs(l[i].alpha -