POJ 1269

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值