uva 12304(圆的相关函数模板)

题意:要求解答6个关于圆的问题。
1.给出三角形坐标求外接圆
2.给出三角形坐标求内切圆
3.给出一个圆心和半径已知的圆,求过点(x,y)的所有和这个圆相切的直线
4.求所有和已知直线相切的过定点(x,y)的已知半径的圆的圆心
5.给出两个不平行的直线,求所有半径为r的同时和这两个直线相切的圆
6.给定两个相离的圆,求出所有和这两个圆外切的半径为r的圆。
题解:花了一天做这个,就当整理模板吧。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const double PI = acos(-1);
const int N = 100;
char str[N];
struct Point {
    double x, y;
    Point(double x = 0, double y = 0): x(x), y(y) {}
};
struct Circle {
    Point c;
    double r;
    Circle() {}
    Circle(Point c, double r = 0): c(c), r(r) {}
    Point point(double a) {
        return Point(c.x + cos(a) * r, c.y + sin(a) * c.y);
    }
};
//重载运算符
Point operator + (Point A, Point B) {
    return Point(A.x + B.x, A.y + B.y);
}
Point operator - (Point A, Point B) {
    return Point(A.x - B.x, A.y - B.y);
}
Point operator * (Point A, double p) {
    return Point(A.x * p, A.y * p);
}
Point operator / (Point A, double p) {
    return Point(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);
}
bool operator == (const Point& a, const Point& b) {
    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}
