点上的旋转矩形 [英] Points on a rotated rectangle
问题描述
我试图计算左下点为矩形,因为它围绕其旋转。我试图谷歌,但显然我失去了一些东西。我试图用一个转换矩阵来计算点。
有关我的设置,我有一个长方形夹名为test和一个叫PNT剪辑我试图保持在左下角点。这里是$ C $下我的演示。我刚抛出这个到时间线上的第一帧测试:
//申报点的初始位置
pnt.x =(test.x - test.width / 2);
pnt.y =(test.y + test.height / 2);
//角落和中心之间的距离
变种DX:数= pnt.x - test.x;
变种DY:数= pnt.y - test.y;
的addEventListener(Event.ENTER_FRAME,旋转);
// X'= XC + DX COS(THETA) - DY罪(THETA)
// Y'= YC + DX罪(THETA)+ DY COS(THETA)
功能旋转(五:事件):无效{
test.rotation ++;
//使用变换矩阵来计算新的x的角落和y
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));
迹线(X:+ Math.cos(旋转));
跟踪(Y+ pnt.y);
//计算新距离市中心
DX = pnt.x - test.x;
DY = pnt.y - test.y;
}
我们可以通过
模拟的一个点的轨迹 (X',Y')=(XC + R COS(THETA + theta0),YC + R罪(THETA + theta0))
其中
(X',Y')=新的位置
(XC,YC)=中心点的东西绕
(X,Y)=初始点
间(X,Y)和r =距离(XC,YC)
THETA =逆时针旋转,弧度
theta0 =初始(X,Y)的旋转,以弧度
我们的初始点告诉我们,
研究罪(theta0)=(Y - YC)
- [R COS(theta0)=(X - XC)
通过trigonomerty功率:
研究COS(THETA + theta0)=
- [R COS(THETA)COS(theta0) - R的罪(THETA)罪(theta0)=
COS(THETA)(X - XC) - 罪(THETA)(Y - YC)
和
研究罪(THETA + theta0)=
- [R罪(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罪(THETA)
Y'= YC + DX罪(THETA)+ DY COS(THETA)
与 DX
和 DY
按
DX = X - XC
DY = Y - YC
I'm attempting to calculate the bottom-left point in a rectangle as it is rotated around. I've attempted to Google it, but apparently I'm missing something. I'm attempting to use a transformation matrix to calculate the point.
For my setup, I have a rectangle clip called "test" and a clip called "pnt" that I'm trying to keep on the lower left point. Here is the code for my demo. I've just thrown this onto the first frame of the timeline to test:
//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;
}
We can model the trajectory of a single point by
(x',y') = (xc + r cos(theta + theta0), yc + r sin(theta + theta0))
where
(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
Our initial point tells us that
r sin(theta0) = (y - yc)
r cos(theta0) = (x - xc)
By the power of trigonomerty:
r cos(theta + theta0) =
r cos(theta)cos(theta0) - r sin(theta)sin(theta0) =
cos(theta)(x - xc) - sin(theta)(y - yc)
and
r sin(theta + theta0) =
r sin(theta)cos(theta0) + r cos(theta)sint(theta0)
sin(theta)(x - xc) + cos(theta)(y - yc)
Therefore, given
- The center point
(xc, yc)
that stuff is rotating around - The point to track
(x, y)
- (your rectangle corner) - A rotation
theta
, in radians
The new position of the point will be:
x' = xc + dx cos(theta) - dy sin(theta)
y' = yc + dx sin(theta) + dy cos(theta)
with dx
and dy
given by
dx = x - xc
dy = y - yc
这篇关于点上的旋转矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!