MrB
MrB

Reputation: 1594

When Rotating a bitmap it cuts corners off

I rotating some bitmap data in AS3. When ever I start rotating the bitmap the corners get cut off. I'm trying figure out what size the bitmap should be to fit the newly rotated bitmap.

The code Im currently using

var point:Point=new Point(BMPHolder.x+BMPHolder.width/2,BMPHolder.y+BMPHolder.height/2);
var RotateMatrix:Matrix = new Matrix();
var radains:Number = 45 * (Math.PI/180);

myMatrix.tx -= point.x;
myMatrix.ty -= point.y;
myMatrix.rotate(radains);
myMatrix.tx += point.x;
myMatrix.ty += point.y;

preApply = new BitmapData (BMPHolder.width,AdjustY, false);
preApply.draw(BMPHolder,myMatrix, null, null, null, false);

With the help of the answer i was able to get this working. This is the code I had to add.

       //Anchor point.
        xArray.push(0);
        yArray.push(0);

        xArray.push((BMPHolder.width * Math.cos(radains)) - (BMPHolder.height * Math.sin(radains)));
        yArray.push((BMPHolder.width * Math.sin(radains)) + (BMPHolder.height * Math.cos(radains)));

        xArray.push((BMPHolder.width * Math.cos(radains)) - (BMPHolder.y * Math.sin(radains)));
        yArray.push((BMPHolder.width * Math.sin(radains)) + (BMPHolder.y * Math.cos(radains)));

        xArray.push((BMPHolder.x * Math.cos(radains)) - (BMPHolder.height * Math.sin(radains)));
        yArray.push((BMPHolder.x * Math.sin(radains)) + (BMPHolder.height * Math.cos(radains)));


        var NewWidth:Number = MaxValue(xArray) - MinValue(xArray);
        var NewHeight:Number = MaxValue(yArray) - MinValue(yArray);

        myMatrix.tx -= point.x;
        myMatrix.ty -= point.y;
        myMatrix.rotate(radains);

        myMatrix.tx += Math.abs(NewWidth/2);
        myMatrix.ty += Math.abs(NewHeight/2);

I'm assuming that I could have used the matrix to find all the points but I just did it by hand to keep my sanity. Thanks again

Upvotes: 0

Views: 296

Answers (1)

John Percival Hackworth
John Percival Hackworth

Reputation: 11531

Rotate your corner points, and then extract the maximum and minimum x and y values from the set of rotated points. You can then use those to compute the minimum size bitmap to hold the data. You'll want to something like the folioing pseudocode:

points = [[0,0], [0,height], [width,0], [width, height]]
points = xform(points, myMatrix)
minx = min(xcoords(points))
maxx = max(xcoords(points))
miny = min(ycoords(points))
maxy = max(ycoords(points))

newwidth = maxx - minx
newheight = maxy - miny

You will then need to translate your rotated bitmap by minx, miny to put it in the right spot.

Upvotes: 2

Related Questions