Sicily 1059. exocenter of a trian

Source: Internet
Author: User

Descriptiongiven 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.

Inputthe 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-dimeneting) 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),还要注意的是 double 对 0 的处理,也即下面的 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.