2011-12-16 12:01:00
地址:http://acm.hdu.edu.cn/showproblem.php?pid=2105
题意:给3个点的坐标代表一个三角形,求三角形的重心(三条中线的交点)。
mark:计算几何果断在mathematica的帮助下很给力!
代码:
# include <stdio.h>
typedef struct POINT{
double x, y ;
}POINT ;
typedef struct LINE{
double a, b, c ; //ax+by+c == 0
}LINE ;
POINT tri[3] ;
POINT Middle(POINT p1, POINT p2)
{
POINT rtn ;
rtn.x = (p1.x + p2.x) / 2.0 ;
rtn.y = (p1.y + p2.y) / 2.0 ;
return rtn ;
}
LINE GetLine (POINT p1, POINT p2)
{
LINE rtn ;
rtn.a = (p1.y - p2.y) ;
rtn.b = (p2.x - p1.x) ;
rtn.c = -(p2.x*p1.y - p1.x*p2.y) ;
return rtn ;
}
POINT Cross(LINE l1, LINE l2)
{
POINT rtn ;
double fm = l2.a*l1.b - l1.a*l2.b ;
rtn.x = (l2.b*l1.c - l1.b*l2.c) / fm ;
rtn.y = (l1.a*l2.c - l2.a*l1.c) / fm ;
return rtn ;
}
POINT gao()
{
POINT p1, p2, rtn ;
LINE l1, l2 ;
p1 = Middle(tri[0], tri[1]) ;
p2 = Middle(tri[1], tri[2]) ;
l1 = GetLine(p1, tri[2]) ;
l2 = GetLine(p2, tri[0]) ;
rtn = Cross(l1, l2) ;
return rtn ;
}
int main ()
{
int n, i ;
POINT pt ;
while (~scanf ("%d", &n) && n)
{
while (n--)
{
for (i = 0 ; i< 3 ; i++)
scanf ("%lf%lf", &tri[i].x, &tri[i].y) ;
pt = gao() ;
printf ("%.1lf %.1lf\n", pt.x, pt.y) ;
}
}
return 0 ;
}