zoukankan      html  css  js  c++  java
  • POJ 1269 /// 判断两条直线的位置关系

    题目大意:

    t个测试用例 每次给出一对直线的两点

    判断直线的相对关系

    平行输出NODE 重合输出LINE 相交输出POINT和交点坐标

    1.直线平行 两向量叉积为0

    2.求两直线ab与cd交点

    设直线ab上点为 a+(b-a)t,t为变量

    交点需满足在直线cd上 则(d-c)*(a+t(b-a)-c)=0(外积)

    分解为加减式 将t放在等号左边 其他放在右边

    化简推导得t=(d-c)*(c-a)/(d-c)*(b-a)

    则交点为a+(b-a)*((d-c)*(c-a)/(d-c)*(b-a))

    #include <cstdio>
    #include <algorithm>
    #include <string.h>
    #include <cmath>
    using namespace std;
    
    const double eps=1e-10;
    double add(double a,double b) {
        if(abs(a+b)<eps*(abs(a)+abs(b))) return 0;
        return a+b;
    }
    struct P {
        double x,y;
        P(){};
        P(double _x,double _y):x(_x),y(_y){}
        P operator - (P p) {
            return P(add(x,-p.x),add(y,-p.y)); }
        P operator + (P p) {
            return P(add(x,p.x),add(y,p.y)); }
        P operator * (double d) {
            return P(x*d,y*d); }
        double dot(P p) {
            return add(x*p.x,y*p.y); }
        double det(P p) {
            return add(x*p.y,-y*p.x); }
    }p;
    struct L {
        P a,b;
        L(){};
        L(P _a,P _b):a(_a),b(_b){};
    }l1,l2;
    int n;
    
    P ins(P a,P b,P c,P d)  {
        return a+(b-a)*((d-c).det(c-a)/(d-c).det(b-a));
    }
    int solve()
    {
        if((l1.a-l1.b).det(l2.a-l2.b)==0) { // 平行
            return (l1.a-l2.b).det(l1.b-l2.b)==0;
        } // 若l2有一点在l1上 就是重合
        p=ins(l1.a,l1.b,l2.a,l2.b); // 相交求交点
        return -1; 
    }
    
    int main()
    {
        while(~scanf("%d",&n)) {
            printf("INTERSECTING LINES OUTPUT
    ");
            for(int i=0;i<n;i++) {
                scanf("%lf%lf%lf%lf"
                      ,&l1.a.x,&l1.a.y,&l1.b.x,&l1.b.y);
                scanf("%lf%lf%lf%lf"
                      ,&l2.a.x,&l2.a.y,&l2.b.x,&l2.b.y);
                int t=solve();
                if(t==0) printf("NONE
    ");
                else if(t==1) printf("LINE
    ");
                else printf("POINT %.2f %.2f
    ",p.x,p.y);
            }
            printf("END OF OUTPUT
    ");
        }
    }
    View Code
  • 相关阅读:
    Leetcode 92. Reverse Linked List II
    Leetcode 206. Reverse Linked List
    Leetcode 763. Partition Labels
    Leetcode 746. Min Cost Climbing Stairs
    Leetcode 759. Employee Free Time
    Leetcode 763. Partition Labels
    搭建数据仓库第09篇:物理建模
    Python进阶篇:Socket多线程
    Python进阶篇:文件系统的操作
    搭建数据仓库第08篇:逻辑建模–5–维度建模核心之一致性维度2
  • 原文地址:https://www.cnblogs.com/zquzjx/p/9612437.html
Copyright © 2011-2022 走看看