流水依依

希望朋友们有个好的身体,开开心心工作与学习。

博客园 首页 新随笔 联系 订阅 管理

坐标旋转公式

x1=cos(angle)*x-sin(angle)*y;

y1=cos(angle)*y+sin(angle)*x;

其中x,y表示物体相对于旋转点旋转angle的角度之前的坐标,x1,y1表示物体旋转angle后相对于旋转点的坐标

 

从数学上来说,此公式可以用来计算某个点绕另外一点旋转一定角度后的坐标,例如:A(x,y)绕B(a,b)旋转β度后的位置为C(c,d),则x,y,a,b,β,c,d有如下关系式:

1。设A点旋转前的角度为δ,则旋转(逆时针)到C点后角度为δ+β

2。求A,B两点的距离:dist1=|AB|=y/sin(δ)=x/cos(δ)

3。求C,B两点的距离:dist2=|CB|=d/sin(δ+β)=c/cos(δ+β)

4。显然dist1=dist2,设dist1=r所以:

  r=x/cos(δ)=y/sin(δ)=d/sin(δ+β)=c/cos(δ+β)

5。由三角函数两角和差公式知:

  sin(δ+β)=sin(δ)cos(β)+cos(δ)sin(β)

  cos(δ+β)=cos(δ)cos(β)-sin(δ)sin(β)

  所以得出:

  c=r*cos(δ+β)=r*cos(δ)cos(β)-r*sin(δ)sin(β)=xcos(β)-ysin(β)

  d=r*sin(δ+β)=r*sin(δ)cos(β)+r*cos(δ)sin(β)=ycos(β)+xsin(β)

即旋转后的坐标c,d只与旋转前的坐标x,y及旋转的角度β有关

 

从图中可以很容易理解出A点旋转后的C点总是在圆周上运动,圆周的半径为|AB|,利用这点就可以使物体绕圆周运动,即旋转物体。

 

上面公式是相对于B点坐标来的,也就是假如B点位(0,0)可以这么做。现在给出可以适合任意情况的公式:

x0 = dx * cos(β) - dy * sin(β)

y0 = dy * cos(β) + dx * sin(β)

 

参数解释:

x0,y0是旋转后相对于中心点的坐标,也就是原点的坐标,但不是之前点旋转后的实际坐标,还要计算一步,

β旋转角度,可以是顺时针或者逆时针。

dx是旋转前的x坐标-旋转后的x坐标

dy是旋转前的y坐标-旋转后的y坐标

 

x1=a+x0;

y1=b+y0;

 

上面才是旋转后的实际坐标,其中a,b是原点坐标

 

下面是上面图的公式解答:

x0=(x-a)*cos(β)-(y-b)*sin(β);

y0=(y-b)*cos(β)+(x-a)*sin(β);

x1=x0+a;

y1=y0+b;

 

 

Rescue The Princess

 

题目描述

    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.

输出

    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.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)

提示

 

来源

2013年山东省第四届ACM大学生程序设计竞赛
       cin>>a>>b>>x>>y ;
       X=(x-a)*0.5-(y-b)*sqrt(3.0)*0.5 +a;
       Y=(y-b)*0.5+(x-a)*sqrt(3.0)*0.5 +b;
       printf("(%.2lf,%.2lf)\n",X,Y) ;

 

 
posted on 2013-09-05 19:56  流水依依  阅读(5508)  评论(0编辑  收藏  举报