计算几何初步-向量的旋转 Rescue The Princess

来源:互联网 发布:string 转 日历 java 编辑:程序博客网 时间:2024/05/19 12:25

题目地址:http://acm.upc.edu.cn/problem.php?id=2217

题目描述

    Several days ago, a beast caught a beautiful princess and the princess was put in prison. To rescue the princess, a prince who wanted to marry  the princess set out immediately. Yet, the beast set a maze. Only if the prince find out the maze’s exit can he save the princess.

    Now, here comes the problem. The maze is a dimensional plane. The beast is smart, and he hidden the princess snugly. He marked two coordinates of an equilateral triangle in the maze. The two marked coordinates are A(x1,y1) and B(x2,y2). The third coordinate C(x3,y3) is the maze’s exit. If the prince can find out the exit, he can save the princess. After the prince comes into the maze, he finds out the A(x1,y1) and B(x2,y2), but he doesn’t know where the C(x3,y3) is. The prince need your help. Can you calculate the C(x3,y3) and tell him?

输入

    The first line is an integer T(1 <= T <= 100) which is the number of test cases. T test cases follow. Each test case contains two coordinates A(x1,y1) and B(x2,y2), described by four floating-point numbers x1, y1, x2, y2 ( |x1|, |y1|, |x2|, |y2| <= 1000.0).
    Please notice that A(x1,y1) and B(x2,y2) and C(x3,y3) are in an anticlockwise direction from theequilateral triangle. And coordinates A(x1,y1) and B(x2,y2) are given by anticlockwise.

输出

    For each test case, you should output the coordinate of C(x3,y3), the result should be rounded to 2 decimal places in a line.

示例输入

4-100.00 0.00 0.00 0.000.00 0.00 0.00 100.000.00 0.00 100.00 100.001.00 0.00 1.866 0.50

示例输出

(-50.00,86.60)(-86.60,50.00)(-36.60,136.60)(1.00,1.00)


告诉你是一个等边三角形并且给你两个点a(x1,y1)和b(x2,y2),让你求第三个点,按照逆时针顺序求。

我这里是用的纯三角函数做的。

#include <stdio.h>#include <math.h>int n;double xx1,xx2,yy1,yy2;int main() {double l, atana, pi = 3.14159265358;freopen("in.txt", "r", stdin);scanf("%d", &n);while(n--){scanf("%lf %lf %lf %lf", &xx1,&yy1, &xx2, &yy2);double tmp = (double)(xx2-xx1) * (xx2-xx1) + (yy2-yy1)*(yy2-yy1);l = sqrt(tmp);atana = atan2((yy2-yy1) , (xx2-xx1)) + pi/3;printf("(%.2lf,%.2lf)\n", cos(atana)*l + xx1, sin(atana)*l + yy1);}return 0;}


这里有推导的公式:

http://www.cnblogs.com/E-star/archive/2013/06/11/3131563.html

我们可以这样认为:对于任意点A(x,y)A非原点,绕原点旋转θ角后点的坐标为:(x*cosθ- y * sinθ, y*cosθ + x * sinθ)

因此又有此解法:


  1. #include<stdio.h>  
  2. #include<math.h>  
  3. int main()  
  4. {  
  5.     int t;  
  6.     scanf("%d",&t);  
  7.     while(t--)  
  8.     {  
  9.         double x1,x2,y1,y2,x3,y3,x,y;  
  10.         scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);  
  11.         x=x2-x1;  
  12.         y=y2-y1;  
  13.         x3=x/2-y*sqrt(3)/2+x1;  
  14.         y3=y/2+x*sqrt(3)/2+y1;  
  15.         printf("(%.2f,%.2f)\n",x3,y3);  
  16.     }  
  17.     return 0;  
  18. }  



http://www.cnblogs.com/E-star/archive/2013/06/11/3131563.html