码迷,mamicode.com
首页 > 其他好文 > 详细

数学 A Rescue The Princess

时间:2015-04-07 15:20:33      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

 

题目传送门

 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)\n", xc, yc);
30         }
31     }
32     
33     return 0;
34 }
 1 /*
 2     C点对A点的增量是长度 * cos/sin,角度是60 + 斜率的角度
 3 */
 4 #include <cstdio>
 5 #include <cmath>
 6 #include <algorithm>
 7 #include <iostream>
 8 #include <cstring>
 9 using namespace std;
10 
11 const double PI = acos (-1.0);
12 
13 int main(void)        //A Rescue The Princess
14 {
15     //freopen ("A.txt", "r", stdin);
16     
17     int t;
18     while (scanf ("%d", &t) == 1)
19     {
20         while (t--)
21         {
22             double xa, ya, xb, yb, xd, yd;
23             scanf ("%lf%lf%lf%lf", &xa, &ya, &xb, &yb);
24             double ab = sqrt ((xa-xb) * (xa-xb) + (ya-yb) * (ya-yb));
25             double R = atan ((ya - yb) / (xa - xb));
26             double xc = xa + ab * cos (R + PI / 3.0);
27             double yc = ya + ab * sin (R + PI / 3.0);
28             printf ("(%.2f,%.2f)\n", xc, yc);
29         }
30     }
31     
32     return 0;
33 }
 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\n", 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)\n", xc, yc);
85         }
86     }
87     
88     return 0;
89 }

 

数学 A Rescue The Princess

标签:

原文地址:http://www.cnblogs.com/Running-Time/p/4398178.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!