# Matrix, rotate and translate help needed

**URL:** https://forum.kirupa.com/t/matrix-rotate-and-translate-help-needed/259795
**Category:** Uncategorized
**Created:** [May 8, 2008, 7:20pm UTC](https://forum.kirupa.com/t/matrix-rotate-and-translate-help-needed/259795 "2008-05-08T19:20:40Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![vintage](https://avatars.discourse-cdn.com/v4/letter/v/e68b1a/32.png) [@vintage](https://forum.kirupa.com/u/vintage)
#### Post date: [May 8, 2008, 7:20pm UTC](https://forum.kirupa.com/t/matrix-rotate-and-translate-help-needed/259795/1 "2008-05-08T19:20:40Z")

</div>

I draw() a new bitmap with the use of a transform matrix, but Im having serious problems with calculating de translate positions after rotate…

Is there some kind of formula/function to calculate the **matrix.translate** Numbers after **matrix.rotate**?

What I do now is something like this:

```auto

    var matrix = new Matrix();
    matrix.identity();
    matrix.rotate(2 * Math.PI * (rotatie / 360)); //rotatie is a Number that I "post" via a dropbox
    matrix.translate(helpX, helpY); //HELP ME HERE PLEASE :)

    
    var bmd:BitmapData = new BitmapData(b, h);
    bmd.draw(img, matrix);
    
    var bm:Bitmap = new Bitmap(bmd);
    img = bm; //img is a bitmap variable on the stage
    addChild(img);

```

I actually did read a gazzilion tuts and topics about the matrix, but I didnt find anything about this.  
Any help would be appreciated. thx

---

<div class="post-metadata">

### Author: ![substance](https://avatars.discourse-cdn.com/v4/letter/s/c6cbf5/32.png) [@substance](https://forum.kirupa.com/u/substance)
#### Post date: [May 9, 2008, 1:51am UTC](https://forum.kirupa.com/t/matrix-rotate-and-translate-help-needed/259795/2 "2008-05-09T01:51:37Z")

</div>

```php
		public static function PreRotate(img:Object):Array{
			RI = new Array();				
			for (var i:Number = -18; i < 19; i++){ 
				var rotationMatrix:Matrix = new Matrix(); 
				var temp:Number = Math.sqrt((img.width/2)*(img.width/2)+(img.height/2)*(img.height/2)); 
				rotationMatrix.identity()
				rotationMatrix.translate(-img.width/ 2,-img.height / 2); 
				rotationMatrix.rotate(i * Math.PI/180 * 10);				 
				rotationMatrix.translate(temp,temp);				
				RI* = new BitmapData(temp*2, temp*2, true, 0x000000); 					
				RI*.draw(img, rotationMatrix);						
			}
			return RI																	
		}

```

---

<div class="post-metadata">

### Author: ![vintage](https://avatars.discourse-cdn.com/v4/letter/v/e68b1a/32.png) [@vintage](https://forum.kirupa.com/u/vintage)
#### Post date: [May 9, 2008, 7:05am UTC](https://forum.kirupa.com/t/matrix-rotate-and-translate-help-needed/259795/3 "2008-05-09T07:05:39Z")

</div>

Thank you guys!  
The script substance posted is almost perfect, it takes the right sizes and rotation , but it draws the new bitmap on the wrong place, I have to puzzle a little with that.  
The new bitmaps need to be on the scene’s x:0 and y:0, while this script moves the new bitmap every time.  
I think its beqausse the translate uses the same values for x and y.

once again, THX 🙂

**update:**  
I am a little further now, the code doesnt draw my images on the wrong place anymore, but it does “crop” the “copy” to the smallest value.

For example:  
flash loads an image from 400_200 px  
Rotate it with 90  
and draw() a new image, with the right sizes, but the content is now 200_200 instead of 200\*400.

Im horrible with math, so I hope you guys can help me out once more 🙂

this is what I have so far…

```auto

    var rotatie:Number = 90;

    var rotationMatrix:Matrix = new Matrix();
    var tempX:Number = Math.sqrt((img.width/2)*(img.width/2));
    var tempY:Number = Math.sqrt((img.height/2)*(img.height/2));
    
    rotationMatrix.identity();
    rotationMatrix.translate(-img.width/ 2,-img.height / 2);
    rotationMatrix.rotate(2 * Math.PI * (rotatie / 360));
    rotationMatrix.translate(tempX,tempY);

    var bmd:BitmapData = new BitmapData(tempX*2, tempY*2, true, 0x000000);

```

Since my english isnt that good, here an image what happens  
 ![](http://forum.kirupa.com/uploads/kirupa/6863/d99865c3a571d9a6.png)

---

<div class="post-metadata">

### Author: ![vintage](https://avatars.discourse-cdn.com/v4/letter/v/e68b1a/32.png) [@vintage](https://forum.kirupa.com/u/vintage)
#### Post date: [May 9, 2008, 11:20am UTC](https://forum.kirupa.com/t/matrix-rotate-and-translate-help-needed/259795/4 "2008-05-09T11:20:02Z")

</div>

Solved, I now have this and works like a charm 🙂 (since I only need to rotate with 90)

```auto

var rotatie:Number = 90;
    var rotationMatrix:Matrix = new Matrix();
    var tempX:Number = Math.sqrt((img.width/2)*(img.width/2));
    var tempY:Number = Math.sqrt((img.height/2)*(img.height/2));
    var bmd:BitmapData;
    rotationMatrix.identity();
    rotationMatrix.translate(-img.width/ 2,-img.height / 2);
    rotationMatrix.rotate(2 * Math.PI * (rotatie / 360));
    rotationMatrix.translate(tempY,tempX); //this
    
    bmd = new BitmapData(tempY*2, tempX*2, false, 0x00ff00);
    bmd.draw(img, rotationMatrix);

```
