我正在进行一项代码/数学任务,我需要将一个形状旋转到0度,然后再回到它所处的任何角度。
以下方法:
function rotateAroundOrigin(xPos:Number, yPos:Number, angle:Number, origin:Point):Point {
var point:Point = new Point();
angle *= Math.PI / 180;
point.x = Math.cos(angle) * (xPos - origin.x) + Math.sin(angle) * (yPos - origin.y) + origin.x;
point.y = Math.sin(angle) * (xPos - origin.x) - Math.cos(angle) * (yPos - origin.y) + origin.y;
return point;
}
这样调用两次:
var rotatePoint:Point = rotateAroundOrigin(30, 30, -90, midPoint);
var rotatePointBack:Point = rotateAroundOrigin(30, 30, -90, midPoint);
它工作正常,但它没有意义,因为度数值是“-90”它旋转的两倍。单向旋转应为“-90”,然后旋转“90”旋转回原位。
任何人都有任何想法如何修改“rotateAroundOrigin”方法,以便您可以调用“-90”然后使用“90”调用?
var rotatePoint:Point = rotateAroundOrigin(30, 30, -90, midPoint);
var rotatePointBack:Point = rotateAroundOrigin(30, 30, 90, midPoint);
答案 0 :(得分:1)
var rotatePoint:Point = rotateAroundOrigin(30, 30, -90, midPoint); var rotatePointBack:Point = rotateAroundOrigin(30, 30, -90, midPoint);
看起来你只做了两次相同的事情:围绕midPoint转动点(30,30)。考虑一下:
var rotatePoint:Point = rotateAroundOrigin(30, 30, -90, midPoint);
var rotatePointBack:Point = rotateAroundOrigin(rotatePoint.x, rotatePoint.y, 90, midPoint);
编辑:我还注意到你在旋转方法中混淆了一些迹象。试试这个:
function rotateAroundOrigin(xPos:Number, yPos:Number, angle:Number, origin:Point):Point {
var point:Point = new Point();
angle *= Math.PI / 180;
point.x = Math.cos(angle) * (xPos - origin.x) - Math.sin(angle) * (yPos - origin.y) + origin.x;
point.y = Math.sin(angle) * (xPos - origin.x) + Math.cos(angle) * (yPos - origin.y) + origin.y;
return point;
}