UVa 378 - Intersecting Lines

來源:互聯網
上載者:User

標籤:

題目:給你平面上的兩條直線,判斷兩直線關係,平行,重合,相交,如果相交求交點。


公式:p1xp2=x1*y2-x2*y1(外積)

判斷q是否線上段p1-p2上面,根據(p1-q)x(p2-q)=0來判斷q是否在直線p1-p2上。

兩直線平行:(p1-p2)x(q1-q2)=0,為什嗎?把兩條直線的斜率寫出來並且令他們相等,這個等式就是這個公式。

p1-p2,q1-q2的交點:

(x,y)=p1+(p2-p1)*((q2-q1)x(q1-p1)/((q2-q1)x(p2-p1)));

推理:把p1-p2直線寫成點p1+t(p2-p1),然後就是點和q1-q2直線了,最後計算得出上面的。


#include <iostream>#include<cstdio>#include<cmath>using namespace std;#define EPS 1e-10struct point{    double a,b;    point(){}    point(double a,double b):a(a),b(b){}    point operator +(point p)    {        return point(p.a+a,p.b+b);    }    point operator -(point p)    {        return point(a-p.a,b-p.b);    }    point operator *(double p)    {        return point(a*p,b*p);    }    double dot(point p)//內積    {        return (p.a*a+p.b*b);    }    double det(point p)//外積    {        return (a*p.b-b*p.a);    }};//判斷q是否在直線p1-p2上bool on_str(point p1,point p2,point q){    return (p1-q).det(p2-q)==0;//&&(p1-q).dot(p2-q)<=0;}//求兩直線交點point intersection(point p1,point p2,point q1,point q2){    return p1+(p2-p1)*((q2-q1).det(q1-p1)/(q2-q1).det(p2-p1));}point p1,p2,q1,q2;int main(){    int n;    scanf("%d",&n);    printf("INTERSECTING LINES OUTPUT\n");    while(n--)    {        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&p1.a,&p1.b,&p2.a,&p2.b,&q1.a,&q1.b,&q2.a,&q2.b);         if(abs((p1-p2).det(q1-q2))<EPS)//判斷外積是否為0         {             if(on_str(p1,p2,q1))                printf("LINE\n");             else             printf("NONE\n");         }         else         {             point r=intersection(p1,p2,q1,q2);             printf("POINT %.2f %.2f\n",r.a,r.b);         }    }    printf("END OF OUTPUT\n");    return 0;}


UVa 378 - Intersecting Lines

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.