2013年11月13日 星期三

POJ 1269 Intersecting Lines

[計算幾何]
NOTE:
誤差((double)i/(double)j) > 誤差(i/(double)j)

#include<stdio.h>
#include<stdlib.h>
#include<cstring>
#include<algorithm>
using namespace std;
class Point
{
public:
    int x,y;
    Point(){x=0,y=0;}
    Point(int _x,int _y){x=_x,y=_y;}
    void set(int _x,int _y){x=_x,y=_y;}
    void Scan(){scanf("%d %d",&x,&y);}
};
class Line
{
public:
    int a,b,c;//ax+by+c=0;
    Line(){a=0,b=0,c=0;}
    void set(int _a,int _b,int _c)
    {
        a=_a,b=_b,c=_c;
    }
    void set(Point p1,Point p2)
    {
        set((p1.y-p2.y),(p2.x-p1.x),p1.x*p2.y-p1.y*p2.x);
    }
    void Scan()
    {
        Point a,b;
        a.Scan();b.Scan();
        set(a,b);
    }
};
int det(int a,int b,int c,int d){return a*d-b*c;}
int main()
{
    int T;scanf("%d",&T);
    printf("INTERSECTING LINES OUTPUT\n");
    while(T--)
    {
        Line L1,L2;   L1.Scan();L2.Scan();
        int D=det(L1.a,L1.b,L2.a,L2.b);
        int xx=det(L1.c,L1.b,L2.c,L2.b);
        int yy=det(L1.c,L1.a,L2.c,L2.a);
        if(D==0)
        {
            if((xx==0)&&(yy==0))
                printf("LINE\n");
            else
                printf("NONE\n");
        }
        else
            printf("POINT %.2f %.2f\n",-xx/(double)D,yy/(double)D);
    }
    printf("END OF OUTPUT\n");
    return 0;
}

沒有留言:

張貼留言