首页 > 其他 > 详细

UVA 438 - The Circumference of the Circle(计算几何)

时间:2015-03-26 17:48:21      阅读:262      评论:0      收藏:0      [点我收藏+]

给三点,求外切圆周长

代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

struct Point {
    double x, y;
    Point() {}
    Point(double x, double y) {
        this->x = x;
        this->y = y;
    }
    void read() {
        scanf("%lf%lf", &x, &y);
    }
};

typedef Point Vector;

Vector operator + (Vector A, Vector B) {
    return Vector(A.x + B.x, A.y + B.y);
}

Vector operator - (Vector A, Vector B) {
    return Vector(A.x - B.x, A.y - B.y);
}

Vector operator * (Vector A, double p) {
    return Vector(A.x * p, A.y * p);
}

Vector operator / (Vector A, double p) {
    return Vector(A.x / p, A.y / p);
}

bool operator < (const Point& a, const Point& b) {
    return a.x < b.x || (a.x == b.x && a.y < b.y);
}

const double eps = 1e-8;
const double PI = acos(-1.0);

int dcmp(double x) {
    if (fabs(x) < eps) return 0;
    else return x < 0 ? -1 : 1;
}

bool operator == (const Point& a, const Point& b) {
    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}

double Dot(Vector A, Vector B) {return A.x * B.x + A.y * B.y;} //点积
double Length(Vector A) {return sqrt(Dot(A, A));} //向量的模
double Angle(Vector A, Vector B) {return acos(Dot(A, B) / Length(A) / Length(B));} //向量夹角
double Cross(Vector A, Vector B) {return A.x * B.y - A.y * B.x;} //叉积
double Area2(Point A, Point B, Point C) {return Cross(B - A, C - A);} //有向面积
double angle(Vector v) {return atan2(v.y, v.x);}

struct Circle {
    Point c;
    double r;
    Circle(Point c, double r) {
        this->c = c;
        this->r = r;
    }
    Point point(double a) {
        return Point(c.x + cos(a) * r, c.y + sin(a) * r);
    }
};

Circle CircumscribedCircle(Point p1, Point p2, Point p3) {
    double Bx = p2.x - p1.x, By = p2.y - p1.y;
    double Cx = p3.x - p1.x, Cy = p3.y - p1.y;
    double D = 2 * (Bx * Cy - By * Cx);
    double cx = (Cy * (Bx * Bx + By * By) - By * (Cx * Cx + Cy * Cy)) / D + p1.x;
    double cy = (Bx * (Cx * Cx + Cy * Cy) - Cx * (Bx * Bx + By * By)) / D + p1.y;
    Point p = Point(cx, cy);
    return Circle(p, Length(p1 - p));
}

Point p[3];

int main() {
    while (~scanf("%lf%lf", &p[0].x, &p[0].y)) {
        p[1].read(); p[2].read();
        Circle ans = CircumscribedCircle(p[0], p[1], p[2]);
        printf("%.2f\n", ans.r * 2 * PI);
    }
    return 0;
}


UVA 438 - The Circumference of the Circle(计算几何)

原文:http://blog.csdn.net/accelerator_/article/details/44650967

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!