```
const double eps = 1e-8;
int n;
struct Point {
double x, y;
Point() {}
static double c_eps(double p) {
if(fabs(p) < eps) return 0;
return p;
}
void cin(){
scanf("%lf%lf", &x, &y);
}
Point(double x, double y): x(x), y(y) {}
Point operator + (const Point &p) const {
return Point(x + p.x, y + p.y);
}
Point operator - (const Point &p) const {
return Point(x - p.x, y - p.y);
}
Point operator * (double d) {
return Point(x * d, y * d);
}
//用于求解点在向量的哪一侧
double dot(Point p) {
return x * p.x + y * p.y;
}
//a.det(b) == 0表示平行
double det(Point p) {
return x * p.y - y * p.x;
}
double dist(Point p) {
return c_eps(sqrt((*this - p).dot(*this - p)));
}
void print() {
printf("point : %.3f, %.3f\n", x, y);
}
};
struct Line {
Point s, e;
Line() {}
Line(Point s, Point e): s(s), e(e) {}
void cin() {
s.cin();
e.cin();
}
//判断线段平行
bool px(Line p){
return Point::c_eps((e - s).det(p.e - p.s)) == 0;
}
//判断点在线段上
bool on_seg(Point p) {
return Point::c_eps((s - p).det(e - p)) == 0 && Point::c_eps((s - p).dot(e - p)) <= 0;
}
//计算出两直线的交点
Point stra_stra_intersection(Line p) {
return s + (e - s) * ((p.e - p.s).det(p.s - s) / (p.e - p.s).det(e - s));
}
//判断直线是否与线段相交
bool is_stra_seg_intersection_1(Line p) {
return (s - p.s).det(e - p.s) * (s - p.e).det(e - p.e) <= eps;
}
//存在严重的精度问题,待测试中
bool is_stra_seg_intersection_2(Line p) {
if(px(p) && (Line(s, p.s).on_seg(e) || Line(s, p.e).on_seg(e) || Line(s, p.s).on_seg(p.e) || Line(s, p.e).on_seg(p.s) )) {
return true;
}
if(px(p)) return false;
Point r = stra_stra_intersection(p);
//r.print();
return p.on_seg(r);
}
//判断线段和线段是否相交
bool is_seg_seg_intersection(Line p) {
if(px(p)) {
return on_seg(p.e) || on_seg(p.s) || p.on_seg(e) || p.on_seg(s);
} else {
Point r = stra_stra_intersection(p);
return on_seg(r) && p.on_seg(r);
}
}
void print(){
printf("Line:\n");
s.print();
e.print();
}
};
几何模板
最新推荐文章于 2024-07-10 10:51:06 发布