Problem D
Morley’s Theorem
Input: Standard Input
Output: Standard Output
Morley’s theorem states that that the lines trisecting the angles of an arbitrary plane triangle meet at the vertices of an equilateral triangle. For example in the figure below the tri-sectors of angles A, B and C has intersected and created an equilateral triangle DEF.
Of course the theorem has various generalizations, in particular if all of the tri-sectors are intersected one obtains four other equilateral triangles. But in the original theorem only tri-sectors nearest to BC are allowed to intersect to get point D, tri-sectors nearest to CA are allowed to intersect point E and tri-sectors nearest to AB are intersected to get point F. Trisector like BD and CE are not allowed to intersect. So ultimately we get only one equilateral triangle DEF. Now your task is to find the Cartesian coordinates of D, E and F given the coordinates of A, B, and C.
Input
First line of the input file contains an integer N (0<N<5001) which denotes the number of test cases to follow. Each of the next lines contain sixintegers . This six integers actually indicates that the Cartesian coordinates of point A, B and C are respectively. You can assume that the area of triangle ABC is not equal to zero, and the points A, B and C are in counter clockwise order.
Output
For each line of input you should produce one line of output. This line contains six floating point numbers separated by a single space. These six floating-point actually means that the Cartesian coordinates of D, E and F are respectively. Errors less than will be accepted.
Sample Input Output for Sample Input
2 1 1 2 2 1 2 0 0 100 0 50 50 |
1.316987 1.816987 1.183013 1.683013 1.366025 1.633975 56.698730 25.000000 43.301270 25.000000 50.000000 13.397460 |
Problemsetters: Shahriar Manzoor
Special Thanks: Joachim Wulff
计算几何基础练习,求两射线交点。
题意:
Morley定理是这样的:作三角形ABC每个内角的三等分线,相交成三角形DEF,则DEF是等边三角形,如图所示。
你的任务是根据A、B、C 3个点的位置确定D、E、F 3个点的位置。
———— 《算法竞赛入门经典——训练指南》
思路:
分别求出三角形每对顶点形成内角的三等分线,求这两条三等分线的交点。
因为是练习基础的一道题,所以我自己敲了好几个模板,但实际上只用到了求交点的函数,求2向量角度的函数以及向量旋转的函数,没有太复杂的算法,思路很好理解。
代码:
1 #include <iostream>
2 #include <iomanip>
3 #include <cmath>
4 using namespace std;
5 struct Point{
6 double x,y;
7 Point(double x=0,double y=0):x(x),y(y) {} //构造函数
8 };
9 typedef Point Vector;
10 //向量 + 向量 = 向量
11 Vector operator + (Vector A,Vector B)
12 {
13 return Vector(A.x + B.x,A.y + B.y);
14 }
15 //点 - 点 = 向量
16 Vector operator - (Point A,Point B)
17 {
18 return Vector(A.x - B.x,A.y - B.y);
19 }
20 //向量 * 数 = 向量
21 Vector operator * (Vector A,double p)
22 {
23 return Vector(A.x * p,A.y * p);
24 }
25 //向量 / 数 = 向量
26 Vector operator / (Vector A,double p)
27 {
28 return Vector(A.x / p,A.y / p);
29 }
30 bool operator < (const Point & a,const Point& b)
31 {
32 return a.x < b.x || (a.x==b.x && a.y < b.y);
33 }
34 const double eps = 1e-10;
35 //减少精度问题
36 int dcmp(double x)
37 {
38 if(fabs(x)<eps)
39 return 0;
40 else
41 return x<0?-1:1;
42 }
43 bool operator == (const Point& a,const Point& b)
44 {
45 return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0;
46 }
47 double Dot(Vector A,Vector B)
48 {
49 return A.x*B.x + A.y*B.y;
50 }
51 double Length(Vector A)
52 {
53 return sqrt(A.x*A.x + A.y*A.y);
54 }
55 //求两向量夹角的弧度
56 double Angle(Vector A,Vector B)
57 {
58 return acos(Dot(A,B) / Length(A) / Length(B));
59 }
60 //求叉积
61 double Cross(Vector A,Vector B)
62 {
63 return A.x*B.y - A.y*B.x;
64 }
65 double Area2(Point A,Point B,Point C)
66 {
67 return Cross(B-A,C-A);
68 }
69 Vector Rotate(Vector A,double rad)
70 {
71 return Vector(A.x*cos(rad) - A.y*sin(rad),A.x*sin(rad) + A.y*cos(rad));
72 }
73 Point GetLineIntersection(Point P,Vector v,Point Q,Vector w)
74 {
75 Vector u = P-Q;
76 double t = Cross(w,u) / Cross(v,w);
77 return P+v*t;
78 }
79 Point GetPoint(Point A,Point B,Point C)
80 {
81 Vector v1 = C-B;
82 double a1 = Angle(A-B,v1);
83 v1 = Rotate(v1,a1/3);
84
85 Vector v2 = B-C;
86 double a2 = Angle(A-C,v2);
87 v2 = Rotate(v2,-a2/3); //负数代表顺时针旋转
88
89 return GetLineIntersection(B,v1,C,v2);
90 }
91
92 int main()
93 {
94 int n;
95 cin>>n;
96 while(n--){
97 Point a,b,c,d,e,f;
98 cin>>a.x>>a.y>>b.x>>b.y>>c.x>>c.y;
99 d = GetPoint(a,b,c);
100 e = GetPoint(b,c,a);
101 f = GetPoint(c,a,b);
102 cout<<setiosflags(ios::fixed)<<setprecision(6);
103 cout<<d.x<<' '<<d.y<<' '
104 <<e.x<<' '<<e.y<<' '
105 <<f.x<<' '<<f.y<<endl;
106 }
107 return 0;
108 }
Freecode : www.cnblogs.com/yym2013