计算几何 2013年山东省赛 A Rescue The Princess

 

题目传送门

 1 /*
 2     已知一向量为(x , y) 则将它旋转θ后的坐标为(x*cosθ- y * sinθ , y*cosθ + x * sinθ)
 3     应用到本题,x变为(xb - xa), y变为(yb - ya)相对A点的位置,即B绕着A点旋转60度至C点
 4     注意:计算后加回A点的坐标才是相对于原点的坐标
 5     详细解释:http://www.tuicool.com/articles/FnEZJb
 6 */
 7 #include <cstdio>
 8 #include <cmath>
 9 #include <algorithm>
10 #include <iostream>
11 #include <cstring>
12 using namespace std;
13 
14 const double PI = acos (-1.0);
15 
16 int main(void)        //A Rescue The Princess
17 {
18     //freopen ("A.txt", "r", stdin);
19     
20     int t;
21     while (scanf ("%d", &t) == 1)
22     {
23         while (t--)
24         {
25             double xa, ya, xb, yb, xd, yd;
26             scanf ("%lf%lf%lf%lf", &xa, &ya, &xb, &yb);
27             double xc = (xb - xa) * cos (PI/3.0) - (yb - ya) * sin (PI/3.0) + xa;
28             double yc = (yb - ya) * cos (PI/3.0) + (xb - xa) * sin (PI/3.0) + ya;
29             printf ("(%.2f,%.2f)
", xc, yc);
30         }
31     }
32     
33     return 0;
34 }
 1 /*
 2     分各种情况讨论,这是我比赛写的,最后因为没有去绝对值而WA几次,
 3     与上面的比较来看,可见好的思维和数学素养是多么重要:)
 4 */
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <algorithm>
 8 #include <iostream>
 9 #include <cstring>
10 using namespace std;
11 
12 int main(void)        //A Rescue The Princess
13 {
14     //freopen ("A.txt", "r", stdin);
15     
16     int t;
17     while (scanf ("%d", &t) == 1)
18     {
19         while (t--)
20         {
21             double xa, ya, xb, yb, xc, yc, xd, yd;
22             scanf ("%lf%lf%lf%lf", &xa, &ya, &xb, &yb);
23             double ab = sqrt ((xa-xb) * (xa-xb) + (ya-yb) * (ya-yb));
24             double cd = sqrt (3.0) / 2 * ab;
25             xd = (xa + xb) / 2;    yd = (ya + yb) / 2;
26             if (ya == yb)
27             {
28                 xc = (xa + xb) / 2;
29                 if (xa < ab)
30                 {
31                     yc = ya + cd;
32                 }
33                 else
34                 {
35                     yc = ya - cd;    
36                 }
37             }
38             else if (xa == xb)
39             {
40                 yc = (ya + yb) / 2;
41                 if (ya > yb)
42                 {
43                     xc = xa + cd;
44                 }
45                 else
46                 {
47                     xc = xa - cd;
48                 }
49             }
50             else
51             {
52                 double k = (ya - yb) / (xa - xb);
53                 k = -1.0 / k;
54                 double q = atan (k);
55                 //printf ("%.2f %.2f %.2f %.2f %.2f %.2f
", ab, cd, xd, yd, k, q);
56                 if (k > 0)
57                 {
58                     if (xa < xb)
59                     {
60                         xc = xd + abs (cd * cos (q));
61                         yc = yd + abs (cd * sin (q));
62                     }
63                     else
64                     {
65                         xc = xd - abs (cd * cos (q));
66                         yc = yd - abs (cd * sin (q));
67                     }
68                 }
69                 else
70                 {
71                     if (xa < xb)
72                     {
73                         xc = xd - abs (cd * cos (q));
74                         yc = yd + abs (cd * sin (q));
75                     }
76                     else
77                     {
78                         xc = xd + abs (cd * cos (q));
79                         yc = yd - abs (cd * sin (q));
80                     }
81                 }
82             }
83             
84             printf ("(%.2f,%.2f)
", xc, yc);
85         }
86     }
87     
88     return 0;
89 }