Показать сообщение отдельно
Старый 14.01.2014, 18:04
Sonet вне форума Посмотреть профиль Отправить личное сообщение для Sonet Найти все сообщения от Sonet
  № 9  
Ответить с цитированием
Sonet

Регистрация: Apr 2012
Сообщений: 44
Код AS3:
public static function rotateImageAroundCenterOfViewPort(image:Image, value:int):void
		{
			trace("rotation Begin: x ", image.x, " y: ", image.y);
			var bounds:Rectangle = image.getBounds(image.parent);
			trace("rotation Begin: bounds.x ", bounds.x, " bounds.y: ", bounds.y);
			// Calculate rotation and shifts
			var angle:Number = value - Math.round(image.rotation);
			var radians:Number = angle * (Math.PI / 180.0);
			var shiftByX:Number;
			var shiftByY:Number;
			// Perform rotation
			switch(Math.round(image.rotation) / 90 % 4)
			{
				case 0:
				{
					shiftByX = image.parent.width / 2 - bounds.x;
					shiftByY = image.parent.height / 2 - bounds.y;
					break;
				};
				case 1:
				{
					shiftByX = image.parent.height / 2 - bounds.y;
					shiftByY = bounds.width - (image.parent.width / 2 - bounds.x);
					break;
				};
				case -2:
				case 2:
				{
					shiftByX = bounds.width - (image.parent.width / 2 - bounds.x);
					shiftByY = bounds.height - (image.parent.height / 2 - bounds.y);
					break;
				};
				case -1:
				{
					shiftByX = bounds.height - (image.parent.height / 2 - bounds.y);
					shiftByY = image.parent.width / 2 - bounds.x;
					break;
				};
				default:
				{
					break;
				}
			}
			var matrix:Matrix = new Matrix();
			matrix.translate(-shiftByX, -shiftByY);
			matrix.rotate(radians);
			matrix.translate(+shiftByX, +shiftByY);
			matrix.concat(image.transform.matrix);
			image.transform.matrix = matrix;
			trace("rotation End: x ", image.x, " y: ", image.y);
			trace("rotation: xShift ", shiftByX, " yShift: ", shiftByY);
		}
Немного поизучал тему. Выяснил на практике, что смещение надо задавать от текущего положения top-left точки. А нахождение этой точки в свою очередь связано с углом поворота. Но вот незадача: в image.rotation накапливается погрешность, из-за чего через некоторое число поворотов метод начинает сбоить и появляется лишний угол поворота.