flash - 旋转矩形上的点

标签 flash actionscript-3 algorithm geometry

我正在尝试计算旋转的矩形中的左下角点。我试图用谷歌搜索它,但显然我遗漏了一些东西。我正在尝试使用转换矩阵来计算点。

对于我的设置,我有一个名为“test”的矩形剪辑和一个名为“pnt”的剪辑,我试图将其保留在左下角。这是我的演示代码。我刚刚将其放到时间轴的第一帧进行测试:

//declare initial position of points
pnt.x = (test.x - test.width/2);
pnt.y = (test.y + test.height/2);

//distance between corner and center
var dx:Number = pnt.x - test.x;
var dy:Number = pnt.y - test.y;

addEventListener(Event.ENTER_FRAME,rotate);


//x' = xc + dx cos(theta) - dy sin(theta)
//y' = yc + dx sin(theta) + dy cos(theta)
function rotate(e:Event):void{
    test.rotation++;

    // use the transformation matrix to calculate the new x and y of the corner
    pnt.x = test.x + dx*Math.cos(test.rotation*(Math.PI/180)) - dy*Math.sin(test.rotation*(Math.PI/180));
    pnt.y = test.y + dx*Math.sin(test.rotation*(Math.PI/180)) + dy*Math.cos(test.rotation*(Math.PI/180));

    trace("X: " + Math.cos(rotation));
    trace("Y: " + pnt.y);
    // calculate the new distance to the center
    dx = pnt.x - test.x;
    dy = pnt.y - test.y;
}

最佳答案

我们可以通过以下方式对单点的轨迹进行建模

(x',y') = (xc + r cos(theta + theta0), yc + r sin(theta + theta0))

在哪里

(x', y') = new position
(xc, yc) = center point things rotate around
(x, y) = initial point
r = distance between (x,y) and (xc, yc)
theta = counterclockwise rotation, in radians
theta0 = initial rotation of (x,y), in radians

我们的初始点告诉我们

r sin(theta0) = (y - yc) 
r cos(theta0) = (x - xc)

利用三角函数:

r cos(theta + theta0) =
r cos(theta)cos(theta0) - r sin(theta)sin(theta0) = 
cos(theta)(x - xc) - sin(theta)(y - yc)

r sin(theta + theta0) = 
r sin(theta)cos(theta0) + r cos(theta)sint(theta0)
sin(theta)(x - xc) + cos(theta)(y - yc)

因此,给定

  1. 东西围绕着旋转的中心点(xc, yc)
  2. 要跟踪的点 (x, y) -(你的矩形角)
  3. 旋转 theta,以弧度为单位

点的新位置将是:

x' = xc + dx cos(theta) - dy sin(theta)
y' = yc + dx sin(theta) + dy cos(theta)

dxdy

dx = x - xc
dy = y - yc    

关于flash - 旋转矩形上的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6867474/

相关文章:

java - 在数组中查找组合

识别鼠标移动的算法

actionscript-3 - AS3 使用静态类型调用可能未定义的方法问题

javascript - Flash/Animate CC 补间格式数字

javascript - 打开 Flash Chart 2 IE 不加载图表的新数据

actionscript-3 - "Up, Hit, Down, and over"在 Action Script 3 中意味着什么?如何创建 "scrolling"

actionscript-3 - VerifyError : Error #1014: class could not be found

flash - XFL - 什么是 ./bin/*.dat 文件?

actionscript-3 - 返回到as3中开始的帧时,如何阻止音乐多次播放?

algorithm - 动态规划中的背包变体