#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef double db;
const db Pi = acos(-1);
const db inf = 1e15;
const double eps = 1e-11;
const double Pi = acos(-1);
// 判断正、负还是0
inline int dcmp(db x){
if(fabs(x) < eps) return 0;
return x > 0 ? 1 : -1;
}
db Acos(db x){
if(x <= -1) return Pi;
if(x >= 1) return 0;
return acos(x);
}
db Asin(db x){
if(dcmp(x - 1) >= 0) return Pi / 2;
if(dcmp(x + 1) <= 0) return -Pi / 2;
return asin(x);
}
typedef struct Point{
db x, y;
Point(db a = 0, db b = 0) { x = a, y = b; }
Point operator -(const Point &ret)const { return Point{x - ret.x, y - ret.y}; }
Point operator +(const Point &ret)const { return Point{x + ret.x, y + ret.y}; }
Point operator *(db mul)const { return Point{x * mul, y * mul}; }
Point operator /(db div)const { return Point{x / div, y / div}; }
db operator *(const Point &ret) { return x * ret.x + y * ret.y; } // 点积
db operator ^(const Point &ret) { return x * ret.y - y * ret.x; } // 叉积
db length() { return sqrt(x * x + y * y); }
}Vector;
struct Line{
Point p[2];
db k, b;
Line(db a = 0, db b = 0, db c = 0, db d = 0) { p[0].x = a, p[0].y = b, p[1].x = c, p[1].y = d; }
db length(){
return sqrt((p[0].x - p[1].x) * (p[0].x - p[1].x) + (p[0].y - p[1].y) * (p[0].y - p[1].y));
}
// 获取直线k,b参数
void get_para(){
if(dcmp(p[0].x - p[1].x) == 0){
k = inf;
b = inf;
}
else{
k = (p[1].y - p[0].y) / (p[1].x - p[0].x);
b = p[0].y - k * p[0].x;
}
}
// 点到直线距离
db dis_Point(Point ret){
db x = ret.x, y = ret.y;
if(k == inf) return fabs(x - p[0].x);
return fabs(k * x - y + b) / sqrt(1 + k * k);
}
};
// 点积
db dot(Point a, Point b) { return a.x * b.x + a.y * b.y; }
// 叉积
db cross(Point a, Point b) { return a.x * b.y - a.y * b.x; }
// 判断两直线是否相交,包含端点
bool intersection(const Line &l1, const Line &l2){
// 快速排斥实验
if(max(l1.p[0].x, l1.p[1].x) < min(l2.p[0].x, l2.p[1].x) || max(l2.p[0].x, l2.p[1].x) < min(l1.p[0].x, l1.p[1].x)) return false;
if(max(l1.p[0].y, l1.p[1].y) < min(l2.p[0].y, l2.p[1].y) || max(l2.p[0].y, l2.p[1].y) < min(l1.p[0].y, l1.p[1].y)) return false;
// 跨立实验
Vector L1 = {l1.p[1].x - l1.p[0].x, l1.p[1].y - l1.p[0].y}, L2 = {l2.p[1].x - l2.p[0].x, l2.p[1].y - l2.p[0].y};
Vector h = {l2.p[0].x - l1.p[0].x, l2.p[0].y - l1.p[0].y}, hh = {l2.p[1].x - l1.p[0].x, l2.p[1].y - l1.p[0].y};
if(cross(L1, h) * cross(L1, hh) > 0) return false;
h = {l1.p[0].x - l2.p[0].x, l1.p[0].y - l2.p[0].y}, hh = {l1.p[1].x - l2.p[0].x, l1.p[1].y - l2.p[0].y};
if(cross(L2, h) * cross(L2, hh) > 0) return false;
return true;
}
// 逆时针旋转
Point rorate_point(const Point &p, db A){
return Point{p.x * cos(A) - p.y * sin(A), p.x * sin(A) + p.y * cos(A)};
}
// 找直线交点
Point inter_point(const Line &x, const Line &y){
Point a = x.p[0], b = y.p[0], c = x.p[1], d = y.p[1];
double s1 = (c-a) ^ (b-a);
double s2 = (d-a) ^ (b-a);
return (c * s2 - d * s1) / (s2-s1);
}
原文:https://www.cnblogs.com/214txdy/p/14157756.html