码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1269 Intersecting Lines

时间:2014-04-27 21:23:59      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:blog   os   io   2014   问题   re   

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;
}

POJ 1269 Intersecting Lines,码迷,mamicode.com

POJ 1269 Intersecting Lines

标签:blog   os   io   2014   问题   re   

原文地址:http://blog.csdn.net/achiberx/article/details/24599245

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!