题意:要求解答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);elsereturn 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;   }elsereturn 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]);elseprintf(",%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);elseprintf(",(%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);elseprintf(",(%lf,%lf)", v[i].x, v[i].y);printf("]\n");}}return 0;
}

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

  1. 简单多边形与圆交面积模板

    1 /* 2 求多边形与圆相交的面积 3 */ 4 5 #include <iostream> 6 #include <cstdio> 7 #include <cmath ...

  2. 洛谷 :P5236 【模板】静态仙人掌(圆方树模板 + 仙人掌最短路)

    题意很简单,在仙人掌图上求两点的最短路. 做法:需要用到圆方树 先来看看什么是圆方树:圆方树,就是由仙人掌图转化而来,树上分两种点:圆点和方点,圆点是仙人掌图上的点,方点是由仙人掌的环转化而来. 由于 ...

  3. 牛客网暑期ACM多校训练营(第三场) J Distance to Work 计算几何求圆与多边形相交面积模板...

    链接:https://www.nowcoder.com/acm/contest/141/J 来源:牛客网 Eddy has graduated from college. Currently, he ...

  4. Halcon找圆系列(4)测量圆直径/半径的方法之暴力拟合法 vs 测量工具法

    今天要给大家分享一点关于Halcon测量圆直径(半径)的方法. 首先容我啰嗦两句:之所以要对这个看似很基础的问题进行探讨,主要原因有二,其一是这个问题确实困扰了我一段时间,当然这主要是由于我自己经验不 ...

  5. OpenMV4 H7 PLUS摄像头模板匹配

    使用OpenMV4 H7 PLUS摄像头进行矩形.三角形.圆三种形状的模板匹配: 参考: 官网:https://book.openmv.cc 函数库:https://docs.singtown.com ...

  6. 道路相遇【一般图的圆方树-广义圆方树】

    题目链接 刚开始拿到这道题的时候,先写了Tarjan缩点构树,没过样例,但是先把40分拿到了. 然后再想,就如同样例给出的那样,我们所定义的原点和方点,实际上在Tarjan直接缩点上并不能起到直接体现 ...

  7. Halcon 模板匹配

    halcon 匹配圆: 基于形状的匹配 目标:对圆进行模板匹配,方式Bolb 分析. 1.首先获取一张图片, read_image (Image, 'C:/Users/admin/Desktop/Pi ...

  8. 三代测序原理与数据文件简介(SMRT+Nanopore)

    三代测序原理与数据文件简介(SMRT+Nanopore) 一生雾梦 2019-12-03 20:48:42  1578  收藏 2 分类专栏: 前沿文献分析 文章标签: 三代测序(SMS) SMRT  ...

  9. Real-Time DNA Sequencing from Single Polymerase Molecules

    Real-Time DNA Sequencing from Single Polymerase Molecules  单聚合酶分子的实时DNA测序 Abstract ①We detected the ...

  10. SIFT四部曲之——高斯滤波

    版权声明:本文为博主原创文章,未经博主允许不得转载.博客不用于商业活动,博主对博客的使用,拥有最终解释权  本文为原创作品,未经本人同意,禁止转载,禁止用于商业用途!本人对博客使用拥有最终解释权  欢 ...

最新文章

  1. nestjs配置MySQL数据库,Nest.js 中的数据库操作
  2. 利用WSUS3.0进行补丁分发
  3. (转)Three challenges you’re going to face when building a chatbot
  4. 《PowerShell V3——SQL Server 2012数据库自动化运维权威指南》——2.13 创建视图...
  5. IT职场的一些处事之道
  6. java内存泄漏案例_寻找内存泄漏:一个案例研究
  7. 具有Stormpath和Spring Boot的OAuth 2.0令牌管理
  8. 由浅到深理解ROS(6)-坐标转换
  9. 字节跳动确认入局车联网:满足车载场景的用户体验
  10. C和指针 第五章 位数组
  11. 华为Mate8 NFC 时好时坏,怎么解决呢?
  12. sklearn 中GBDT的损失函数
  13. Convirt 2.0 更新到 2.1版本
  14. NonEmpty和Hierarchize嵌套的bug
  15. up能不能应急启动计算机,启动盘 提升电脑的性能
  16. 【BUAA_CO_LAB】p5p6碎碎念
  17. plt图像保存到pdf文件
  18. P问题,NP问题,NP完全问题,NP难问题
  19. 关于AutoCAD软件的PDF打印机的问题
  20. Compiling for iOS 10.0, but module ‘xxx‘ has a minimum deployment target of iOS 12.0

热门文章

  1. pvr格式的用什么打开_cocos2d 查看pvr图片的详细格式
  2. 云计算是什么?云计算基础概念讲解
  3. 记录一次 JS 解密去混淆的经历 -- 如何破解加密的 JS 代码(一)
  4. 同一个浏览器多用户同时登录的解决方案
  5. LOJ.2863.[IOI2018]组合动作(交互)
  6. 循序渐进Linux目录
  7. longhorn介绍
  8. arctanx麦克劳林公式推导过程_罗德里格斯公式推导
  9. 安卓手机格式化怎么弄_安卓手机怎么格式化?
  10. 2021最新 从零开始搭建terraria(泰拉瑞亚)云服务器