POJ 1269

来源:互联网 发布:笛卡尔心形线 c语言 编辑:程序博客网 时间:2024/05/16 12:38

code:

#include <cmath>#include <cstdio>#include <utility>using namespace std;typedef double ld;int n;struct Point{    int x, y;    void read(){        scanf("%d%d", &x, &y);    }}s1, e1, s2, e2;typedef pair<pair<int, int>, int> Line;Line a, b;bool operator == (pair<int, int> &a, pair<int, int> &b){    return a.first == b.first && a.second == b.second;}inline int gcd(int a, int b){return b ? gcd(b, a % b) : a;}Line getLine(Point &a, Point &b){    if(a.x == b.x) return make_pair(make_pair(0, 0), a.x);    if(a.y == b.y) return make_pair(make_pair(1, 0), a.y);    int dx = a.x - b.x, dy = a.y - b.y;    if(dx < 0){        dx *= -1;        dy *= -1;    }    int d = gcd(abs(dx), abs(dy));    dx /= d;    dy /= d;    int v = (a.y % dy + dy) % dy;    v = a.x + (v - a.y) / dy * dx;    return make_pair(make_pair(dx, dy), v);}int main(){    puts("INTERSECTING LINES OUTPUT");    scanf("%d", &n);    while(n--){        s1.read();        e1.read();        s2.read();        e2.read();        a = getLine(s1, e1);        b = getLine(s2, e2);        if(a.first == b.first){            if(a.second == b.second) puts("LINE");            else puts("NONE");        }else{            if(a.first == make_pair(0, 0)){                ld k2 = (ld)b.first.second / b.first.first;                ld b2 = (ld)s2.y - k2 * s2.x;                printf("POINT %.2lf %.2lf\n", (ld)s1.x, s1.x * k2 + b2);            }else{                if(b.first == make_pair(0, 0)){                ld k1 = (ld)a.first.second / a.first.first;                ld b1 = (ld)s1.y - k1 * s1.x;                printf("POINT %.2lf %.2lf\n", (ld)s2.x, s2.x * k1 + b1);                }                else{                    ld k1 = (ld)a.first.second / a.first.first;                    ld b1 = (ld)s1.y - k1 * s1.x;                    ld k2 = (ld)b.first.second / b.first.first;                    ld b2 = (ld)s2.y - k2 * s2.x;                    ld x = (b2 - b1) / (k1 - k2);                    printf("POINT %.2lf %.2lf\n", x, x * k1 + b1);                }            }        }    }    puts("END OF OUTPUT");    return 0;}
0 0
原创粉丝点击