我正在尝试计算旋转的矩形中的左下角点。我试图用谷歌搜索它,但显然我遗漏了一些东西。我正在尝试使用转换矩阵来计算点。
对于我的设置,我有一个名为“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)
因此,给定
- 东西围绕着旋转的中心点
(xc, yc)
- 要跟踪的点
(x, y)
-(你的矩形角) - 旋转
theta
,以弧度为单位
点的新位置将是:
x' = xc + dx cos(theta) - dy sin(theta)
y' = yc + dx sin(theta) + dy cos(theta)
用 dx
和 dy
给
dx = x - xc
dy = y - yc
关于flash - 旋转矩形上的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6867474/