POJ 1269 Intersecting Lines(直线相交判断,求交点)

来源:互联网 发布:数据库添加数据 编辑:程序博客网 时间:2024/05/19 02:05

题意 : 就是让你求直线是否相交,若相交求出交点

题解 :先要有一个意识就是求直线方程不是很麻烦,我就是被这个给坑了,会求直线方程这个问题就解决了 剩下的就是解方程了 (注意斜率不存在的情况就好了)


#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <cmath>using namespace std;//const double eps = 1e-8;const int maxn = 15;const double eps = 1e-8;struct line {    double x1,y1,x2,y2;}L[maxn];int n;double corss (double x1,double y1,double x2,double y2) {    return x1 * y2 - x2 * y1;}void solve (line a,double & k,double & b) {    int x1 = a.x1,x2 = a.x2,y1 = a.y1,y2 = a.y2;    k = (y1 - y2) / (double) (x1 - x2);    b = y2 - k * x2;}int main () {    ios_base :: sync_with_stdio(false);    cin >> n;        double k1,k2,b1,b2,px,py;        cout << "INTERSECTING LINES OUTPUT" << endl;        for (int i = 1;i <= n; ++ i) {            cin >> L[1].x1 >> L[1].y1 >> L[1].x2 >> L[1].y2;            cin >> L[2].x1 >> L[2].y1 >> L[2].x2 >> L[2].y2;            if (fabs(corss (L[1].x2 - L[1].x1,L[1].y2 - L[1].y1,L[2].x2 - L[2].x1,L[2].y2 - L[2].y1)) < eps) {                if (fabs(corss(L[1].x1 - L[2].x1,L[1].y1 - L[2].y1,L[1].x1 - L[2].x2,L[1].y1 - L[2].y2)) < eps) {                    cout << "LINE" << endl;                }                else {                    cout << "NONE" << endl;                }            }            else {                cout << "POINT" << ' ';                if (fabs(L[1].x1 - L[1].x2) < eps) {                    solve(L[2],k2,b2);                    px = L[1].x1;                    py = k2 * px + b2;                    printf ("%.2f %.2f",px,py);                }                else if (fabs(L[2].x1 - L[2].x2) < eps) {                    solve(L[1], k1, b1);                    px = L[2].x1;                    py = k1 * px + b1;                    printf ("%.2f %.2f",px,py);                }                else {                    solve(L[1], k1, b1);                    solve(L[2], k2, b2);                    px = (b1 - b2) / double (k2 - k1);                    py = k1 * px + b1;                    printf ("%.2f %.2f",px,py);                }                cout << endl;            }        }    cout << "END OF OUTPUT";    return 0;}
阅读全文
0 0
原创粉丝点击