首页 > 代码库 > POJ 1269 Intersecting Lines
POJ 1269 Intersecting Lines
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; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。