首页 > 代码库 > UVa 10697 - Firemen barracks

UVa 10697 - Firemen barracks

题目:已知三点,求到三点距离相同的点。

分析:计算几何。分三类情况讨论:

            1.三点共线,不成立;

            2.多点重叠,有多组解;

            3.是三角形,输出中点。

说明:注意绝对值小于0.05的按0计算;负数的四舍五入与正数不同,-0.05的%.1lf输出是 -0.0。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double dist( double x0, double y0, double x1, double y1 )
{
	return sqrt((x1-x0)*(x1-x0)+(y1-y0)*(y1-y0));
}

double deal( double x )
{
	double esp = 1e-6;
	if ( x + esp < 0.05 && x - esp > -0.05 )
		return 0.0;
	else if ( x < 0.0 )
		return x - esp;
	else return x;
	
}

void calc( double x0, double y0, double x1, double y1, double x2, double y2 )
{
	double a  = dist( x0, y0, x1, y1 );
	double b  = dist( x0, y0, x2, y2 );
	double c  = dist( x2, y2, x1, y1 );
	if ( x0 == x1 && y1 == y0 || x0 == x2 && y2 == y0 || x2 == x1 && y1 == y2 ) {
		printf("There is an infinity of possible locations.\n");
		return;
	}
	if ( fabs(a-b-c) < 1e-9 || fabs(b-a-c) < 1e-9 || fabs(c-a-b) < 1e-9 ) {
		printf("There is no possible location.\n");
		return;
	}
	double A1 = x1-x0,B1 = y1-y0,C1 = x1*x1-x0*x0+y1*y1-y0*y0;
	double A2 = x2-x0,B2 = y2-y0,C2 = x2*x2-x0*x0+y2*y2-y0*y0;
	double X  = (B1*C2-B2*C1)/(A1*B2-A2*B1)/-2.0;
	double Y  = (A2*C1-A1*C2)/(A1*B2-A2*B1)/-2.0;
	printf("The equidistant location is (%.1lf, %.1lf).\n",deal(X),deal(Y));
}

int main()
{
	int n;
	double x1,x2,x3,y1,y2,y3;
	while ( ~scanf("%d",&n) ) 
	while ( n -- ) {
		scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3);
		calc( x1, y1, x2, y2, x3, y3 );
	}
	return 0;
}