//计算点积的正负  负值夹角为钝角
int dcmp(double x) {
    if (fabs(x) < 1e-9)
        return 0;
    return x < 0 ? -1 : 1;
}
//计算点积 正负和夹角有关 |a||b|cosC
double Dot(Point A, Point B) {
    return A.x * B.x + A.y * B.y;
}
//计算叉积,也就是数量积 向量A和B组成的三角形的有向面积的两倍
//叉积等于0 三角形成为线段
double Cross(Point A, Point B) {
    return A.x * B.y - A.y * B.x;
}
//计算向量长度
double Length(Point A) {
    return sqrt(Dot(A, A));
}
//向量A旋转rad弧度,rad负值为顺时针旋转
Point Rotate(Point A, double rad) {
    return Point(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
}
//得到两直线交点 
Point GetLineIntersection(Point P, Point v, Point Q, Point w) {
    Point u = P - Q;
    double t = Cross(w, u) / Cross(v, w);
    return P + v * t;
}
//点p到线段AB的距离
double DistanceToSegment(Point p, Point A, Point B) {
    if (A == B)
        return Length(p - A);
    Point AB = B - A, AP = p - A, BP = p - B;
    if (dcmp(Dot(AB, AP)) < 0)
        return Length(AP);
    else if (dcmp(Dot(AB, BP)) > 0)
        return Length(BP);
    else
        return fabs(Cross(AB, AP)) / Length(AB);
}
//判断两个线段是否有交点(不包括端点)
bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2) {
    double c1 = Cross(a2 - a1, b1 - a1);
    double c2 = Cross(a2 - a1, b2 - a1);
    double c3 = Cross(b2 - b1, a1 - b1);
    double c4 = Cross(b2 - b1, a2 - b1);
    return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
}
//判断点p是否在线段a1--a2上(不包括端点)
bool OnSegment(Point p, Point a1, Point a2) {
    return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0;
}
//三角形外接圆
Circle CircumscribedCircle(Point a, Point b, Point c) {
    Point mid1 = Point((a.x + b.x) / 2, (a.y + b.y) / 2);
    Point mid2 = Point((b.x + c.x) / 2, (b.y + c.y) / 2);
    Point t1 = b - a;
    Point t2 = b - c;
    Point cc = GetLineIntersection(mid1, Point(t1.y, -t1.x), mid2, Point(t2.y, -t2.x));
    return Circle(cc, Length(cc - a));
}
//三角形内切圆
Circle InscribedCircle(Point a, Point b, Point c) {
    double l1 = Length(b - c);
    double l2 = Length(c - a);
    double l3 = Length(a - b);
    Point cc = (a * l1 + b * l2 + c * l3) / (l1 + l2 + l3);
    return Circle(cc, DistanceToSegment(cc, a, b));
}
//将角度规范化到0 <= x < 180
double solve(double x) {
    if (x >= 180)
        x -= 180;
    else if (x < 0)
        x = 180 + x;
    return x;
}
//过定点的圆的切线
int getTangents(Point p, Circle C, double* v) {
    Point temp; 
    Point u = C.c - p;
    double dist = Length(u);
    if (dist < C.r)
        return 0;
    else if (dcmp(dist - C.r) == 0) {
        temp = Rotate(u, PI / 2);
        v[0] = atan2(temp.y, temp.x) * 180 / PI;
        v[0] = solve(v[0]);
        return 1;
    }
    else {
        double ang = asin(C.r / dist);
        temp = Rotate(u, -ang);
        v[0] = atan2(temp.y, temp.x) * 180 / PI;
        temp = Rotate(u, ang);
        v[1] = atan2(temp.y, temp.x) * 180 / PI;
        v[0] = solve(v[0]); 
        v[1] = solve(v[1]);
        if (v[0] > v[1])
            swap(v[0], v[1]);
        return 2;
    }
}
//求经过点(x,y)的所有和直线(x1,y1)-(x2,y2)相切的半径为r的圆
int CircleThroughPointAndTangentToLineWithRadius(Point p, Point a, Point b, double rr, Point* v) {
    Point pcl = Point(p.x + a.y - b.y, p.y + b.x - a.x);
    Point pcz = GetLineIntersection(p, p - pcl, a, a - b);
    if (!dcmp(Cross(a - p, b - p))) {
        v[0] = p + (pcl - p) * (rr / Length(p - pcl));
        v[1] = p - (pcl - p) * (rr / Length(p - pcl));
        if (v[0].x > v[1].x || (fabs(v[0].x - v[1].x) < 1e-9 && v[0].y > v[1].y))
            swap(v[0], v[1]);
        return 2;
    }
    else if (!dcmp(rr - Length(p - pcz) * 0.5)) {
        v[0] = (p + pcz) * 0.5;
        return 1;
    }
    else if (dcmp(rr - Length(p - pcz) * 0.5) > 0) {
        double rcp = sqrt(rr * rr - (rr - Length(p - pcz)) * (rr - Length(p - pcz)));
        v[0] = pcz + (a - b) * (rcp / Length(a - b)) + (p - pcz) * (rr / Length(p - pcz));
        v[1] = pcz - (a - b) * (rcp / Length(a - b)) + (p - pcz) * (rr / Length(p - pcz));
        if (v[0].x > v[1].x || (fabs(v[0].x - v[1].x) < 1e-9 && v[0].y > v[1].y))
            swap(v[0], v[1]);
        return 2;   
    }
    else
        return 0;
}
//求和两条不平行直线相切的半径为r的圆
void CircleTangentToTwoWithRadius(Point a, Point b, Point c, Point d, double rr, Point* v) {
    Point t1 = Point(a.y - b.y, b.x - a.x);
    t1 = t1 * (rr / Length(t1));
    Point t2 = Point(c.y - d.y, d.x - c.x);
    t2 = t2 * (rr / Length(t2));
    v[0] = GetLineIntersection(a + t1, a - b, c + t2, c - d);
    v[1] = GetLineIntersection(a + t1, a - b, c - t2, c - d);
    v[2] = GetLineIntersection(a - t1, a - b, c + t2, c - d);
    v[3] = GetLineIntersection(a - t1, a - b, c - t2, c - d);
    sort(v, v + 4);
}
//求两个相离的圆的所有半径为r的外切圆
int CircleTangentToTwoDisjointCirclesWithRadius(Circle c1, Circle c2, double rr, Point* v) {
    if (!dcmp(rr * 2 + c1.r + c2.r - Length(c1.c - c2.c))) {
        v[0] = c1.c + (c2.c - c1.c) * ((rr + c1.r) / (rr * 2 + c1.r + c2.r));
        return 1;
    }
    else if (dcmp(rr * 2 + c1.r + c2.r - Length(c1.c - c2.c)) < 0)
        return 0;
    else {
        double p = (rr + c1.r + rr + c2.r + Length(c1.c - c2.c)) * 0.5;
        double S = sqrt(p * (p - rr - c1.r) * (p - rr - c2.r) * (p - Length(c1.c - c2.c))); //海伦公式
        double h = S * 2.0 / Length(c1.c - c2.c);
        double temp = sqrt((rr + c1.r) * (rr + c1.r) - h * h);
        Point mid = c1.c + (c2.c - c1.c) * (temp / (Length(c2.c - c1.c))); //对称圆心的中点

        double dis = sqrt((rr + c1.r) * (rr + c1.r) - Length(mid - c1.c) * Length(mid - c1.c)); //中点到所求圆心距离
        Point t = Point(c1.c.y - c2.c.y, c2.c.x - c1.c.x); //中点到所求圆心的向量
        v[0] = mid + t * (dis / Length(t));
        v[1] = mid - t * (dis / Length(t));
        sort(v, v + 2);
        return 2;
    }
}

