URAL-1103. Pencils and Circles
程序员文章站
2022-04-01 13:32:15
...
1、知识点:三角形外接圆
2、思路:外三层循环遍历组圆方式,内一层循环用于判断。重点在于计算外接圆中心坐标公式。先计算P1P2,P1P3的中垂线方程,然后联立求交点即可。
/*用途:提交
**版本:2
**说明:AC
*/
#include <stdio.h>
#include <math.h>
#define MAXN 5000
struct Point{
double x;
double y;
};
Point poi_arr[MAXN];
void calcu_cen(Point p1, Point p2, Point p3, Point &cen); //计算三点外接圆圆心,引用带回
double get_dis(Point p1, Point p2); //计算两点间距离
int main()
{
int n;
scanf("%d", &n);
for(int i=0; i<n; i++)
scanf("%lf%lf", &(poi_arr[i].x), &(poi_arr[i].y));
for(int i=0; i<n; i++)
for(int j=i+1; j<n; j++)
for(int k=j+1; k<n; k++){
Point poi_cen; //圆心
double r;
int ins_num, out_num; //圆内、外点数目
ins_num = out_num = 0;
calcu_cen(poi_arr[i], poi_arr[j], poi_arr[k], poi_cen);
r = get_dis(poi_arr[i], poi_cen); //计算圆的半径
for(int l=0; l<n; l++){
if(l==i || l==j || l==k) //如果该点在圆上
continue;
double dis = get_dis(poi_arr[l], poi_cen); //计算该点与圆心距离
if(dis < r) //如果是内部点
ins_num++;
else //如果是外部点
out_num++;
}
if(ins_num == out_num){ //符合要求,打印返回
printf("%d %d\n", (int)poi_arr[i].x, (int)poi_arr[i].y);
printf("%d %d\n", (int)poi_arr[j].x, (int)poi_arr[j].y);
printf("%d %d\n", (int)poi_arr[k].x, (int)poi_arr[k].y);
return 0;
}
}
printf("No solution\n");
return 0;
}
void calcu_cen(Point p1, Point p2, Point p3, Point &cen)
{
double mol, den, cen_x, cen_y;
mol = (pow(p1.x, 2)-pow(p2.x, 2) + pow(p1.y, 2)-pow(p2.y, 2)) * (p1.y-p3.y) -
(pow(p1.x, 2)-pow(p3.x, 2) + pow(p1.y, 2)-pow(p3.y, 2)) * (p1.y-p2.y);
den = 2 * ((p1.y-p3.y)*(p1.x-p2.x) - (p1.y-p2.y)*(p1.x-p3.x));
cen.x = mol / den;
mol = (pow(p1.x, 2)-pow(p2.x, 2) + pow(p1.y, 2)-pow(p2.y, 2)) * (p1.x-p3.x) -
(pow(p1.x, 2)-pow(p3.x, 2) + pow(p1.y, 2)-pow(p3.y, 2)) * (p1.x-p2.x);
den = 2 * ((p1.y-p2.y)*(p1.x-p3.x) - (p1.y-p3.y)*(p1.x-p2.x));
cen.y = mol / den;
}
double get_dis(Point p1, Point p2)
{
double dis = sqrt(pow((p1.x-p2.x), 2) + pow((p1.y-p2.y), 2));
return dis;
}
推荐阅读
-
CF-Educational Codeforces Round 44 (Rated for Div. 2)-E-Pencils and Boxes
-
Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes
-
UVA 375 - Inscribed Circles and Isosceles Triangles
-
【CodeChef NTHCIR】Rohith and Circles(线性递推)(圆的反演)
-
URAL-1103. Pencils and Circles
-
CodeForces 372 E.Drawing Circles is Fun(计算几何+dp)
-
In-circles Again (计算几何)
-
[zoj1608]Two Circles and a Rectangle--计算几何
-
问题 B: 【计算几何】Two Circles and a Rectangle
-
[ZOJ 1608] Two Circles and a Rectangle(计算几何初步)