hdu 4720 计算几何简单题

昨天用vim练了一道大水题,今天特地找了道稍难一点的题。不过也不是很难,简单的计算几何而已。练习用vim编码,用gdb调试,结果居然1A了,没调试。。。囧。。。

做法很简单,无非就是两种情况:①三个巫师构成一个钝角(极限情况是直角)三角形,那么所画的圆应该是钝角所对边为直径的圆;②三个巫师构成一个锐角三角形,那么所画的圆应该是三角形的外接圆。

就这样纸,上了点模板,代码如下:

/*

 * Author    : ben

 */

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <ctime>

#include <iostream>

#include <algorithm>

#include <queue>

#include <set>

#include <map>

#include <stack>

#include <string>

#include <vector>

#include <deque>

#include <list>

#include <functional>

#include <numeric>

#include <cctype>

using namespace std;

typedef struct MyPoint {

    double x;

    double y;

    MyPoint(double xx, double yy) {

        x = xx;

        y = yy;

    }

    MyPoint() {

        x = y = 0;

    }

}MyPoint;



typedef struct MyLine {

    MyPoint a, b;

} MyLine;

MyPoint intersection(MyLine u, MyLine v) {

    MyPoint 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;

}

//外心

MyPoint circumcenter(MyPoint a, MyPoint b, MyPoint c) {

    MyLine u, v;

    u.a.x = (a.x + b.x) / 2;

    u.a.y = (a.y + b.y) / 2;

    u.b.x = u.a.x - a.y + b.y;

    u.b.y = u.a.y + a.x - b.x;

    v.a.x = (a.x + c.x) / 2;

    v.a.y = (a.y + c.y) / 2;

    v.b.x = v.a.x - a.y + c.y;

    v.b.y = v.a.y + a.x - c.x;

    return intersection(u, v);

}

typedef struct MyCircle {

    MyPoint p;

    double r;

}MyCircle;



inline double dis(const MyPoint &p1, const MyPoint &p2) {

    return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y);

}



MyCircle getCircle(MyPoint ps[]) {

    MyCircle ret;

    ret.r = -1;

    for(int i = 0; i < 3; i++) {

        MyPoint a = ps[i % 3];

        MyPoint b = ps[(i + 1) % 3];

        MyPoint c = ps[(i + 2) % 3];

        double x = (a.x + b.x) / 2;

        double y = (a.y + b.y) / 2;

        MyPoint t = MyPoint(x, y);

        double r = dis(a, b) / 4;

        double temp = dis(t, c);

        if(temp <= r) {

            ret.r = r;

            ret.p = t;

        }

    }

    if(ret.r != -1) {

        return ret;

    }

    ret.p = circumcenter(ps[0], ps[1], ps[2]);

    ret.r = dis(ret.p, ps[0]);

    return ret;

}



int main() {

//    freopen("data.in", "r", stdin);

    int T;

      double x, y;

    scanf("%d", &T);

    MyPoint wizards[3];

    MyPoint muggle;

    for(int t = 1; t <= T; t++) {

        for(int i = 0; i < 3; i++) {

            scanf("%lf%lf", &x, &y);

            wizards[i] = MyPoint(x, y);

        }

        scanf("%lf%lf", &x, &y);

        muggle = MyPoint(x, y);

        MyCircle c = getCircle(wizards);

        double d = dis(muggle, c.p);

        if(d <= c.r) {

            printf("Case #%d: Danger\n", t);

        } else {

            printf("Case #%d: Safe\n", t);

        }

    }

    return 0;

}

 

你可能感兴趣的:(HDU)