int main() {
    while (scanf("%s", str) == 1) {
        if (str[4] == 'u') {
            Point a, b, c;
            scanf("%lf%lf%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y, &c.x, &c.y);
            Circle res = CircumscribedCircle(a, b, c);
            printf("(%lf,%lf,%lf)\n", res.c.x, res.c.y, res.r);
        }
        else if (str[0] == 'I') {
            Point a, b, c;
            scanf("%lf%lf%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y, &c.x, &c.y);
            Circle res = InscribedCircle(a, b, c);
            printf("(%lf,%lf,%lf)\n", res.c.x, res.c.y, res.r);
        }
        else if (str[0] == 'T') {
            Circle cc;
            Point p;
            scanf("%lf%lf%lf%lf%lf", &cc.c.x, &cc.c.y, &cc.r, &p.x, &p.y);
            double v[5];
            int cnt = getTangents(p, cc, v);
            printf("[");
            for (int i = 0; i < cnt; i++) {
                if (i == 0)
                    printf("%lf", v[0]);
                else
                    printf(",%lf", v[i]);
            }
            printf("]\n");
        }
        else if (str[7] == 'h') {
            double rr;
            Point p, a, b;
            scanf("%lf%lf%lf%lf%lf%lf%lf", &p.x, &p.y, &a.x, &a.y, &b.x, &b.y, &rr);
            Point v[5];
            int cnt = CircleThroughPointAndTangentToLineWithRadius(p, a, b, rr, v);
            printf("[");
            for (int i = 0; i < cnt; i++)
                if (i == 0)
                    printf("(%lf,%lf)", v[0].x, v[0].y);
                else
                    printf(",(%lf,%lf)", v[i].x, v[i].y);
            printf("]\n");
        }
        else if (str[18] == 'L') {
            double rr;
            Point a, b, c, d;
            scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y, &c.x, &c.y, &d.x, &d.y, &rr);
            Point v[5];
            CircleTangentToTwoWithRadius(a, b, c, d, rr, v);        
            printf("[(%lf,%lf),(%lf,%lf),(%lf,%lf),(%lf,%lf)]\n", v[0].x, v[0].y, v[1].x, v[1].y, v[2].x, v[2].y, v[3].x, v[3].y);
        }
        else {
            Circle c1, c2;
            double rr;
            Point v[5];
            scanf("%lf%lf%lf%lf%lf%lf%lf", &c1.c.x, &c1.c.y, &c1.r, &c2.c.x, &c2.c.y, &c2.r, &rr);
            int cnt = CircleTangentToTwoDisjointCirclesWithRadius(c1, c2, rr, v);   
            printf("[");
            for (int i = 0; i < cnt; i++)
                if (i == 0)
                    printf("(%lf,%lf)", v[0].x, v[0].y);
                else
                    printf(",(%lf,%lf)", v[i].x, v[i].y);
            printf("]\n");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值