Sicily1059-Exocenter of a Trian
代码地址: https://github.com/laiy/Datastructure-Algorithm/blob/master/sicily/1059.c
1059. Exocenter of a Trian
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
Given a triangle ABC, the Extriangles of ABC are constructed as follows:
On each side of ABC, construct a square (ABDE, BCHJ and ACFG in the figure below).
Connect adjacent square corners to form the three Extriangles (AGD, BEJ and CFH in the figure).
The Exomedians of ABC are the medians of the Extriangles, which pass through vertices of the original triangle, extended into the original triangle (LAO, MBO and NCO in the figure. As the figure indicates, the three Exomedians intersect at a common point called the Exocenter (point O in the figure).
This problem is to write a program to compute the Exocenters of triangles.
Input
The first line of the input consists of a positive integer n, which is the number of datasets that follow. Each dataset consists of 3 lines; each line contains two floating point values which represent the (two -dimensional) coordinate of one vertex of a triangle. So, there are total of (n*3) + 1 lines of input. Note: All input triangles wi ll be strongly non-degenerate in that no vertex will be within one unit of the line through the other two vertices.
Output
For each dataset you must print out the coordinates of the Exocenter of the input triangle correct to four decimal places.
这题首先最重要的一点: 证明我们要求解的就是三角形ABC的垂心的坐标。
证明如下:
证明:
∵AK = A'K DK = GK ∠6 = ∠7
根据(SAS) ∴△AGK≌A'GK
∴∠1 = ∠4
又∵∠1 + ∠2 + ∠3 = 180°
∴∠2 + ∠3 + ∠4 = 180°
又∵∠3 + ∠4 + ∠5 = 180°
∴∠2 = ∠5
又∵AD = AB AG = AC
根据SAS ∴△ABC≌DAA'
∴∠3 = ∠8
又∵∠BAO + ∠3 = 90°
∴∠BAO + ∠8 = 90°
∴∠9 = 90°
同理∠10 = ∠11 = 90°
∴点O为高线交点 为△ABC的垂心
证毕。
好, 接下来是垂心的求解思路,很简单,设垂心坐标为(x, y), 三角形3个点坐标为(x1, y1) (x2, y2) (x3, y3)
用向量垂直来得到以下公式:
(x2 - x1)(x - x3) + (y2 - y1)(y - y3) = 0
(x3 - x1)(x - x2) + (y3 - y1)(y - y2) = 0
然后就求解这个方程组得到x, y就行了
但是这么做会有两个坑:
1. 不能输出-0.0000 所以最后结果输出的时候+个EPS就可以了。
2. 用向量计算的时候计算出x, 在计算y的时候,如果这时候使用的直线是平行于y轴的, 带入x将会输出错误的结果, 此时应换另一个直线方程做计算。
AC代码:
1 #include <cstdio> 2 #include <cmath> 3 4 #define EPS 1e-8 5 6 int main() { 7 int t; 8 double x1, y1, x2, y2, x3, y3, x, y; 9 scanf("%d", &t); 10 while (t--) { 11 scanf("%lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3); 12 x = (x3 * (x2 - x1) * (y3 - y1) - (y2 - y1) * ((x3 - x1) * x2 + (y2 - y3) * (y3 - y1))) 13 / ((x2 - x1) * (y3 - y1) + (y2 - y1) * (x1 - x3)); 14 y = (fabs(x - x3) < EPS) ? y2 + (x3 - x1) * (x - x2) / (y1 - y3) : y3 + (x2 - x1) * (x - x3) / (y1 - y2); 15 printf("%.4f %.4f\n", x + EPS, y + EPS); 16 } 17 return 0; 18 }