POJ 1329 Circle Through Three Points

POJ_1329

    题目主要就是在求三角形的外心,即各边中垂线的交点,推导出公式后直接计算即可。

#include<stdio.h>
#include<string.h>
#include<math.h>
#define zero 1e-8
double x3, y3, x4, y4, x5, y5;
double dcmp(double x)
{
if(fabs(x) < zero)
return 0;
if(x < 0)
return -1;
return 1;
}
char sign(double x)
{
return x < 0 ? '-' : '+';
}
void solve()
{
double x1, y1, x2, y2, k1, k2, x, y, r, a1, a2, a3;
if(dcmp(y3 - y4) == 0)
{
x1 = (x3 + x4) / 2, y1 = y3;
k2 = (x4 - x5) / (y5 - y4);
x2 = (x4 + x5) / 2, y2 = (y4 + y5) / 2;
x = x1, y = k2 * (x1 - x2) + y2;
}
else
{
if(dcmp(y4 - y5) == 0)
{
x2 = (x4 + x5) / 2, y2 = y4;
k1 = (x3 - x4) / (y4 - y3);
x1 = (x3 + x4) / 2, y1 = (y3 + y4) / 2;
x = x2, y = k1 * (x2 - x1) + y1;
}
else
{
x1 = (x3 + x4) / 2, y1 = (y3 + y4) / 2;
k1 = (x3 - x4) / (y4 - y3);
x2 = (x4 + x5) / 2, y2 = (y4 + y5) / 2;
k2 = (x4 - x5) / (y5 - y4);
x = (y2 - y1 + k1 * x1 - k2 * x2) / (k1 - k2);
y = k1 * (x - x1) + y1;
}
}
r = sqrt((x - x3) * (x - x3) + (y - y3) * (y - y3));
printf("(x %c %.3lf)^2 + (y %c %.3lf)^2 = %.3lf^2\n", sign(-x), fabs(x), sign(-y), fabs(y), r);
a1 = (-2) * x, a2 = (-2) * y, a3 = x * x + y * y - r * r;
printf("x^2 + y^2 %c %.3lfx %c %.3lfy %c %.3lf = 0\n", sign(a1), fabs(a1), sign(a2), fabs(a2), sign(a3), fabs(a3));
}
int main()
{
while(scanf("%lf%lf%lf%lf%lf%lf", &x3, &y3, &x4, &y4, &x5, &y5) == 6)
{
solve();
printf("\n");
}
return 0;
}


你可能感兴趣的:(poi)