Snu*_*kus 2 algorithm flash geometry actionscript-3
我正试图计算一个矩形的左下角,因为它在周围旋转.我试图谷歌它,但显然我错过了一些东西.我正在尝试使用变换矩阵来计算点.
对于我的设置,我有一个名为"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;
}
Run Code Online (Sandbox Code Playgroud)
我们可以通过模拟单点的轨迹
(x',y') = (xc + r cos(theta + theta0), yc + r sin(theta + theta0))
Run Code Online (Sandbox Code Playgroud)
哪里
(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
Run Code Online (Sandbox Code Playgroud)
我们的初步观点告诉我们
r sin(theta0) = (y - yc)
r cos(theta0) = (x - xc)
Run Code Online (Sandbox Code Playgroud)
借助三角形的力量:
r cos(theta + theta0) =
r cos(theta)cos(theta0) - r sin(theta)sin(theta0) =
cos(theta)(x - xc) - sin(theta)(y - yc)
Run Code Online (Sandbox Code Playgroud)
和
r sin(theta + theta0) =
r sin(theta)cos(theta0) + r cos(theta)sint(theta0)
sin(theta)(x - xc) + cos(theta)(y - yc)
Run Code Online (Sandbox Code Playgroud)
因此,给定
(xc, yc)东西旋转的中心点(x, y)- (你的矩形角)theta,以弧度表示这一点的新立场将是:
x' = xc + dx cos(theta) - dy sin(theta)
y' = yc + dx sin(theta) + dy cos(theta)
Run Code Online (Sandbox Code Playgroud)
与dx和dy由下式给出
dx = x - xc
dy = y - yc
Run Code Online (Sandbox Code Playgroud)