PS: 求解直线相交问题,用叉积和点积判断,叉积和正弦函数相关,点积和余弦函数变化相关。具体参见代码。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <cmath> using namespace std; const double eps = 1e-10; int dcmp(double x) { if(fabs(x)<eps) return 0; else return x > 0 ? 1 : -1; } struct point { double x, y; point(double x = 0, double y = 0):x(x), y(y) {} }; point operator - (point A, point B) { return point(A.x-B.x, A.y-B.y); } point operator * (point A, double p) { return point(A.x*p, A.y*p); } point operator / (point A, double p) { return point(A.x/p, A.y/p); } double det(point A, point B) { return A.x*B.y - A.y*B.x; } struct line{ point a, b; }; bool parallel(line t1, line t2) { return !dcmp(det(t1.a-t1.b, t2.a-t2.b)); } bool work(line t1, line t2, point *res) { if(parallel(t1,t2)) return false; double s1 = det(t1.a-t2.a, t2.b-t2.a); double s2 = det(t1.b-t2.a, t2.b-t2.a); *res = (t1.b*s1-t1.a*s2)/(s1-s2); return true; } bool PointOnLine(point p, point s, point t) { return dcmp(det(s-p, t-p))==0; } int main() { int T; line t1, t2; point res; scanf("%d", &T); bool first = false; while(T--) { scanf("%lf%lf%lf%lf", &t1.a.x, &t1.a.y, &t1.b.x, &t1.b.y); scanf("%lf%lf%lf%lf", &t2.a.x, &t2.a.y, &t2.b.x, &t2.b.y); bool result = work(t1, t2, &res); if(!first) { first = true; printf("INTERSECTING LINES OUTPUT\n"); } if(result) { printf("POINT %.2f %.2f\n", res.x, res.y); } else { bool mid = PointOnLine(t1.a, t2.a, t2.b); if(mid) printf("LINE\n"); else printf("NONE\n"); } } printf("END OF OUTPUT\n"); return 0; }