首页 > 代码库 > sicily 1059. Exocenter of a Trian

sicily 1059. Exocenter of a Trian

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.

Sample Input
 Copy sample input to clipboard 
20.0 0.09.0 12.014.0 0.03.0 4.013.0 19.02.0 -10.0
Sample Output
9.0000 3.7500-48.0400 23.3600
分析:这题要求的点其实就是三角形的垂心,那么只要根据三角形坐标求得垂心的坐标即可,我是根据斜率乘积是 -1 来求解方程式而得到结果的。但是这样做的话要注意斜率不存在的情况,所以一共有 5 种情况(分母为 0),还要注意的是 double0 的处理,也即下面的 dcmp 函数。这样处理显得有点,乱,特别是在公式处理。但是只要自己把公式列出来还是能很快理解的。另外这里将三个点排序的原因是这样处理后能使得三个点的相对位置明确,减少可能出现的斜率为零的情况。
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;const double eps = 1e-5;struct Point{    double x;    double y;};bool cmp(const Point &a, const Point &b) {    return a.x < b.x;}int dcmp(double x){    return (x > -eps && x < eps) ? 0 : 1;} int main(int argc, char const *argv[]){    int testNum;    cin >> testNum;    Point point[3];    while (testNum--) {        for (int i = 0; i != 3; ++i) {            cin >> point[i].x >> point[i].y;        }        sort(point, point + 3, cmp);        double x1_sub_x2 = point[0].x - point[1].x;        double y2_sub_y1 = point[1].y - point[0].y;        double x2_sub_x3 = point[1].x - point[2].x;        double y3_sub_y2 = point[2].y - point[1].y;        double x3_sub_s1 = point[2].x - point[0].x;        double y3_sub_y1 = point[2].y - point[0].y;        double x, y;        if (dcmp(x1_sub_x2) == 0) {            x = point[0].x;            y = point[2].y;        } else if (dcmp(y2_sub_y1) == 0) {            x = point[2].x;            y = point[0].y - (x2_sub_x3 / y3_sub_y2) * (-x3_sub_s1);        } else if (dcmp(x2_sub_x3) == 0) {            y = point[0].y;            x = (-y2_sub_y1) * y3_sub_y1 / x1_sub_x2 + point[2].x;        } else if (dcmp(x3_sub_s1) == 0) {            y = point[1].y;            x = point[2].x + y3_sub_y1 * y2_sub_y1 / (-x1_sub_x2);        } else {            x = (y3_sub_y1 - (x1_sub_x2 / y2_sub_y1) * point[2].x +                     (x2_sub_x3 / y3_sub_y2) * point[0].x) / (x2_sub_x3 / y3_sub_y2 - x1_sub_x2 / y2_sub_y1);            y = point[0].y - (x2_sub_x3 / y3_sub_y2) * (point[0].x - x);        }        x = dcmp(x) == 0 ? 0 : x;        y = dcmp(y) == 0 ? 0 : y;        printf("%.4lf %.4lf\n", x, y);    }    return 0;}

 

sicily 1059. Exocenter of a Trian