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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值