《训练指南》——8.1

  Uva11178:

  题目大意:根据Morley定理我们可以知道,任意三角形的三个角的三等分点将交出一个等边三角形,那么现在给出三角形三个顶点A、B、C,请你计算D、E、F.

  分析:大部分几何的题目数理分析上都比较简单,但是实现起来是较为繁琐的。这道问题其实就是进行三次直线交点的计算,而在每一次计算过程中,两条直线分别是两个角的三等分直线靠近它们公共边的那一部分。

  那么如何求解这条直线?涉及向量旋转。

  交点如何求解?涉及相交直线求交点。

  这里需要结合一定解析几何的知识然后推出较为简单、丢失精度较少的公式,集成函数后进行运算,简单的参考代码如下:

#include<cstdio>
#include<cstring>
#include<cmath>

using namespace std;
struct point
{
    double x , y;
    point(double x = 0 , double y = 0):x(x),y(y){};//定义点的时候直接利用构造函数,很方便
};
typedef point Vector;//这里因为向量都有两个维度的有序参量

Vector operator + (Vector A,Vector B){return Vector(A.x+B.x , A.y+B.y);}
Vector operator - (point A,point B)  {return Vector(A.x-B.x , A.y-B.y);}
Vector operator * (Vector A,double p){return Vector(A.x*p , A.y*p);}
Vector operator / (Vector A,double p){return Vector(A.x/p , A.y/p);}

double Cross(Vector A , Vector B)//向量叉积
{
    return A.x * B.y - A.y * B.x;
}

point GetLineIntersection(point P ,Vector v , point Q , Vector w) //两直线交点(参数法)
{
    Vector u = P-Q;
    double t = Cross(w , u) / Cross(v ,w);
    return P+v*t;
}

Vector Rotate(Vector A , double rad)//向量旋转(逆时针是正角)
{
    return Vector(A.x*cos(rad) - A.y*sin(rad) , A.x*sin(rad) + A.y*cos(rad));
}

double Dot(Vector A, Vector B){return A.x*B.x + A.y*B.y;}//向量点乘
double Length(Vector A){return sqrt(Dot(A,A));}          //向量模长
double Angle(Vector A, Vector B){return acos(Dot(A,B)/Length(A)/Length(B));}//向量之间夹角

point getD(point A , point B , point C)
{
    Vector v1 = C - B;
    double a1 = Angle(A-B , v1);
    v1 = Rotate(v1 , a1/3);

    Vector v2 = B - C;
    double a2 = Angle(A-C , v2);
    v2 = Rotate(v2 , -a2/3);
    return GetLineIntersection(B , v1 , C , v2);
}

int main()
{
    int T;
    point A,B,C,D,E,F;
    scanf("%d",&T);
    while(T--)
    {
      scanf("%lf%lf%lf%lf%lf%lf",&A.x,&A.y,&B.x,&B.y,&C.x,&C.y);
      D = getD(A,B,C);
      E = getD(B,C,A);
      F = getD(C,A,B);
      printf("%.6lf %.6lf %.6lf %.6lf %.6lf %.6lf
",D.x,D.y,E.x,E.y,F.x,F.y);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/rhythmic/p/5724747.html