Rescue The Princess
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
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 the equilateral triangle. And coordinates A(x1,y1) and B(x2,y2) are given by anticlockwise.
输出
示例输入
4 -100.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00 0.00 0.00 100.00 100.00 1.00 0.00 1.866 0.50
示例输出
(-50.00,86.60) (-86.60,50.00) (-36.60,136.60) (1.00,1.00)
来源
这道题就是当初我卡的那道。。很简单,就是自己*%&了。。。
当时不知道脑子咋了,愣是看了好久好久。。。
无论计算几何还是解析几何都可以做出来。
用计算几何搞得这道题。
题意: 给你等边三角形的两个点,逆时针的第三个点坐标。
用的公式:OB=(x*cosα-y*sinα,x*sinα+y*cosα)
这是逆时针的,还有顺时针的:
OA=(y*sinα - x*cosα,x*sinα+y*cosα)
OA,OB看晕了吧,当然还要配图:
然后,这道题就迎刃而解了。。。
O,还有,这道题会卡精度。。。π值最好用acos(-1)来求,
反正我用3.1415926不行(我只能背到这了,o(╯□╰)o)
/**************************************
***************************************
* Author:Tree *
*From :http://blog.csdn.net/lttree *
* Title : Rescue The Princess *
*Source: 第四届山东省ACM比赛 *
* Hint : 计算几何 *
***************************************
**************************************/
#include <stdio.h>
#include <cmath>
using namespace std;
#define Pi acos(-1.0)
struct Point
{
double x,y;
};
struct Line
{
Point a,b;
};
// 向量l,绕l.a点逆时针转a弧度,返回转到的点。
// OB=(x*cosα-y*sinα,x*sinα+y*cosα)
Point rotate_anticw(Line l,double a)
{
Point p;
p.x = ( l.b.x-l.a.x ) * cos(a) - ( l.b.y-l.a.y ) * sin(a);
p.y = ( l.b.x-l.a.x ) * sin(a) + ( l.b.y-l.a.y ) * cos(a);
p.x += l.a.x;
p.y += l.a.y;
return p;
}
// 向量l,绕l.a点顺时针转a弧度,返回转到的点。
// OA=(y*sinα - x*cosα,x*sinα+y*cosα)
Point rotate_cw(Line l,double a)
{
Point p;
p.x = ( l.b.y-l.a.y ) * sin(a) - ( l.b.x-l.a.x ) * cos(a);
p.y = ( l.b.x-l.a.x ) * sin(a) + ( l.b.y-l.a.y ) * cos(a);
p.x += l.a.x;
p.y += l.a.y;
return p;
}
int main()
{
int t;
Point c;
Line l;
scanf("%d",&t);
while( t-- )
{
scanf("%lf%lf%lf%lf",&l.a.x,&l.a.y,&l.b.x,&l.b.y);
c = rotate_anticw(l,60*Pi/180);
printf("(%.2lf,%.2lf)\n",c.x,c.y);
}
return 0;
}