题意: 二维平面,给两条线段,判断形成的直线是否重合,或是相交于一点,或是不相交。
解法: 简单几何。
重合: 叉积为0,且一条线段的一个端点到另一条直线的距离为0
不相交: 不满足重合的情况下叉积为0
相交于一点: 直线相交的模板
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #define pi acos(-1.0) #define eps 1e-8 using namespace std; #define N 100017 struct Point{ double x,y; Point(double x=0, double y=0):x(x),y(y) {} void input() { scanf("%lf%lf",&x,&y); } }; typedef Point Vector; struct Circle{ Point c; double r; Circle(){} Circle(Point c,double r):c(c),r(r) {} Point point(double a) { return Point(c.x + cos(a)*r, c.y + sin(a)*r); } void input() { scanf("%lf%lf%lf",&c.x,&c.y,&r); } }; struct Line{ Point p; Vector v; double ang; Line(){} Line(Point p, Vector v):p(p),v(v) { ang = atan2(v.y,v.x); } Point point(double t) { return Point(p.x + t*v.x, p.y + t*v.y); } bool operator < (const Line &L)const { return ang < L.ang; } }; int dcmp(double x) { if(x < -eps) return -1; if(x > eps) return 1; return 0; } template <class T> T sqr(T x) { return x * x;} Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); } Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); } Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); } Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); } bool operator < (const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } bool operator >= (const Point& a, const Point& b) { return a.x >= b.x && a.y >= b.y; } bool operator <= (const Point& a, const Point& b) { return a.x <= b.x && a.y <= b.y; } bool operator == (const Point& a, const Point& b) { return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0; } double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; } double Length(Vector A) { return sqrt(Dot(A, A)); } double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); } double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } Vector VectorUnit(Vector x){ return x / Length(x);} Vector Normal(Vector x) { return Point(-x.y, x.x) / Length(x);} double angle(Vector v) { return atan2(v.y, v.x); } bool OnSegment(Point P, Point A, Point B) { return dcmp(Cross(A-P,B-P)) == 0 && dcmp(Dot(A-P,B-P)) < 0; } double DistanceToSeg(Point P, Point A, Point B) { if(A == B) return Length(P-A); Vector v1 = B-A, v2 = P-A, v3 = P-B; if(dcmp(Dot(v1, v2)) < 0) return Length(v2); if(dcmp(Dot(v1, v3)) > 0) return Length(v3); return fabs(Cross(v1, v2)) / Length(v1); } double DistanceToLine(Point P, Point A, Point B){ Vector v1 = B-A, v2 = P-A; return fabs(Cross(v1,v2)) / Length(v1); } Point GetLineIntersection(Line A, Line B){ Vector u = A.p - B.p; double t = Cross(B.v, u) / Cross(A.v, B.v); return A.p + A.v*t; } //data segment //data ends int main() { Point A,B,C,D; int n,i,j; scanf("%d",&n); { puts("INTERSECTING LINES OUTPUT"); for(i=1;i<=n;i++) { scanf("%lf%lf%lf%lf",&A.x,&A.y,&B.x,&B.y); scanf("%lf%lf%lf%lf",&C.x,&C.y,&D.x,&D.y); Line L1 = Line(A,B-A); Line L2 = Line(C,D-C); if(dcmp(Cross(L1.v,L2.v)) == 0 && dcmp(DistanceToLine(A,C,D)) == 0) puts("LINE"); else if(dcmp(Cross(L1.v,L2.v)) == 0) puts("NONE"); else printf("POINT %.2f %.2f\n",GetLineIntersection(L1,L2).x,GetLineIntersection(L1,L2).y); } puts("END OF OUTPUT"); } return 0; }