poj 1269 Intersecting Lines[直线的关系]

来源:互联网 发布:那些引擎源码能用 编辑:程序博客网 时间:2024/05/20 06:28

题目链接: poj 1269

 题目的意思很是简单,两条直线的关系,平行,共线,或者给出交点。

Code:

#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const double eps = 1e-8;struct POINT{    double x, y;};double cross(POINT o, POINT a, POINT b){    return (a.x - o.x) * (b.y - o.y) - (b.x - o.x) * (a.y - o.y);}double cross(POINT a, POINT b, POINT c, POINT d){    return (b.x - a.x) * (d.y - c.y) - (d.x - c.x) * (b.y - a.y);}double f(POINT a, POINT b, double x){    return x * (b.y - a.y) + a.y * b.x - a.x * b.y;}void TheRelationshipOfTwoLine(POINT p1, POINT p2, POINT p3,POINT p4){ //p1p2,p3p4    if(fabs(cross(p1, p2, p3)) < eps && fabs(cross(p1, p2, p4)) < eps){// line        puts("LINE");    }    else if(fabs(cross(p1, p2, p3, p4)) < eps){// //        puts("NONE");    }    else {        if(p2.x == p1.x && p3.x != p4.x){// special judge p1p2 |x            printf("POINT %.2f %.2f\n", p2.x, f(p3, p4, p2.x) / (p4.x - p3.x));        }        else if(p3.x == p4.x && p1.x != p2.x){ // special judge p3p4 |x            printf("POINT %.2f %.2f\n", p3.x, f(p1, p2, p3.x) / (p2.x - p1.x));        }        else{            double k1 = (p2.y - p1.y) / (p2.x - p1.x), k2 = (p4.y - p3.y) / (p4.x - p3.x);            double x = (p1.x * k1 - p3.x * k2 + p3.y - p1.y) / (k1 - k2);            double y = k1 * (x - p1.x) + p1.y;            printf("POINT %.2f %.2f\n",x, y);        }    }}int main(){    int T;    scanf("%d", &T);    puts("INTERSECTING LINES OUTPUT");    while(T --){        POINT p1, p2, p3, p4;        scanf("%lf %lf %lf %lf %lf %lf %lf %lf",&p1.x, &p1.y, &p2.x, &p2.y, &p3.x, &p3.y, &p4.x, &p4.y);        TheRelationshipOfTwoLine(p1, p2, p3, p4);    }    puts("END OF OUTPUT");    return 0;<span style="font-size:18px;">}</span>

留下小小的模板吧。。。!

代码比较优美的又写了一遍,不过这次用到的不是求斜率来求解交点。而是应用到了定比分点公式来进行求解。。。

Code:

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>using namespace std;const int N = 20;const double eps = 1e-8;const double pi = acos(-1);//点struct POINT{    double x, y;    POINT(){ }    POINT(double a, double b){        x = a;        y = b;    }};//直线struct Line{    POINT a, b;    Line() {}    Line(POINT x, POINT y){        a = x;        b = y;    }}l1, l2;//叉乘double cross(POINT o, POINT a, POINT b){    return (a.x - o.x) * (b.y - o.y) - (b.x - o.x) * (a.y - o.y);}int Line_cross(Line l1, Line l2){    //两条直线平行    if(fabs(cross(l1.a, l2.a, l2.b) - cross(l1.b, l2.a, l2.b)) < eps && fabs(cross(l1.a, l2.a, l2.b)) > eps) return 0;    //重合..    if(fabs(cross(l1.a, l2.a, l2.b)) < eps && fabs(cross(l1.b, l2.a, l2.b)) < eps) return 2;    //有交点///    return 1;}POINT Inter(Line l1, Line l2){    double k = fabs(cross(l1.a, l2.a, l2.b)) / fabs(cross(l1.b, l2.a, l2.b));    return POINT((l1.a.x + l1.b.x * k) / (1 + k), (l1.a.y + l1.b.y * k) / (1 + k));}int main(){    int n;    while(~scanf("%d", &n)){        puts("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 cnt = Line_cross(l1, l2);            if(cnt == 0){                puts("NONE");            }            else if(cnt == 1){                POINT ans = Inter(l1, l2);                printf("POINT %.2f %.2f\n", ans.x, ans.y);            }            else {                puts("LINE");            }        }        puts("END OF OUTPUT");    }    return 0;}





0 0
原创粉丝点击