POJ 1269 Intersecting Lines(線段相交,水題)

來源:互聯網
上載者:User

標籤:style   class   blog   code   http   tar   

Intersecting Lines


大意:給你兩條直線的座標,判斷兩條直線是否共線、平行、相交,若相交,求出交點。

思路:線段相交判斷、求交點的水題,沒什麼好說的。

struct Point{    double x, y;} ;struct Line{    Point a, b;} A, B;double xmult(Point p1, Point p2, Point p){    return (p1.x-p.x)*(p2.y-p.y)-(p1.y-p.y)*(p2.x-p.x);}bool parallel(Line u, Line v){    return zero((u.a.x-u.b.x)*(v.a.y-v.b.y)-(v.a.x-v.b.x)*(u.a.y-u.b.y));}Point intersection(Line u, Line v){    Point ret = u.a;    double t = ((u.a.x-v.a.x)*(v.a.y-v.b.y)-(u.a.y-v.a.y)*(v.a.x-v.b.x))/((u.a.x-u.b.x)*(v.a.y-v.b.y)-(u.a.y-u.b.y)*(v.a.x-v.b.x));    ret.x += (u.b.x-u.a.x)*t, ret.y += (u.b.y-u.a.y)*t;    return ret;}int T;void Solve(){    scanf("%d", &T);    printf("INTERSECTING LINES OUTPUT\n");    while(T--)    {        scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &A.a.x, &A.a.y, &A.b.x, &A.b.y, &B.a.x, &B.a.y, &B.b.x, &B.b.y);        if(parallel(A, B) && zero(xmult(A.a, B.a, B.b)))        {            printf("LINE\n");        }        else if(parallel(A, B))        {            printf("NONE\n");        }        else        {            Point t = intersection(A, B);            printf("POINT %.2f %.2f\n", t.x, t.y);        }    }    printf("END OF OUTPUT\n");}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.