- Written
- Published
- Updated
分篇:1 · 2 · 3 · 4 · 5 · 6 · 7 · 8 · 9 · 10 · 11 · 12 · 13
5 计算几何
注意事项
· 尽量少用三角函数、除法、开方、求幂、取对数运算
· 在不溢出的情况下将除式比较转化为乘式比较
· 必要情况下把浮点数化为整数处理
· 注意输出负零的情况
· 注意除数 模数是否为零
三角形
面积
· 两条边叉积的一半的绝对值
· 海伦公式
. 三角函数
#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include <cstdio>
#include <assert.h>
// #include <windows.h>
#include <cstring>
using namespace std;
typedef long long ll;
const int N = 1e6 + 5; //remember to modify the range of the data!!
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int maxp = 1e6 + 5;
const double Pi = acos(-1.0);
co;nst double eps = 1e-8;
// #define double long double //开的时候注意输入输出用 %Lf
int n, m, T;
int sgn(double x)
{
if(fabs(x) < eps)
return 0;
if(x > 0)
return 1;
else
return -1;
}
//向量或者点
struct Point
{
double x, y;
Point() {};//构造函数
Point(double a, double b): x(a), y(b) {};
void input()
{
scanf("%lf%lf", &x, &y);
}
void write()
{
printf("%.8f %.8f\n", x, y);
}
bool operator == (const Point rhs) const
{
return sgn(x - rhs.x) == 0 && sgn(y - rhs.y) == 0;
}
bool operator < (const Point rhs) const //按x轴升序,x相同时按y升序
{
return sgn(x - rhs.x) == 0 ? sgn(y - rhs.y) < 0 : x < rhs.x;
}
Point operator + (const Point rhs) const //加法
{
return Point(x + rhs.x, y + rhs.y);
}
Point operator - (const Point rhs) const //减法
{
return Point(x - rhs.x, y - rhs.y);
}
double operator ^ (const Point rhs) const //向量叉积
{
return x * rhs.y - y * rhs.x;
}
double operator * (const Point rhs) const //向量点积
{
return x * rhs.x + y * rhs.y;
}
Point operator * (const double rhs) const //数量乘
{
return Point(x * rhs, y * rhs);
}
Point operator / (const double rhs) const //数量除法
{
return Point(x / rhs, y / rhs);
}
//返回向量模长
double len()
{
return hypot(x,y);//库函数计算sqrt(x * x + y * y)
}
//返回向量模长的平方
double len2()
{
return x * x + y * y;
}
//将向量转化为长度为r的向量方向不变
Point trunc(double r)
{
double l = len();
if(!sgn(l))
return *this;
r /= l;
return Point(x * r, y * r);
}
//向量顺时针旋转90度
Point clockwiseRot()
{
return Point(y, -x);
}
//向量逆时针旋转90度
Point anticlockwiseRot()
{
return Point(-y, x);
}
//向量(即绕原点)逆时针旋转theta度(弧度制)
Point rotate(double theta)
{
double c = cos(theta), s = sin(theta);
return Point(x * c - y * s, x * s + y * c);
}
//点绕p点逆时针旋转theta度(弧度制)
Point rotate(Point p, double theta)
{
Point v = (*this) - p;
double c = cos(theta), s = sin(theta);
return Point(p.x + v.x * c - v.y * s, p.y + v.x * s + v.y * c);
}
//两点之间的距离
double distance (const Point rhs)
{
return hypot(x - rhs.x, y - rhs.y);
}
//两向量的夹角(弧度制)
double rad(const Point rhs)
{
return fabs(atan2(fabs((*this) ^ rhs), (*this) * rhs));
}
bool quadrant(void) const //判断是否在一四象限含x正半轴和y负半轴
{
return y > 0 || (y == 0 && x >= 0);
}
};
//直线或线段
struct Line
{
Point s, e; //s起点, e终点
Line() {}; //构造函数
Line(Point s, Point e) : s(s), e(e) {};
Line(double x1, double y1, double x2, double y2) : s(Point(x1, y1)), e(Point(x2, y2)) {};
//根据一个点和倾斜角 theta(弧度) 确定直线,0 <= angle < Pi
Line(Point p, double theta)
{
s = p;
if(sgn(theta - Pi / 2) == 0) //垂直x轴
e = s + Point(0, 1);
else
e = s + Point(1, tan(theta));
}
//ax + by + c = 0
Line(double a, double b, double c)
{
if(sgn(a) == 0) //垂直y轴
s = Point(0, - c / b), e = Point(1, - c / b);
else if(sgn(b) == 0) //垂直x轴
s = Point(- c / a, 0), e = Point(- c / a, 1);
else
s = Point(0, -c / b), e = Point(1, (-c - a) / b);
}
void input()
{
scanf("%lf%lf%lf%lf", &s.x, &s.y, &e.x, &e.y);
}
void write()
{
printf("%.5f %.5f %.5f %.5f\n", s.x, s.y, e.x, e.y);
}
//x小的在前面,相同时y小的在前面
void adjust()
{
if(e < s)
swap(e, s);
}
//线段求长度
double lenth()
{
return s.distance(e);
}
//返回直线的倾斜角 0<= angle <pi
double angle()
{
double k = atan2(e.y - s.y, e.x - s.x);
if(sgn(k) < 0)
k += Pi;
if(sgn(k - Pi) == 0)
k -= Pi;
return k;
}
//点和直线关系 1左边,2右边,3在线上
//左边是指向量srhs与se叉积为负数所以与s,e点的顺序有关
int relation(Point rhs)
{
int c = sgn((rhs - s) ^ (e - s));
if(c < 0)
return 1;
if(c > 0)
return 2;
return 3;
}
// 点在线段上的判断
bool pointOnSeg(Point rhs)
{
return sgn((rhs - s) ^ (e - s)) == 0 && sgn((rhs - s) * (rhs - e)) <= 0;
}
//判断两直线平行或重合
//重合是特殊的平行关系
bool parallel(Line rhs)
{
return sgn((e - s) ^ (rhs.e - rhs.s)) == 0;
}
//判断两直线是否垂直
bool vertical(Line rhs)
{
return ((e - s) * (rhs.e - rhs.s)) == 0;
}
//两线段相交判断 0不相交 1非规范相交 2规范相交
int segCrossSeg(Line rhs)
{
int d1 = sgn((e - s) ^ (rhs.s - s)), d2 = sgn((e - s) ^ (rhs.e - s));
int d3 = sgn((rhs.e - rhs.s) ^ (s - rhs.s)), d4 = sgn((rhs.e - rhs.s)^(e - rhs.s));
if( (d1 ^ d2) == -2 && (d3 ^ d4) == -2) //当前线段端点在另线段两边
return 2;
return (d1 == 0 && sgn((rhs.s - s) * (rhs.s - e)) <= 0) ||
(d2 == 0 && sgn((rhs.e - s) * (rhs.e - e)) <= 0) ||
(d3 == 0 && sgn((s - rhs.s) * (s - rhs.e)) <= 0) ||
(d4 == 0 && sgn((e - rhs.s) * (e - rhs.e)) <= 0);
}
//直线线段相交判断 0不相交 1非规范相交 2规范相交
//-*this line -rhs seg
int lineCrossSeg(Line rhs)
{
int d1 = sgn((e - s) ^ (rhs.s - s));
int d2 = sgn((e - s) ^ (rhs.e - s));
if((d1 ^ d2) == -2)
return 2;
return d1 == 0 || d2 == 0;
}
//两直线关系 0平行 1重合 2垂直 3非垂直相交
int relation(Line rhs)
{
if(parallel(rhs))
return rhs.relation(s) == 3;
if(vertical(rhs))
return 2;
return 3;
}
//求两直线的交点 要保证两直线不平行或重合
Point crossPoint(Line rhs)
{
double s1 = (rhs.e - rhs.s) ^ (s - rhs.s);
double s2 = (rhs.e - rhs.s) ^ (e - rhs.s);
if(rhs.s == s && rhs.e == e)
assert(0);
return Point((s.x * s2 - e.x * s1) / (s2 - s1), (s.y * s2 - e.y * s1) / (s2 - s1));
}
//点到直线的距离
double disPointtoLine(Point rhs)
{
return fabs((rhs - s) ^ (e - s)) / lenth();
}
//点到线段的距离
double disPointtoSeg(Point rhs)
{
if(sgn((rhs - s) * (e - s)) < 0 || sgn((rhs - e) * (s - e) < 0))
return min(rhs.distance(s), rhs.distance(e));
return disPointtoLine(rhs);
}
//线段到线段的距离 相交时距离为零
double disSegtoSeg(Line rhs)
{
if(segCrossSeg(rhs) != 0)
return 0.0;
return min(min(disPointtoSeg(rhs.s), disPointtoSeg(rhs.e)), min(rhs.disPointtoSeg(s), rhs.disPointtoSeg(e)));
}
//点 p 在直线上的投影
Point lineProjection(Point rhs)
{
return s + ((e - s) * ((e - s) * (rhs - s)) / (e - s).len2());
}
//点 p 关于直线的对称点
Point symmetryPoint(Point rhs)
{
Point q = lineProjection(rhs);
return Point(2 * q.x - rhs.x, 2 * q.y - rhs.y);
}
//直线顺时针旋转90度
Line clockwiseRot()
{
return Line(s, s + Point(e - s).clockwiseRot());
}
//直线逆时针旋转90度
Line anticlockwiseRot()
{
return Line(s, s + Point(e - s).anticlockwiseRot());
}
};
//圆
struct circle
{
Point p; //圆心
double r; //半径
circle() {};
circle(Point P, double R): p(P), r(R) {};
circle(double x, double y, double R): p(Point(x, y)), r(R) {};
//三点定圆(三角形的外接圆) 利用两条边的中垂线交点得到圆心
circle(Point a, Point b, Point c)
{
Line u((a + b) / 2, (a + b) / 2 + (b - a).anticlockwiseRot());
Line v((b + c) /2 , (b + c) / 2 + (c - b).anticlockwiseRot());
p = u.crossPoint(v);
r = p.distance(a);
}
// //三角形的内切圆 利用角平分线交点得到圆心
// circle(Point a, Point b, Point c, bool t)
// {
// Line u, v;
// double m = atan2(b.y - a.y, b.x - a.x), n = atan2(c.y - a.y, c.x - a.x);
// u.s = a;
// u.e = u.s + Point(cos((n + m) / 2), sin((n + m) / 2));
// v.s = b;
// m = atan2(a.y - b.y, a.x - b.x) , n = atan2(c.y - b.y, c.x - b.x);
// v.e = v.s + Point(cos((n + m) / 2),sin((n + m) / 2));
// p = u.crossPoint(v);
// r = Line(a, b).disPointtoLine(p);
// }
//三角形的内切圆 利用面积求半径,圆心用内心的向量性质求
circle(Point a, Point b, Point c, bool t)
{
double lena = (b - c).len(), lenb = (a - c).len(), lenc = (a - b).len();
p.x = (lena * a.x + lenb * b.x + lenc * c.x) / (lena + lenb + lenc);
p.y = (lena * a.y + lenb * b.y + lenc * c.y) / (lena + lenb + lenc);
double hf = (lena + lenb + lenc) / 2;
r = sqrt(hf * (hf - lena) * (hf - lenb) * (hf - lenc)) / hf;
}
void input(void)
{
scanf("%lf%lf%lf", &p.x, &p.y, &r);
}
void write(void)
{
printf("%.5f %.5f %.5f\n", p.x, p.y, r);
}
bool operator == (const circle rhs)
{
return (p == rhs.p) && (sgn(r - rhs.r) == 0);
}
//面积
double area(void)
{
return Pi * r * r;
}
//周长
double circumference(void)
{
return 2 * Pi * r;
}
//点和圆的关系 0圆外 1圆上 2圆内
int relation(Point rhs)
{
double dstdiff = sgn(rhs.distance(p) - r);
if(dstdiff < 0)
return 2;
else if(dstdiff == 0)
return 1;
return 0;
}
//线段和圆的关系
//比较的是圆心到线段的距离和半径的关系
int relationSeg(Line rhs)
{
double disdiff = sgn(rhs.disPointtoSeg(p) - r);
if(disdiff < 0)
return 2;
else if(disdiff == 0)
return 1;
return 0;
}
//直线和圆的关系 0相离 1相切 2相交
int relationLine(Line rhs)
{
double disdiff = sgn(rhs.disPointtoLine(p) - r);
if(disdiff < 0)
return 2;
else if(disdiff == 0)
return 1;
return 0;
}
//两圆的关系
//5相离 4外切 3相交 2内切 1内含 0重叠
int relation(circle rhs)
{
double dst = p.distance(rhs.p), diff = fabs(r - rhs.r);
if(sgn(dst - r - rhs.r) > 0)
return 5;
if(sgn(dst - r - rhs.r) == 0)
return 4;
if(sgn(dst - r - rhs.r) < 0 && sgn(dst - diff) > 0)
return 3;
if(sgn(dst - diff) == 0)
return 2;
if((*this) == rhs)
return 0;
return 1;
}
//求两个圆的交点,返回 0 表示没有交点,返回 1 是一个交点,2 是两个交点
int pointCrossCircle(circle rhs, Point &p1, Point &p2)
{
int rel = relation(rhs);
if(rel == 1 || rel == 5)
return 0;
double d = p.distance(rhs.p);
double l = (d * d + r * r - rhs.r * rhs.r) / (2 * d);//圆心到弦长的距离
double h = sqrt(r * r - l * l); //半弦长
Point mid = p + (rhs.p - p).trunc(l), v = (rhs.p - p).anticlockwiseRot().trunc(h);//圆心连续与弦的交点
p1 = mid + v;
p2 = mid - v;
if(rel == 2 || rel == 4)
return 1;
return 2;
}
//求直线和圆的交点,返回 0 表示没有交点,返回 1 是一个交点,2 是两个交点
int pointCrossLine(Line rhs, Point &p1, Point &p2)
{
if(relationLine(rhs) == 0)
return 0;
Point a = rhs.lineProjection(p);
double d = rhs.disPointtoLine(p);
d = sqrt(r * r - d * d); //半弦长
if(sgn(d) == 0)
{
p1 = p2 = a;
return 1;
}
Point v = (rhs.e - rhs.s).trunc(d);
p1 = a + v;
p2 = a - v;
return 2;
}
//过一点q作圆的切线 0没有 1有一条 2有两条
//第一个点是q, 第二个点切点
int tangentline(Point q, Line &x, Line &y)
{
int rel = relation(q);
if(rel == 2)
return 0;
if(rel == 1)
{
x = y = Line(q, (q - p).anticlockwiseRot());
return 1;
}
double d = p.distance(q), l = r * r / d, h = sqrt(r * r - l * l);
Point mid = p + (q - p).trunc(l), v = (q - p).anticlockwiseRot().trunc(h);
x = Line(q, mid + v);
y = Line(q, mid - v);
return 2;
}
//求两圆相交的面积 必要时开long double
double areaCircle(circle rhs)
{
int rel = relation(rhs);
if(rel >= 4)
return 0.0;
if(rel <= 2)
return min(area(), rhs.area());
double d = p.distance(rhs.p);
double hf = (r + rhs.r + d) / 2.0; //半周长
double ss = 2 * sqrtl(hf * (hf - r) * (hf - rhs.r) * (hf - d)); //海伦公式
double a1 = acosl((r * r + d * d - rhs.r * rhs.r) / (2.0 * d * r));
double a2 = acosl((rhs.r * rhs.r + d * d - r * r) / (2.0 * d * rhs.r));
a1 = a1 * r * r;
a2 = a2 * rhs.r * rhs.r;
return a1 + a2 - ss;
}
//求圆和三角形 pab 的相交面积
double areatriangle(Point a, Point b)
{
if(sgn((a - p) ^ (b - p)) == 0)
return 0.0;
Point q[5], p1, p2;
int len = 0;
q[len++] = a;
Line l(a, b);
if(pointCrossLine(l, q[1], q[2]) == 2)
{
if (sgn((a - q[1]) * (b - q[1])) < 0)
q[len++] = q[1];
if (sgn((a - q[2]) * (b - q[2])) < 0)
q[len++] = q[2];
}
q[len++] = b;
if(len == 4 && sgn((q[0] - q[1]) * (q[2] - q[1])) > 0)
swap(q[1], q[2]);
double res = 0;
for(int i = 0; i < len - 1; i++)
if(relation(q[i]) == 0 || relation(q[i + 1]) == 0)
{
double arg = Point(q[i] - p).rad(q[i + 1] - p);
res += r * r * arg / 2.0;
}
else
res += fabs((q[i] - p) ^ (q[i + 1] - p)) / 2.0;
return res;
}
//两圆的公切线 0没有 -1无数 1条数参照vector里点的个数
int commonTangent(circle rhs, vector <Point> &a, vector <Point> &b)
{
bool swaped = false;
circle lhs((*this));
if(lhs.r < rhs.r)
swap(lhs, rhs), swaped = true;
int rel = lhs.relation(rhs);
auto topoint = [](circle o, double ang){
return Point(o.p.x + cos(ang) * o.r, o.p.y + sin(ang) * o.r);
};
// AB向量的极角,A位大圆
double base = atan2(rhs.p.y - lhs.p.y, rhs.p.x - lhs.p.x);
if(rel == 1)
return 0;
if(rel == 0)
return -1;
if(rel == 2 || rel == 4)
a.push_back(topoint(lhs, base)), b.push_back(topoint(rhs, base + Pi * (rel == 4)));
if(rel >= 3) //有两条外公切线
{
double ang = acos((lhs.r - rhs.r) / lhs.p.distance(rhs.p));
a.push_back(topoint(lhs, base + ang)), b.push_back(topoint(rhs, base + ang));
a.push_back(topoint(lhs, base - ang)), b.push_back(topoint(rhs, base - ang));
}
if(rel == 5) //有两条内公切线
{
double ang = acos((lhs.r + rhs.r) / lhs.p.distance(rhs.p));
a.push_back(topoint(lhs, base + ang)), b.push_back(topoint(rhs, base + ang + Pi));
a.push_back(topoint(lhs, base - ang)), b.push_back(topoint(rhs, base - ang + Pi));
}
if(swaped)
swap(a, b);
return 1;
}
};
//多边形
struct polygon
{
int n;
Point p[maxp];
void input(void)
{
for(int i = 0; i < n; i++)
p[i].input();
}
//极角排序
//以最左边的点(x相同时取最下面的)为原点
void norm(void)
{
Point mi = p[0];
for(int i = 1; i < n; i++)
mi = min(mi, p[i]);
sort(p, p + n, [&](Point a, Point b){
int d = sgn((a - mi) ^ (b - mi));
if(d == 0)
return sgn(a.distance(mi) - b.distance(mi)) > 0;
return d > 0;
});
}
//以原点为极点的极角排序
void polarsort(void)
{
sort(p, p + n, [](Point a, Point b){
if(a.quadrant() ^ b.quadrant())//如果象限不同
return a.quadrant() > b.quadrant();
return sgn(a ^ b) > 0;
});
}
//凸包 需要极角排序
void Graham(polygon &convex)
{
norm();
int &top = convex.n;
top = 0;
if(n == 1)
{
top = 1;
convex.p[0] = p[0];
return ;
}
if(n == 2)
{
top = 2;
convex.p[0] = p[0];
convex.p[1] = p[1];
if(p[0] == p[1])
top--;
return;
}
convex.p[0] = p[0];
convex.p[1] = p[1];
top = 2;
for(int i = 2; i < n; i++)
{
while(top > 1 && sgn((convex.p[top - 1] - convex.p[top - 2]) ^ (p[i] - convex.p[top - 2])) <= 0)
top--;
convex.p[top++] = p[i];
}
if(top == 2 && convex.p[0] == convex.p[1]) //特判
top--;
}
//凸包 需要排序
void Andrew(polygon &convex)
{
sort(p, p + n);
convex.n = n;
for(int i = 0; i < min(n, 2); i++)
convex.p[i] = p[i];
if(convex.n == 2 && convex.p[0] == convex.p[1])
convex.n--;
if(n <= 2)
return;
int &top = convex.n = 0;
for(int i = 0; i < n; convex.p[top++] = p[i++])
while(top >= 2 && sgn((convex.p[top - 1] - convex.p[top - 2]) ^ (p[i] - convex.p[top - 1])) <= 0)
top--;
for(int i = n - 2, tem = top + 1; i >= 0; convex.p[top++] = p[i--])
while(top >= tem && sgn((convex.p[top - 1] - convex.p[top - 2]) ^ (p[i] - convex.p[top - 1])) <= 0)
top--;
top--; //需要减一,因为第一个点会入两次
if(top == 2 && convex.p[0] == convex.p[1]) //特判
top--;
}
//判断是否是凸多边形
bool isconvex(void)
{
bool s[3] = {false};
for(int i = 0; i < n; i++)
{
int j = (i + 1) % n, k = (j + 1) % n;
s[sgn((p[j] - p[i]) ^ (p[k] - p[i])) + 1] = true;
if(s[0] && s[2])
return false;
}
return true;
}
//判断是否在凸包里logN
//将凸包三角剖分判断点是否在三角形内
bool inConvex(Point rhs)
{
int l = 1, r = n - 2;
while(l <= r)
{
int mid = l + r >> 1;
double s1 = (p[mid] - p[0]) ^ (rhs - p[0]);
double s2 = (p[mid + 1] - p[0]) ^ (rhs - p[0]);
if(sgn(s1) >= 0 && sgn(s2) <= 0)
{
if(sgn((p[mid + 1] - p[mid]) ^ (rhs - p[mid])) >= 0)
return 1;
return 0;
}
else if(sgn(s1) < 0)
r = mid - 1;
else
l = mid + 1;
}
return 0;
}
//点和多边形的关系 3点上 2边上 1内部 0外部
int relation(Point rhs)
{
assert(n > 0);
for(int i = 0; i < n; i++)
if(rhs == p[i])
return 3;
for(int i = 0; i < n; i++)
if(Line(p[i], p[(i + 1) % n]).pointOnSeg(rhs))
return 2;
int cnt = 0;
for(int i = 0; i < n; i++)
{
int j = (i + 1) % n;
int k = sgn((rhs - p[j]) ^ (p[i] - p[j]));
int u = sgn(p[i].y - rhs.y);
int v = sgn(p[j].y - rhs.y);
if(k > 0 && u < 0 && v >= 0)
cnt++;
if(k < 0 && v < 0 && u >= 0)
cnt--;
}
return cnt != 0;
}
//直径,旋转卡壳
//前提是凸多边形 注意可能模零
double diameter(void)
{
int k = 1;//从第二个点开始枚举
double res = 0;
for(int i = 0; i < n; i++)
{
while(((p[(i + 1) % n] - p[i]) ^ (p[k] - p[i])) < ((p[(i + 1) % n] - p[i]) ^ (p[(k + 1) % n] - p[i])))
k = (k + 1) % n;
res = max(res, max(p[i].distance(p[k]), p[(i + 1) % n].distance(p[k])));
}
return res;
}
//周长
double perimeter(void)
{
double res = 0;
for(int i = 0; i < n; i++)
res += p[i].distance(p[(i + 1) % n]);
return res;
}
//面积
double area(void)
{
double res = 0;
for(int i = 0; i < n; i++)
res += p[i] ^ p[(i + 1) % n];
return fabs(res) / 2;
}
//重心
Point baryCenter(void)
{
Point res(0, 0);
double area = 0;
for(int i = 1; i < n - 1; i++)
{
double S = (p[i] - p[0]) ^ (p[i + 1] - p[0]);
if(sgn(S) == 0) //共线
continue;
area += S;
res.x += (p[0].x + p[i].x + p[i + 1].x) / 3 * S;
res.y += (p[0].y + p[i].y + p[i + 1].y) / 3 * S;
}
if(sgn(area))
res = res / area;
return res;
}
//直线切凸多边形
//多边形是逆时针的,在有向直线rhs的左侧
void convexCut(polygon &res, Line rhs)
{
res.n = 0;
for(int i = 0; i < n; i++)
{
Point now = p[i], nxt = p[(i + 1) % n];
int d1 = sgn((rhs.e - rhs.s) ^ (now - rhs.s)), d2 = sgn((rhs.e - rhs.s) ^ (nxt - rhs.s));
if(d1 >= 0)
res.p[res.n++] = now;
if(d1 * d2 < 0)
res.p[res.n++] = rhs.crossPoint(Line(now, nxt));
}
}
//多边形和圆的面积交
double areacircle(circle c)
{
double ans = 0;
for (int i = 0; i < n; i++)
{
int j = (i + 1) % n;
if (sgn((p[j] - c.p) ^ (p[i] - c.p)) >= 0)
ans += c.areatriangle(p[i], p[j]);
else
ans -= c.areatriangle(p[i], p[j]);
}
return fabs(ans);
}
};
struct halfplane : public Line
{
double angle;
halfplane() {};
halfplane(Point a, Point b)
{
s = a, e = b;
}
void calcangle(void)
{
angle = atan2(e.y - s.y, e.x - s.x);
}
bool operator < (const halfplane &rhs) const
{
return angle < rhs.angle;
}
};
struct halfplanes
{
int n;
halfplane hp[maxp];
Point p[maxp];
int que[maxp];
int st, ed;
void push(halfplane rhs)
{
hp[n++] = rhs;
}
//去重
void unique(void)
{
int m = 1;
for(int i = 1; i < n; i++)
if(sgn(hp[i].angle - hp[i - 1].angle) != 0)
hp[m++] = hp[i];
else if(sgn((hp[m - 1].e - hp[m - 1].s) ^ (hp[i].s - hp[m - 1].s)) > 0) //极角相同取最左边的
hp[m - 1] = hp[i];
n = m;
}
//返回是否面积为零
bool halfplaneintersect(void)
{
for(int i = 0; i < n; i++)
hp[i].calcangle();
sort(hp, hp + n);
unique();
que[st = 0] = 0, que[ed = 1] = 1;
p[1] = hp[0].crossPoint(hp[1]);
for(int i = 2; i < n; i++)
{
while(st < ed && sgn((hp[i].e - hp[i].s) ^ (p[ed] - hp[i].s)) < 0)
ed--;
while(st < ed && sgn((hp[i].e - hp[i].s) ^ (p[st + 1] - hp[i].s)) < 0)
st++;
que[++ed] = i;
if(hp[i]. parallel(hp[que[ed - 1]]))
return false;
p[ed] = hp[i].crossPoint(hp[que[ed - 1]]);
}
while(st < ed && sgn((hp[que[st]].e - hp[que[st]].s) ^ (p[ed] - hp[que[st]].s)) < 0)
ed--;
while(st < ed && sgn((hp[que[ed]].e - hp[que[ed]].s) ^ (p[st + 1] - hp[que[ed]].s)) < 0)
st++;
if(st + 1 >= ed)
return false;
return true;
}
//得到最后半平面交得到的凸多边形
//需要先调用 halfplaneinsert() 且返回 true
void getconvex(polygon &res)
{
p[st] = hp[que[st]].crossPoint(hp[que[ed]]);
res.n = ed - st + 1;
for(int j = st, i = 0; j <= ed; i++, j++)
res.p[i] = p[j];
}
};
//AB x AC
double cross(Point A, Point B, Point C)
{
return (B - A) ^ (C - A);
}
//AB * AC
double dot(Point A, Point B, Point C)
{
return (B - A) * (C - A);
}
//平面最近点对 注意需要先sort一遍对x升序 NlogN
double closestPairDis(Point p[], int num)
{
if(num <= 1)
return 1e100;
int mid = num >> 1;
double x = p[mid].x;
double res = min(closestPairDis(p, mid), closestPairDis(p + mid, num - mid));
inplace_merge(p, p + mid, p + num, [](Point a, Point b){
return a.y < b.y;
});
vector <Point> v;
for(int i = 0; i < num; i++)
{
if(fabs(p[i].x - x) >= res)
continue;
for(int j = v.size() - 1; j >= 0; j--)
{
if(p[i].y - v[j].y >= res) //这就是按y排序的作用满足y和x距离同时小于等于res的点才很少
break;
res = min(res, p[i].distance(v[j]));
}
v.push_back(p[i]);
}
return res;
}
//最小矩形覆盖必须是凸包且逆时针排序
//面积唯一,矩形不唯一
double minRectangleCover(polygon &a)
{
//要特判点小于3的情况
if(a.n < 3)
return 0.0;
a.p[n] = a.p[0];
double ans = -1;
int r = 1, p = 1, q;
for(int i = 0; i < a.n; i++)
{
//卡出离边 a.p[i] - a.p[i + 1] 最远的点
while(sgn(cross(a.p[i], a.p[i + 1], a.p[r + 1]) - cross(a.p[i], a.p[i + 1], a.p[r])) >= 0)
r = (r + 1) % a.n;
//卡出 a.p[i] - a.p[i + 1] 正向最远的点
while(sgn(dot(a.p[i], a.p[i + 1], a.p[p + 1]) - dot(a.p[i], a.p[i + 1], a.p[p])) >= 0 )
p = (p + 1) % a.n;
if(i == 0)
q = p;
//卡出 a.p[i] - a.p[i + 1] 负向最远的点
while(sgn(dot(a.p[i], a.p[i + 1], a.p[q + 1]) - dot(a.p[i], a.p[i + 1], a.p[q])) <= 0 )
q = (q + 1) % a.n;
double d = (a.p[i] - a.p[i + 1]).len2();
double S = cross(a.p[i], a.p[i + 1], a.p[r]) *
(dot(a.p[i], a.p[i + 1], a.p[p]) - dot(a.p[i], a.p[i + 1], a.p[q])) / d;
if(ans < 0 || ans > S)
ans = S;
}
return ans;
}
//凸包a, b的闵可夫斯基和结果存在res里
//多个时可以全部边一起求(需重构)
void Minkowski(polygon &tem, polygon &res, polygon const &a, polygon const &b)
{
// 得到两个凸包的向量集
vector <Point> va(a.n), vb(b.n);
for(int i = 0; i < a.n; i++)
va[i] = a.p[(i + 1) % a.n] - a.p[i];
for(int i = 0; i < b.n; i++)
vb[i] = b.p[(i + 1) % b.n] - b.p[i];
int &n = tem.n;
n = 0;
tem.p[n++] = a.p[0] + b.p[0];
int l1 = 0, l2 = 0;
while(l1 < a.n && l2 < b.n)
tem.p[n] = tem.p[n - 1] + ((va[l1] ^ vb[l2]) > 0 ? va[l1++] : vb[l2++]), n++;
while(l1 < a.n)
tem.p[n] = tem.p[n - 1] + va[l1++], n++;
while(l2 < b.n)
tem.p[n] = tem.p[n - 1] + vb[l2++], n++;
tem.Andrew(res);
}
polygon tem, ans, a, b, cona, conb;
int main(void)
{
return 0;
}
Comments
No approved comments yet.
Your display name and plain-text comment become public only after owner approval. No account or email is required.